-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Refactor/reentrancy guard gas optimization #1056 #1057
Refactor/reentrancy guard gas optimization #1056 #1057
Conversation
I believe changing to uint8 will save storage costs in the case that other storage variables can fit within the same 32bit word |
Yes, but with ~500 gas overhead on read-write. Was't sure it worths it in a long run. |
Where does that ~500 gas overhead come from? |
Before writing 1 byte solidity has to read (200 gas) current 32-bytes slot and merge 1 byte into it and finally write back 32-bytes slot. Mechanics is the same as in partial writes of disk sectors. This extra gas overhead is noted in solidity docs afair. |
Either way, you have to SSTORE and SLOAD a whole word (32 bytes). I'm not sure why 500 gas would be needed to do a bitmask. |
You don't need SLOAD to write uint. But this is not the case for uint8. |
Ah makes sense, you're right |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @Eenae, thanks! I'm puzzled by Solidity not providing any guarantees regarding enums, specially considering there's (AFAIK) no way to retrieve those values from outside Solidity (i.e. they are not specified in the ABI, nor anywhere else).
It is on the roadmap though: ethereum/solidity#1602 |
Fixes #1056
🚀 Description
Implemented solution proposed in the issue. Using integer constants to represent state of the guard. Not using
enum {Dummy, Free, Locked}
because solidity documentation as far as I can see does't provide necessary guarantees on underlying integer values of enum members (again, our goal is to never touch 0).It could be seen in Remix that execution cost of each empty nonReentrant call is 10390 gas now.
npm run lint:all:fix
).