EIP-5656: MCOPY - An efficient EVM instruction

EIP-5656 introduces MCOPY. Addressing gas cost challenges, MCOPY benefits, use cases, and impact, presenting a promising upgrade for Ethereum's ecosystem.

EIP-5656: MCOPY - An efficient EVM instruction

EIP-5656 is a proactive Ethereum Improvement Proposal (EIP) that introduces a new EVM instruction called MCOPY. EIP-5656 with Dencun upgrade aims to enhance the EVM's efficiency by introducing a new opcode for improved memory copying.

Problem :

Memory copying in the Ethereum Virtual Machine (EVM) is a fundamental operation crucial for various tasks, such as building data structures and copying objects. However, existing methods, especially in scenarios involving arbitrary offsets, incur high gas costs and are not as efficient as desired. The identity precompile, once an option for memory copying, lost its efficiency due to changes in gas costs. Additionally, the need for more efficient memory copying becomes apparent in computationally heavy operations like EVM384.

Solution :

EIP-5656 addresses the inefficiencies in memory copying by introducing a new EVM instruction, MCOPY. The purpose of MCOPY is to significantly improve the efficiency of memory copying operations. It allows for the copying of memory with reduced gas costs, offering benefits for both non-overlapping and overlapping memory areas. MCOPY is a proactive implementation aimed at enhancing the EVM's performance without introducing trade-offs or incompatibilities. This opcode serves as a more gas-efficient alternative to existing methods like MSTORE and MLOAD, contributing to the optimization of smart contract execution on the Ethereum platform.

Deep Dive into EIP - 5656

MCOPY:

MCOPY is a new EVM opcode that is proposed in EIP-5656. It is designed to optimize the performance of memory copying within the EVM, offering a more efficient method for constructing data structures. It works by taking two memory pointers as input and copying the data from the source pointer to the destination pointer. The size of the data to be copied is specified by a third input parameter.

It is more efficient than the current method of memory copying in the EVM, which involves using a loop to copy the data one byte at a time. MCOPY is able to copy the data in bulk, which is much faster. It also has the advantage of being more concise than the current method of memory copying. This makes it easier to write and read smart contracts.

Benefits:

Improved performance: MCOPY is more efficient than the current method of memory copying in the EVM, which involves using a loop to copy the data one byte at a time. MCOPY is able to copy the data in bulk, which is much faster.

Reduced gas costs for smart contracts that use memory copying.

Improved readability and maintainability of smart contracts.

Memory Copying EVM State
Workflow for copying memory in the EVM proposed by EIP-5656


Memory Copying Overview:

Memory copying involves transferring blocks of data from one memory location to another, a fundamental operation in computing. It is commonly used for tasks like building data structures and copying objects. MCOPY is designed to handle non-overlapping and overlapping memory areas, and it supports moving memory backward.

Use Cases:

Copying a slice of data from one memory location to another.

Building a data structure, such as an array or a linked list.

Copying an object, such as a smart contract struct.

Moving memory backward, such as when resizing an array.

Efficiency Gains:

In terms of efficiency gains, the introduction of MCOPY significantly reduces the gas cost associated with copying memory.
For instance, copying 256 bytes of memory using MCOPY incurs a gas cost of 27, compared to at least 96 gas using the most common previous method, which required multiple instructions. This reduction in gas cost is substantial and contributes to overall efficiency in contract execution.

MCOPY would provide a native way to copy memory within Ethereum contracts, which is currently done using a combination of MLOAD and MSTORE opcodes.

The study analyzed EVM execution traces for 1200 historical blocks to identify cases where memory copying is used. The results showed that memory copying is a common operation in Ethereum contracts, and that MCOPY could provide significant gas savings in many cases.

For example,

it is found  that MCOPY could save up to 80% of the gas used for copying a 10kb memory region. It states that MCOPY could eliminate the need for many memory copy loops in Solidity contracts.

Overall, it is found that MCOPY is a promising new opcode that could improve the performance and efficiency of Ethereum contracts.

Copies of single words:

The study identified copies of single words by examining values loaded from memory via MLOAD, tracking their existence as they are shuffled around the stack during execution of other opcodes, and noting if these values are stored back to memory via MSTORE. When these tracked values are consumed (used as parameters to other opcodes) or duplicated via DUP, they are no longer considered.

This approach is effective because it identifies all cases where a value is loaded from memory and then later stored back to memory, even if the value is manipulated in between.

