-
Notifications
You must be signed in to change notification settings - Fork 1.5k
v1/ast: Add lazy hash evaluation to Array type #8155
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
Open
alex60217101990
wants to merge
8
commits into
open-policy-agent:main
Choose a base branch
from
alex60217101990:ast/lazy-eval-array-hash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
v1/ast: Add lazy hash evaluation to Array type #8155
alex60217101990
wants to merge
8
commits into
open-policy-agent:main
from
alex60217101990:ast/lazy-eval-array-hash
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Refactor Array structure to use lazy hash evaluation instead of maintaining a separate hashs slice, reducing memory overhead by 8*N bytes per array. Key changes: - Remove hashs []int slice from Array struct - Add hashValid bool flag to track hash computation state - Implement lazy hash computation in Hash() method - Optimize Append() with incremental hash updates when hash is already computed - Update Copy(), Sorted(), Slice() to work with new hash model - Simplify rehash() to just invalidate the hash cache This optimization reduces memory allocations while maintaining performance through intelligent caching and incremental updates. Signed-off-by: alex60217101990 <alex6021710@gmail.com>
Add comprehensive benchmarks to measure the impact of lazy hash computation optimization for Array operations. Signed-off-by: alex60217101990 <alex6021710@gmail.com>
4141a17 to
cb4a46b
Compare
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
|
That race detected in tests is probably genuine. Can you have a look? Also, let's better discuss improvements like this before diving into it, I could have made you aware of the proneness to data races in this approach 💭 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Array Lazy Hash Computation Optimization
Why the changes in this PR are needed?
The current
Arrayimplementation maintains a separatehashs []intslice that stores precomputed hashes for each element. This leads to:Many arrays are created for temporary use and never participate in hash-based operations (e.g., map lookups or set operations).
What are the changes in this PR?
Refactor the
Arraystructure to use lazy hash computation:Key Changes:
Removed
hashs []intfield from theArraystructAdded
hashValid boolflag to track hash computation stateImplemented lazy evaluation in the
Hash()methodIncremental hash updates in
Array.Append()hash += newElement.Hash()Updated all related methods:
NewArray()- no longer computes hashes at creation timeCopy()- copies thehashValidflagSorted()- preserves computed hash (sorting doesn't change hash)Slice()- creates a slice with invalid hashrehash()- simplified to just invalidate the cacheset()- invalidates hash instead of recomputingBenchmark Results
Key Improvements:
Array Creation (ArrayCreation)
Append Operations (ArrayAppend)
Array Copy (ArrayCopy)
Slice Operations (ArraySlice)
Set Operations (ArraySet)
Operations Without Hash Access (ArrayNoHashAccess)
This benchmark demonstrates the real benefit of lazy evaluation - when hash is not needed:
Overall Geometric Mean:
Full benchstat Results
Detailed results are available in: