Conversation
Currently, `EVMEncodeABI` iterates valuesArray (ArrayValue) twice. The first iteration is used to report ABI computation, and the second iteration is used to encode values. This optimization combines ABI computation reporting and value encoding into one ArrayValue iteration.
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
📝 WalkthroughWalkthroughModified ABI encoding computation reporting in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
turbolent
left a comment
There was a problem hiding this comment.
This is potentially dangerous: We should always estimate and meter computation before performing computation
|
Hi @turbolent
The optimization meters elements during the loop before each computation, which allows the "within-limits" scenario to iterate the loop just once (instead of twice). Unfortunately, it sacrifices the "exceeds-limits" scenario because the loop is allowed to continue until metering stops it (since it doesn't know upfront the loop doesn't need to start). So this optimization speeds up the "within-limits" scenario at the cost of the "exceeds-limits" scenario being less efficient. I can close this PR if the tradeoff isn't worthwhile or risks cannot be mitigated. Thoughts? |
|
I think the issue here is: empty array is encoded for free. before Iterate should make it similar. |
|
Hi @bluesign
Are you referring to empty The current code (without this PR) is not metering empty Elements are metered regardless of type and value, so an element that is an empty array would still be metered. This aspect is also not affected by this PR. BTW, I'm new to the metering-related codebase (onboarding and looking for existing tests/benchmarks). Please let me know if I miss anything. |
|
@fxamacker ah yeah you are right, it is almost identical. I thought it is calling previously |
|
In this case in the "exceeds-limits" scenario the change is minimal. Imagine the following case user has 100 computation left in transaction. and needs 200 computation for whole operation, where each loop of the operation takes 10 computation. current logic:
in the new logic we are performing some work we technically wouldn't need to, but that work is charged to the user, so that is fine. I think we should optimize for the happy path (which this does), but make sure that any work done is being charged as fees to the user (which it is). |
Updates #8401
Currently,
EVMEncodeABIiterates valuesArray (ArrayValue) twice. The first iteration is used to report ABI computation, and the second iteration is used to encode values.This optimization combines ABI computation reporting and value encoding into one ArrayValue iteration.
Details
The optimization checks limits before each computation inside the loop. So the loop starts and it will exit early to prevent limits being exceeded during the loop.
The happy path (entire loop is within limits) is optimized by looping once instead of twice.
The unhappy path (entire loop doesn't fit within limits) is potentially less efficient because it is detected during the loop. So computations that are within limits are allowed during the loop, and the loop is exited early if the next computation won't fit within limits.
Caveats
The current code (without this PR) is not metering empty
valuesArray, so that aspect is unaffected by this PR.Elements are metered regardless of type and value, so an element that is an empty
valuesArraywould still be metered. This aspect is also unaffected by this PR.Context
I found this quick hit, while looking for other (larger) cross-vm optimizations.
## Summary by CodeRabbit* Refactor* Enhanced computation intensity reporting for encoding operations to provide more granular, per-element metrics, enabling more detailed performance tracking and visibility.EDIT: added Details and added Caveats.