Copies larger than a single word:

It identifies occurrences of copies larger than a single word by looking for multiple single-word copies which operate on source and destination offsets along consecutive 32-byte boundaries.

This approach is effective because it identifies all cases where multiple single-word copies are used to copy a contiguous region of memory.

Cost model:

The study calculated a linear cost model for MCOPY by fitting a line through Go-ethereum client benchmarks measuring varying copy sizes from 32 bytes to 10kb on an i5-6600K processor (32kb L1 cache).

The model is as follows:

gas_mcopy(n) = 2 + 2.56(n-1)

where n is the number of EVM-words being copied.

The model takes into account the overhead of stack manipulation from Geth's EVM, but it is still somewhat of an overcharge because it does not include the cost of memory access.

Solidity augmentation:

Augmented Solidity to use MCOPY for multi-word memory copying helper functions in place of currently-used MLOAD/MSTORE copy loops. This did not identify any cases of memory copying present after recompiling the Solidity contract test suite.

This result suggests that MCOPY could be used to eliminate many memory copy loops in Solidity contracts, which would further improve the performance and efficiency of Ethereum contracts.

There are  strong evidence that MCOPY would be a valuable addition to the EVM. MCOPY could provide significant gas savings in many cases, and it could also eliminate many memory copy loops in Solidity contracts.

869108ea-062a-438f-94b8-9a6506fa4f9b
EVM-mcopy-analysis

Note:

Scripts and utilities to reproduce the tracing and analysis are
at EVM-mem-copy-tracer

MCOPY fills a missing gap in current approaches to copying memory in the EVM

Comparison with Existing Methods:

Currently, developers often resort to using MSTORE and MLOAD instructions for memory copying. MCOPY essentially combines the functionality of these two opcodes, providing a more streamlined and efficient alternative. While developers are not compelled to use MCOPY, it is expected that its introduction will lead to widespread adoption as it offers a more gas-efficient solution.

Developer Impact and User Benefits:

Developers are not mandated to switch to MCOPY, and existing references to MSTORE or MLOAD are not affected. However, it is rational to anticipate that, given the efficiency gains, most developers will choose to utilize MCOPY in place of the previous methods. The more gas-efficient contracts resulting from the use of MCOPY ultimately benefit end-users, as they are the ones calling the contract and incurring gas costs.

Hence, MCOPY is a new EVM opcode that is proposed in EIP-5656. It is designed to optimize the performance of memory copying within the EVM, offering a more efficient method for constructing data structures. MCOPY is expected to have a number of benefits for the Ethereum ecosystem, including:

  • Improved performance of smart contracts
  • Reduced gas costs
  • Improved readability and maintainability of smart contracts

While developers are not forced to use MCOPY, it is rational to expect that most developers will utilize MCOPY in place of MSTORE/MLOAD going forward. This is because MCOPY offers a number of advantages over the current method of memory copying, including improved performance and reduced gas costs.

Ultimately, the adoption of MCOPY by developers is beneficial to end users. As MCOPY can make contracts more gas efficient, end users will pay less gas when calling these contracts.

In conclusion, MCOPY is a promising new EVM opcode that has the potential to significantly improve the performance and efficiency of smart contracts. It is expected to be widely adopted by developers in the coming years.

Read More

Censorship Resistance vs Scalability

Ethereum Layer 2 Projects

scroll-successfully-launches-mainnet-for-zkevm-boosting-ethereum-scalability

Solutions to Scalability: Can Off-Chain Payments Scale Blockchain?

what-is-sharding-and-why-is-it-important

ethereum-updates-q4-round-up-scalability-r-d-subsidy-programs-website-redesigning-eth-recorded-new-all-time-high

_________________________________________________________

Disclaimer: The information contained in this website is for general informational purposes only. The content provided on this website, including articles, blog posts, opinions, and analysis related to blockchain technology and cryptocurrencies, is not intended as financial or investment advice. The website and its content should not be relied upon for making financial decisions. Read full disclaimer and privacy Policy.

For Press Releases, project updates and guest posts publishing with us, email to contact@etherworld.co.

Subscribe to EtherWorld YouTube channel for ELI5 content.

Share if you like the content. Donate at avarch.eth or Gitcoin

You've something to share with the blockchain community, join us on Discord!

Follow us at Twitter, Facebook, LinkedIn,  and Instagram.


Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to EtherWorld.co
Great! Next, complete checkout for full access to EtherWorld.co
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.