-
Notifications
You must be signed in to change notification settings - Fork 994
Precompile Caching MVP #8095
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
Precompile Caching MVP #8095
Conversation
49dd4dc to
e9155f3
Compare
0d95b1d to
67f912f
Compare
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.
I think it makes sens to have a cache per precompile as we discussed. Also you need to change the key to use a hashing function that has no collisions, as the hashcode method that returns int doesn't can have collisions
evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java
Outdated
Show resolved
Hide resolved
ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/BenchmarkSubCommand.java
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java
Show resolved
Hide resolved
d519ece to
73b29e1
Compare
|
This pr is stale because it has been open for 30 days with no activity. |
|
This pr was closed because it has been inactive for 14 days since being marked as stale. |
5de5681 to
93bb963
Compare
evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractPrecompiledContract.java
Show resolved
Hide resolved
ahamlat
left a comment
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.
Could you review the way the hashcode is calculate for some precompiles (see below) ? It is sometimes calculated twice where we can store it and use it to cache the result. Also, I wonder why you didn't add a cache for KZGPointEvalPrecompiledContract.
evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java
Outdated
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java
Outdated
Show resolved
Hide resolved
|
Other than the small requested changes above, the PR is great, I think we can decouple it from parallel transaction execution as it can help even when parallel transaction execution is not enabled. |
evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java
Outdated
Show resolved
Hide resolved
|
An interesting idea from this implementation, is that you created a cache where the eviction mechanism is based on hashcode collisions. It is like a hashmap where we keep only one node behind each bucket (hashcode index), the new value will always replace the existing one. |
Will do, and will add similar caches for the pectra BLS precompiles. |
08fd69d to
352c9f3
Compare
|
OK, I found the issues with false positives. It appears we are subsequently mutating the input bytes. So the input value in our precompile result tuple was getting mutated after we cached it, and that was the source of the false positives. What I have done for all precompiles is to copy the input in the precompile result tuple IF we have caching enabled. This seems to be the sweet spot for removing false positives. We should have little to no impact on precompile performance or overhead if caching is disabled. Since these changes, I have not seen any false positives: If you would, please re-review at your leisure @ahamlat 🙏 |
ahamlat
left a comment
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.
LGTM, just few comments.
I will approve once I checked the impact of the copy of the input.
evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java
Outdated
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java
Outdated
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java
Outdated
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java
Outdated
Show resolved
Hide resolved
evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java
Outdated
Show resolved
Hide resolved
ahamlat
left a comment
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.
Could you update the benchmarks in the description with the results from the last implementation ?
The CPU profiling shows an improvement on the nodes running this PR :
Control node 1 / 11 samples for MessageCallProcessor.executePrecompile

Control node 2 / 11 samples for MessageCallProcessor.executePrecompile

Node 1 running this PR / 4 samples for MessageCallProcessor.executePrecompile

Node 2 running this PR / 4 samples for MessageCallProcessor.executePrecompile

|
Updated the pdf docs. Input copying doesn't seem to be a big problem in this case. The ecmul point-at-infinity optimization performs better without the cache - interestingly even when we run the optimization check before the cache check. But otherwise the current implementation looks to be better all around. |
352c9f3 to
fb18fd6
Compare
|
Added bls precompile caching also. The gas-benchmarks suite doesn't have bls precompile tests, but evmtool gives some pretty dramatic results: versus |
995504b to
edeeace
Compare
BLS doesn't go live until pectra, thus no stats yet... |
Over 4 days, I see a pretty low hit ratio: nuc 8: 198 / 2665 how low of a ratio makes it not worth it ? |
It depends on :
So in the case of the metrics you shared, the cache has an overhead on 2665 calls and could avoid the execution of only 198 precompile calls. |
1c0ae22 to
4e742ea
Compare
So the execution time of KZGPointEval call is around 500 us on my laptop, which pretty slow. Will suggest a PR tomorrow to add it to the existing benchmarks. I think even if it is only 7 % hit ratio, we can keep it. |
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
…ck before cache check in ecmul Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
… inputs may be large Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
Signed-off-by: garyschulte <[email protected]>
4e742ea to
aad1762
Compare



PR description
PR adds precompile caching behavior for an MVP set of precompiles that are costly enough to benefit from caching. Provision is added to disable caching via command line arg (for gas costing reasons), but it is enabled by default in besu, and disabled by default in evmtool and benchmark subcommand.
Changes:
MVP precompiles include:
Feedback welcome on the design choices:
Parallel transaction execution should benefit from precompile caching when state conflicts are detected. Attached are preliminary results from the nethermind gas-benchmarks suite which indicate performance does not seem to take a hit for cache checking and misses, and the caching itself is effective for repetitive/identical inputs
updated:
ecmul_new.pdf
ecrec_new.pdf
blake2f_new.pdf
Fixed Issue(s)
Thanks for sending a pull request! Have you done the following?
doc-change-requiredlabel to this PR if updates are required.Locally, you can run these tests to catch failures early:
./gradlew build./gradlew acceptanceTest./gradlew integrationTest./gradlew ethereum:referenceTests:referenceTests