New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Copying Memory in EVM, how hard can that be? by Elia Anzuoni | Devcon SEA

DevconTue, Oct 7, 2025, 12:00 AM

Memory copy operations in EVM are a useful feature, but there are different ways to do. How do they differ? Which is the best? The options are: MLOAD+MSTORE loop Identity Precompile The new MCOPY opcode Based on concrete examples we will explain how these options differ. We will use different examples as the amount of bytes copied makes a difference. For all these options we will present gas consumption and code size. This way we can compare the different options to copy memory and crown the ult Speaker(s): Elia Anzuoni Skill level: Intermediate Track: Core Protocol Keywords: Core Protocol, Gas, Developer Infrastructure, compilers, optimised Follow us: https://twitter.com/efdevcon, https://twitter.com/ethereum, https://warpcast.com/devcon Learn more about devcon: https://www.devcon.org/ Learn more about ethereum: https://ethereum.org/ Visit the https://archive.devcon.org/ to gain access to the entire library of Devcon talks with the ease of filtering, playlists, personalized suggestions, decentralized access on Swarm, IPFS and more. Devcon is the Ethereum conference for developers, researchers, thinkers, and makers. Devcon SEA was held in Bangkok, Thailand on Nov 12 - Nov 15, 2024. Devcon is organized and presented by the Ethereum Foundation. To find out more, please visit https://ethereum.foundation/

Transcript

[Music] hi everyone am I audible thanks again um good to see you I'm Elan suani I also work at chain security I can guarantee this was not on purpose we didn't bribe be organizers it was so nice of them uh my topic for today is arguably the best fit for a for l talk because it turns out it's not that hard to copy m in evm especially since Cancun uh but yeah we'll see exactly how much so um yeah yeah the good the right way to think about it in my opinion is before Cancun and after Cancun because since Cancun we have a new OP code that does exactly that and it's arguably the best way to do it but before then you had to implement this some other way and there were two main ways to do that to copy from memory to memory in the evm so one was to do an M load plus M store Loop so you would do 32 bytes at a time you would load 32 bytes from memory onto the stack and store it back from the stack to the destination in memory and this used to be well quite heavy in terms of code size because it's a full loop and it also had quite a bad uh gas cost in terms of um gas cost per memory word cped uh the alternative was calling the identity recompile that we just saw with the last presentation and while that has a little less code size because it just a static call that you can do to the to the contract still the gas per memory word is good enough it's only three gas per memory word that you copy but uh the overhead is quite big because you have to do a call so it's at least I think it's 180 yeah we'll see later anyway uh since Cancun all our problems are solved because we have a new OP that does exactly that has the least code size because it's just one off code uh the least amount of gas so now there's no reason not to use it and as I'll tell you it it's very much widespread now so let's just go quickly over some numbers uh one way to implement the mload M store Loop is in software this is done in a in a quite popular yet a bit old solidity library and because it's done in high level solidity rather than entirely an assembly then you see you have two checked additions a checked subtraction all this done for every memory word that you copy which adds up to a gas cost of around 286 gas per memory memory word copied which is quite bad uh then we have the same concept but implemented entirely in assembly which is what the solidity compiler will autogenerate for you it's it's a function if you inspect the IR it's go copy memory to memory with cleanup I think and yeah it's a title Loop so it has less overhead so over overall the cost per per memory word copy is only a 67 yeah 67 gas and the code size is also much smaller uh then just to see an example on of how the identity Recon pile would be used it's used by C4 library and and also that it's what Viper used used to generate to to copy memory to memory before Cancun and yeah the cost per word is the best it's just it's just three words three gas but the the the overhead the constant term as you can see is quite big because you have to make a static call that has also a fixed cost of a 15 inside and then you have to do some checks so it's not ideal for for small for small copies now keep in mind that we're neglecting the memory expansion cost because that'll be the same whichever way you whichever method you opt for and and also we're doing this comparison right now with these upod pricing but keep in mind that those have changed and will change again in the future so whatever conclusion on which method might be the most convenient now maybe wasn't the same you you wouldn't have come to same conclusion five years ago for example uh but yeah to to just to be sure about it now the M Copy op code is used by both solidity and Viper uh for Cancun uh contract and the gas cost is the least the code size is the least so everything is good future is bright um I didn't have the time to add the slides to this but from the same analysis that uh that Dominic and the other colleagues at t Security did it turns out that already now uh the M Copy op code is already used more than the identity recompile even though it's only new it's only quite new since Cancun so it shows that I mean uh I don't know exactly how to phrase it maybe there a lot of new contracts deployed that already outnumber the N the number of I guess solidity Viper contracts for legacy so yeah it's it's quite popular already then yeah I've prepared a remix setup for you and also if you want to shoot a message to our sales representative to get an audit which is never a bad idea feel free to do so uh yeah I'll take any questions thank you for the session please do you have oh wow you have a lot today I'll try hello uh you said that the memory expansion cost is the same in all three cases but my concern is that with the loop option if the memory you are copying is not a multiple of 32 mhm not only you may copy memory you don't need to copy so that's more expansion but also you may override something that is outside of your destination like just after your destination yeah so um the memory expansion cost is actually I'm only 87% sure about this but it's only computed on the number of memory words rounded up so even if you end up writing at most 31 bytes you don't um incur memory exion cost for another word uh uh so it is the same if even if you have a right within the same word that you've already paid for um then what was your other question ah for overlapping yeah uh if in in some of some of those functions are specified for cases where the overlap is okay for example in in in the the U one because I think that one is only used uh to allocate at the tip of the memory so there's nothing afterwards but in cases where that's not okay for example the library they do special tricks like they they they round it up with an M store that's aligned to the so that to the last bite that you want to write to and the rest they mask it with the with the content that's already there so you basically writing back what's already there on the P that overlaps and on the just on the new part I say the 20 20 bytes you'll write the new stuff one more question for OT okay no question I'm curious alas um so as non technical person yeah um being among almost everybody's a technical here how do you communicate to a nois like me to be able to kind comprehend in simple language huh I think I have an act for it like um I it just comes naturally to me like for me I don't think I've understood something until I can explain it to my mom so so I guess I guess all right thank you so much guys let's give him a hand of Applause wow

Automatic transcript — names and jargon may be misspelled.