-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Compile time evaluation of constant hashes #4024
Comments
This has to do with the trade-off between code deployment costs and runtime costs. There is a setting in the optimizer called "runs" where you can adjust it. |
Note that storing a single byte in code costs at least 200+68 gas. |
I understand this, which is why the second example ( This also does not explain why |
OK, perhaps this is not at all related to the compiler finding the most efficient way, but rather to the fact that the value of |
A place to start would be to check if the check just after |
Considering the two test cases,
In both these cases, execution does not reach that line (although it does and is correctly handled for the empty hash). In both of the test cases, Each time, This is as far as my ability to debug C++ can take me... Setup:
|
OK, I will take a look later this week! |
@chriseth have you checked this by any chance? |
No, sorry. |
Are there plans to improve this in the near future? From @chriseth's description above it sounds like the tools to achieve this are already present, just not being used in all scenarios. I find myself recommending people to use hard-coded |
The problem is that 1) it is dangerous to re-implement this outside of the EVM and 2) it is not clear how we should handle memory objects at compile-time. Concerning hashes: The optimizer should actually remove the keccak256 opcode. It might not do it because it is cheaper depending on the "runs" setting (a 32 byte constant is rather expensive to deploy). |
Could we make one exception and evaluate |
This could be a good exception. I would propose to put the value in the annotation object of the constant as opposed to fully supporting this in the type system as we do for constants. |
Do creationCode hashes get compile-time-hashed as well? Such as this:
These hashes are useful for optimizing Create2 calculation |
I don't think this is optimized. There is a proposal to add a function to |
It is covered in #8798. |
There's room for improvement in optimising compile-time evaluation of constant hashes when optimisation is enabled.
With optimisation off, none of the constant hashes is evaluated at compile-time. In each case the
keccak256
operation is being done at run time. This is somewhat expected.With optimisation enabled, only the first (
keccak256('')
) is evaluated at compile time. The remainder (keccak256('12345678901234567890123456789012')
,keccak256(uint256(0))
) are evaluated at run time, as if optimisation were off. This is unexpected. Expected behaviour would be to evaluate all constant hashes at compile time.Test environment: Remix, Solidity version 0.4.23+commit.124ca40d
Conclusions reached by inspecting the generated byte code. It's quite obvious if the hash is present or not.
The text was updated successfully, but these errors were encountered: