Conversation
zzzckck
approved these changes
Jan 17, 2024
This was referenced Apr 8, 2024
This was referenced May 9, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BEP-342: Implement EIP-5656: MCOPY
1. Summary
As part of Cancun upgrade, EIP-5656: MCOPY is required to be implemented to BSC.
2. Abstract
Provide an efficient EVM instruction for copying memory areas.
3. Motivation
Memory copying is a basic operation, yet implementing it on the EVM comes with overhead.
This was recognised and alleviated early on with the introduction of the "identity" precompile, which accomplishes
memory copying by the use of
CALL's input and output memory offsets. Its cost is15 + 3 * (length / 32)gas, plusthe call overhead. The identity precompile was rendered ineffective by the raise of the cost of
CALLto 700, but subsequentlythe reduction by EIP-2929 made it slightly more economical.
Copying exact words can be accomplished with
<offset> MLOAD <offset> MSTOREor<offset> DUP1 MLOAD DUP2 MSTORE,at a cost of at least 12 gas per word. This is fairly efficient if the offsets are known upfront and the copying can be unrolled.
In case copying is implemented at runtime with arbitrary starting offsets, besides the control flow overhead, the offset
will need to be incremented using
32 ADD, adding at least 6 gas per word.Copying non-exact words is more tricky, as for the last partial word, both the source and destination needs to be loaded,
masked, or'd, and stored again. This overhead is significant. One edge case is if the last "partial word" is a single byte,
it can be efficiently stored using
MSTORE8.As example use case, copying 256 bytes costs:
MLOAD/MSTOREinstructionsAccording to an analysis of blocks 10537502 to 10538702, roughly 10.5% of memory copies would have had improved performance with the
availability of an
MCOPYinstruction.Memory copying is used by languages like Solidity and Vyper, where we expect this improvement to provide efficient means of building
data structures, including efficient sliced access and copies of memory objects. Having a dedicated
MCOPYinstruction would also addforward protection against future gas cost changes to
CALLinstructions in general.Having a special
MCOPYinstruction makes the job of static analyzers and optimizers easier, since the effects of aCALLin generalhave to be fenced, whereas an
MCOPYinstruction would be known to only have memory effects. Even if special cases are addedfor precompiles, a future hard fork could change
CALLeffects, and so any analysis of code using the identity precompile would onlybe valid for a certain range of blocks.
Finally, we expect memory copying to be immensely useful for various computationally heavy operations, such as EVM384,
where it is identified as a significant overhead.
4. Specification
The instruction
MCOPYis introduced at0x5E.Input stack
dstsrclengthThis ordering matches the other copying instructions, i.e.
CALLDATACOPY,RETURNDATACOPY.Gas costs
Per yellow paper terminology, it should be considered part of the
W_copygroup of opcodes, and follow the gas calculation forW_copyin the yellow paper. While the calculation in the yellow paper should be considered the final word, for reference, as of time of this writing, that currently means its gas cost is:Output stack
This instruction returns no stack items.
Semantics
It copies
lengthbytes from the offset pointed atsrcto the offset pointed atdstin memory.Copying takes place as if an intermediate buffer was used, allowing the destination and source to overlap.
If
length > 0and (src + lengthordst + length) is beyond the current memory length, the memory is extended with respective gas cost applied.The gas cost of this instruction mirrors that of other
Wcopyinstructions and isGverylow + Gcopy * ceil(length / 32).5. Rationale
Production implementation of exact-word memory copying and partial-word memory copying can be found in the Solidity, Vyper and Fe compilers.
With EIP-2929 the call overhead using the identity precompile was reduced from 700 to 100 gas.
This is still prohibitive for making the precompile a reasonable alternative again.
6. Backwards Compatibility
This BEP introduces a new instruction which did not exist previously. Already deployed contracts using this instruction could change their behaviour after this EIP.
7. Security Considerations
There are no known security considerations introduced by this change.
8. License
The content is licensed under CC0.
9. Reference
Alex Beregszaszi (@axic), Paul Dworzanski (@poemm), Jared Wasinger (@jwasinger), Casey Detrio (@cdetrio), Pawel Bylica (@chfast), Charles Cooper (@charles-cooper), "EIP-5656: MCOPY - Memory copying instruction [DRAFT]," Ethereum Improvement Proposals, no. 5656, February 2021. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5656.