perf: remove empty HashMap instances from TrieUpdates and HashedPostState #13976
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the simplified version of #13929.
Checklist
Checklist
Problem
In our benchmarks, we discovered that
TrieUpdates::insert_storage_updates
andHashedPostState::from_bundle_state
are adding hundreds of thousands of emptyHashMap
instances. Since an emptyHashMap
does nothing, they can be safely removed without affecting correctness. By removing these emptyHashMap
s, we can reduce both memory usage (particularly heap allocation) and computation time.To help visualize this better, here's an example of a state update from an ERC20 transaction:
In the example above, even though 6 accounts are involved, only one (the ERC20 contract) actually has storage changes. The issue is that the other 5 accounts contribute 5 empty
HashMap
s, which are no-ops and unnecessary.Solution
Eliminate empty
HashMap
s by simply not adding them.Notable changes
Results
Run time analysis
We benchmarked the "before" and "after" under the same condition (same machine, test, TPS, config). The results show a significant improvement across all three of our playbooks:
Branch statistics
For each added
if
statement, we tracked how often the condition evaluates totrue
orfalse
. Here’s the summary:The data above shows that while
if storage_updates.is_empty()
does not lead to significant changes, theif !storage.is_empty() { ... }
inHashedPostState::from_bundle_state
is highly effective.E2E Benchmarks