Skip to content

Reserve room for a whole vector in VecWriter with simd - #387

Open
emschwartz wants to merge 3 commits into
RoaringBitmap:mainfrom
emschwartz:vecwriter-simd-reserve
Open

emschwartz wants to merge 3 commits into
RoaringBitmap:mainfrom
emschwartz:vecwriter-simd-reserve

Conversation

@emschwartz

Copy link
Copy Markdown

Stacked on #385: only the last commit is new.

With simd, VecWriter::visit_vector appends all eight lanes and then truncates to the ones that matched. A vector write within eight slots of the reserved capacity therefore grows the Vec even though the output fits, and into_inner does not shrink it. Intersecting 500 values with a superset of 4000 reserves 500 and returns a Vec with capacity 1000.

Reserving eight extra slots under simd avoids the regrow. The added test fails without it. This is about memory, not speed: over 3 runs on the machines from #385, timings were within 1% of that PR.

Authored with Claude (claude-opus-5-5).

emschwartz and others added 3 commits October 11, 2026 04:17
With the `simd` feature, `ArrayStore -= &ArrayStore` reserved
`min(lhs.len(), rhs.len())` for its output, but a difference can hold
up to `lhs.len()` values. The Vec grew to cover it, so results were
correct. Reserve `lhs.len()`, as `Sub` already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every caller reserves an upper bound for its output, so the `push` in
`visit_scalar` never reallocates, but the compiler cannot see that. The
scalar merge loops for `&`, `|`, `^` and `-` therefore carry a call to
`grow_one` and reload the Vec's fields from the stack on each write.

Asserting `len < capacity` before the push removes the call and lets
the loops keep their state in registers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`visit_vector` appends all eight lanes and then truncates to the ones
that matched, so a write near the end of the reserved space grows the
Vec even though the output fits. Intersecting 500 values with a
superset of 4000 reserves 500 and returns a Vec with capacity 1000,
and `into_inner` does not shrink it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

1 participant