Skip to content

perf: optimize Intersect - #1027

Open
mai7star wants to merge 3 commits into
samber:masterfrom
mai7star:mai7star-intersect-performance
Open

mai7star wants to merge 3 commits into
samber:masterfrom
mai7star:mai7star-intersect-performance

Conversation

@mai7star

@mai7star mai7star commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Use the smallest input slice to seed the intersection lookup map, reducing memory use for uneven inputs.
  • Use Uniq for single-list intersections and optimize small two-list intersections with a linear scan.
  • Raise the small-intersection cutoff to 256 and add regression tests and benchmarks for these paths.

Benchmark results

Compared parent 5c6ddcb with this PR (6fd9ecf) on an Apple M4 / darwin arm64. Each result is the median of 5 runs (-benchtime=300ms).

Scenario Before After Change Allocations (before → after)
One 1,000-item list 22.24 µs/op 11.38 µs/op 48.8% faster 6 → 6 (45,136 B → 45,136 B)
Two 10-item lists (full overlap) 268.9 ns/op 59.0 ns/op 78.1% faster 4 → 1 (408 B → 80 B)
4-item list first, 1,000-item list second 7.31 µs/op 3.19 µs/op 56.4% faster 6 → 1 (45,136 B → 32 B)
1,000-item list first, 4-item list second 1.93 µs/op 1.78 µs/op 7.6% faster 1 → 1 (32 B → 32 B)

The comparison used the same temporary benchmark harness on both revisions: go test ./benchmark -run '^$' -bench '^BenchmarkIntersectPREval$' -benchmem -benchtime=300ms -count=5.

Validation

  • go test ./... — passed

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.86%. Comparing base (357bcef) to head (a485230).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1027      +/-   ##
==========================================
- Coverage   98.87%   98.86%   -0.02%     
==========================================
  Files          32       32              
  Lines        4728     4741      +13     
==========================================
+ Hits         4675     4687      +12     
- Misses         53       54       +1     
Flag Coverage Δ
unittests 98.86% <100.00%> (-0.02%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@agammann agammann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a behavior change in the single-list path at head 6fd9ecf with a focused Go test. The inline comment has the input, observed output, and suggested regression coverage.

Comment thread intersect.go Outdated
case 0:
return Slice{}
case 1:
return Uniq(lists[0])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uniq retains NaN here, while the previous map-based Intersect omits it because NaN never compares equal to itself. On this head, Intersect([]float64{math.NaN(), 1}) returns [NaN 1]; the base returns [1]. Could this shortcut preserve the existing behavior and add a NaN regression test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I checked the current head: the single-list path uses intersectLarge again, and the new NaN regression test covers the behavior I raised. That specific concern is addressed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mai7star
mai7star requested a review from agammann November 6, 2026 04:24

@agammann agammann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Targeted re-review: the single-list NaN regression is fixed at the current head, as noted in the inline thread. I have not assessed the separate failing checks or the rest of the performance change.

Copy link
Copy Markdown
Contributor

I checked the CI failures on current head a485230: the Tests workflow passed. The Lint workflow reports unused //nolint:revive directives across existing files including benchmark/it_slice_bench_test.go and concurrency.go; the Security scan's Trivy jobs stop because .trivyignore is missing, and the SARIF upload then has no file to upload. These logs suggest workflow/tooling issues outside the changed files, although I have not verified the base branch or rerun the checks. The Trivy failure means that scan did not complete. (AI-assisted CI triage.)

This branch has not been deployed

No deployments
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