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.
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.
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 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.
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 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.
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.
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.
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.
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.
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.
Scripts and utilities to reproduce the tracing and analysis are
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.
For Press Releases, project updates and guest posts publishing with us, email to email@example.com.
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!