perf(ast): optimize Set.Intersect and Set.Diff#8167
Merged
sspaink merged 2 commits intoopen-policy-agent:mainfrom Jan 6, 2026
Merged
perf(ast): optimize Set.Intersect and Set.Diff#8167sspaink merged 2 commits intoopen-policy-agent:mainfrom
sspaink merged 2 commits intoopen-policy-agent:mainfrom
Conversation
Build result sets directly using newset() and insert() instead of collecting terms into an intermediate slice and eventually passing to NewSet(). This eliminates: - an intermediate slice allocation - redundant hash computations when NewSet() re-inserts all terms - unnecessary sorting via sortedKeys() since membership checks don't require order Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
anderseknert
approved these changes
Jan 2, 2026
Member
anderseknert
left a comment
There was a problem hiding this comment.
Looks good to me! Thanks, Ville 👍
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
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.
Why the changes in this PR are needed?
Currently the
Set.IntersectandSet.Diffmethods have two inefficiencies:This PR optimises both, showing 10-20% less memory usage in these operations.
What are the changes in this PR?
Build result sets directly using
newset()andinsert()instead of collecting terms into an intermediate slice and eventually passing toNewSet(). This eliminates:NewSet()re-inserts all termssortedKeys()since membership checks don't require orderNotes to assist PR review:
When users access the result via
Iter(),String(),Foreach(), etc., these methods callsortedKeys()which sorts in-place on first access. The old code sorted during construction and on access.Benchmarked with:
Benchstat output:
Further comments:
The
Union()method has a similar issue, but I'll introduce a proposal for it in a separate PR.