Skip to content
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

Conversation

Eenae
Copy link
Contributor

@Eenae Eenae commented Jul 2, 2018

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.

  • 📘 I've reviewed the OpenZeppelin Contributor Guidelines
  • ✅ I've added tests where applicable to test my new functionality.
  • 📖 I've made sure that my contracts are well-documented.
  • 🎨 I've run the JS/Solidity linters and fixed any issues (npm run lint:all:fix).

@BrendanChou
Copy link
Contributor

I believe changing to uint8 will save storage costs in the case that other storage variables can fit within the same 32bit word

@Eenae
Copy link
Contributor Author

Eenae commented Jul 6, 2018

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.

@BrendanChou
Copy link
Contributor

Where does that ~500 gas overhead come from?

@Eenae
Copy link
Contributor Author

Eenae commented Jul 9, 2018

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.

@BrendanChou
Copy link
Contributor

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.

@Eenae
Copy link
Contributor Author

Eenae commented Jul 11, 2018

You don't need SLOAD to write uint. But this is not the case for uint8.

@BrendanChou
Copy link
Contributor

Ah makes sense, you're right

@nventuro nventuro added kind:improvement contracts Smart contract code. labels Jul 28, 2018
@nventuro nventuro added this to the v1.12.0 milestone Jul 28, 2018
Copy link
Contributor

@nventuro nventuro left a 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).

@axic
Copy link
Contributor

axic commented Jul 30, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Smart contract code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReentrancyGuard gas usage optimization
4 participants