New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple SSTORE
calls for the same storage location
#2908
Comments
User @adamskrodzki raised this issue on gitter as well, with an even more complex struct. It does 8 writes (1 full price + 7 discounted) for this:
No issues with packing though, they all end up in the same storage slot. This i can confirm on latest stable (
|
This might be related to #1137 |
Yes it's the way the compiler works, it fits in one 32-bytes area but uses 2
|
@cdetrio They are different issues. This is not array related, and technically it is not a bug. At the time of this report, the way storage writes are done (in the case of one-word structs) is:
Old S is read from storage, modified, and the modified result is written to storage, and this is repeated once for each member. We want it to be:
This should also be extended to work for any size struct, which is probably not too difficult since the methods for manipulating packed storage are already in place, and seem to work perfectly fine.
This should result in one |
@androlo that should be fairly safe to implement once storage-related code is written in the IR, but before that it may be a risky change. |
I wasn't sure if it was a bug or an enhancement. Seems to be an enhancement then! |
Discovered same using 0.4.19+commit.c4cbbb05.Emscripten.clang, as an example please take a look at this Smart Contract at Ropsten: |
My bad, my struct size is much larger than 32 bytes and it's not related to this issue - it's just how EVM works (splitting everything to 32 bytes words and calling SSTORE for each). |
Not sure if this has yet been noted, but doing this reduces costs to what is expected:
The optimizer seems smart enough to cache all changes to a |
This might be fixed by #3693 , but we should check. |
This has been fixed. |
Solidity version
I tested a couple of version from
0.4.0
onwards.Source code
Steps to reproduce
Just deploy the contract, call
store
method with non-0 values, and look at the executed OPCODESResult
It executed two
SSTORE
OPCODESExpected Behaviour
As the Struct is composed of 2
uint128
, it fits in one 32-bytes area. However, when callingstore
, it executes twoSSTORE
opcodes, thus increasing the gas usage. This increases the gas usage by 5,000 (since it just re-writes on the some storage location), which could be improved.The text was updated successfully, but these errors were encountered: