Skip to content

Avoid bounds checks in array intersection loops - #551

Merged
lemire merged 2 commits into
RoaringBitmap:masterfrom
perfloop:perfloop-pr-open-jkhaz4jeyf
Aug 19, 2026
Merged

Avoid bounds checks in array intersection loops#551
lemire merged 2 commits into
RoaringBitmap:masterfrom
perfloop:perfloop-pr-open-jkhaz4jeyf

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Description

Removes repeated input bounds checks from the local two-pointer array intersection loops.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement
  • Code refactoring
  • Documentation update
  • Test improvements
  • Build/CI changes

Changes Made

What was changed?

  • Added public benchmarks for array-container And and AndCardinality.
  • Advanced suffix slices in localintersect2by2 and localintersect2by2Cardinality.

Why was it changed?

  • Reading the current suffix element at index zero after a non-empty guard lets the compiler prove the input reads are in bounds.

How was it changed?

  • Guard the final element before advancing, then read the next suffix element at index zero. Galloping intersections and other container types are unchanged.

Testing

go test ./...

Checks: 1 passed.

Formatting

gofmt -w setutil.go setutil_test.go

Fuzzing

Not run; the change only restructures the existing sorted-slice intersection loop.

Performance Impact

The benchmarks use public operations on four aligned 4096-element array containers with alternating half-overlap. They cover the comparable-size array path, not galloping or non-array intersections.

Workload: roaring.And across four aligned 4096-element array containers with alternating half-overlap

Metric Before After Change
ns/op 41284 32717 20.8% lower

Workload: Bitmap.AndCardinality across four aligned 4096-element array containers with alternating half-overlap

Metric Before After Change
ns/op 32750 20425 37.6% lower

Running Benchmarks

go test -bench='^BenchmarkArrayIntersection$' -benchmem -run='^$'

go test -bench='^BenchmarkArrayIntersectionCardinality$' -benchmem -run='^$'

Performance Analysis

The compiler retains the output-buffer check but removes input bounds checks from the hot loops.

Breaking Changes

None.

Related Issues

None.

Additional Notes

None.


Generated by Perfloop. Measurements and checks.

@lemire

lemire commented Aug 19, 2026

Copy link
Copy Markdown
Member

Really impressive.

@lemire
lemire merged commit e9e1ecc into RoaringBitmap:master Aug 19, 2026
8 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