Skip to content

refactor: improve lo[it].Intersect[By] readability and performance#756

Merged
samber merged 2 commits intosamber:masterfrom
d-enk:refactor-intersect
Jan 8, 2026
Merged

refactor: improve lo[it].Intersect[By] readability and performance#756
samber merged 2 commits intosamber:masterfrom
d-enk:refactor-intersect

Conversation

@d-enk
Copy link
Contributor

@d-enk d-enk commented Dec 29, 2025

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).

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).
@d-enk
Copy link
Contributor Author

d-enk commented Dec 29, 2025

@codecov
Copy link

codecov bot commented Dec 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.20%. Comparing base (96bff97) to head (1d63e3c).
⚠️ Report is 5 commits behind head on master.

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     
Flag Coverage Δ
unittests 94.20% <100.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@d-enk
Copy link
Contributor Author

d-enk commented Dec 29, 2025

if len(lists) == 1 {
	return lists[0]
}

Also, it seems that this early return case is harmful.

  • not a copy
  • not unique elements

@samber
Copy link
Owner

samber commented Jan 8, 2026

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.
@d-enk d-enk force-pushed the refactor-intersect branch from cc44610 to 1d63e3c Compare January 8, 2026 15:03
@samber samber merged commit a2553e5 into samber:master Jan 8, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants