refactor: improve lo[it].Intersect[By] readability and performance#756
Merged
samber merged 2 commits intosamber:masterfrom Jan 8, 2026
Merged
refactor: improve lo[it].Intersect[By] readability and performance#756samber merged 2 commits intosamber:masterfrom
samber merged 2 commits intosamber:masterfrom
Conversation
Preallocate seen map capacity. Extract first and last list handling out of the main loop. Remove redundant map traversal by combining the reset and delete passes into a single loop. Values are now initialized to false and toggled to true when found, allowing the cleanup pass to both reset (true→false) and delete in one iteration. Move len(seen) == 0 check into loop condition, allowing early exit before processing next list when intersection becomes empty (skips redundant first iteration).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #756 +/- ##
==========================================
+ Coverage 94.04% 94.20% +0.15%
==========================================
Files 18 18
Lines 2840 2880 +40
==========================================
+ Hits 2671 2713 +42
+ Misses 152 151 -1
+ Partials 17 16 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
if len(lists) == 1 {
return lists[0]
}Also, it seems that this early return case is harmful.
|
Owner
|
Thanks @d-enk for this new contrib. Yes, your implem seems better and leverage CPU cache. Can you make the last suggested fix ? |
The special case `if len(lists) == 1 { return lists[0] }` was buggy:
- Returned the original slice instead of a new one
- Did not deduplicate elements
Removing it allows the general algorithm to handle single lists
correctly by creating a new slice with unique elements.
cc44610 to
1d63e3c
Compare
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.
Preallocate seen map capacity.
Extract first and last list handling out of the main loop.
Remove redundant map traversal by combining the reset and delete passes into a single loop. Values are now initialized to false and toggled to true when found, allowing the cleanup pass to both reset (true→false) and delete in one iteration.
Move len(seen) == 0 check into loop condition, allowing early exit before processing next list when intersection becomes empty (skips redundant first iteration).