Speed up eq/neq of a byte-view array against a short scalar - #10689
Open
giladkl wants to merge 2 commits into
Open
Speed up eq/neq of a byte-view array against a short scalar #10689giladkl wants to merge 2 commits into
giladkl wants to merge 2 commits into
Conversation
The byte-view comparison benchmarks build eight million rows, which is 134MB of views alone. That is far past cache, so those kernels are bound by memory bandwidth and a change to the per-row comparison is mostly hidden: the same comparison measured cache-resident separates by more than twice as much. Add a `stringview_scalar_eq` group that runs the same comparison at 65,536, one million and eight million rows, so a change can be read across the range rather than at one point on it. The existing benchmarks are left alone.
A constant of four bytes or fewer is described entirely by a view's low
64 bits, which hold the length and the first four bytes. Masking those
and comparing them against the constant, resolved once up front, settles
a row with one narrow integer compare over a flat slice, in place of the
generic path's branchy walk over the full 128-bit view reached through a
closure over an `(array, index)` pair. The result is branch-free and
vectorizes.
A wider constant needs the whole view, and comparing that measured
slower than the generic path's early exit on a length mismatch, so those
keep it.
`stringview_scalar_eq`, against main:
rows main this change
65,536 111.4us 47.7us -57.2%
1,048,576 2.04ms 983.2us -51.7%
8,388,608 19.71ms 14.85ms -24.7%
The win narrows as the views outgrow cache and the kernel becomes bound
by memory bandwidth rather than by the comparison.
alamb
reviewed
Aug 15, 2026
|
|
||
| // eq scalar benchmarks across sizes: 16 bytes of view a row, so the largest is bandwidth-bound | ||
|
|
||
| let mut group = c.benchmark_group("stringview_scalar_eq"); |
Contributor
There was a problem hiding this comment.
Can you please add this benchmark as a separate PR so I can use our automated benchmarking scripts?
alamb
reviewed
Aug 15, 2026
|
|
||
| /// Compares every value of a byte-view array against an inlined constant | ||
| /// | ||
| /// `None` for any other shape, which the generic path below then handles. |
| /// Longest constant whose length and bytes both fit a view's low 64 bits | ||
| const MAX_LOW_HALF_LEN: u32 = 4; | ||
|
|
||
| /// Compares every value of a byte-view array against an inlined constant |
Contributor
There was a problem hiding this comment.
Maybe point out this attempts a special case with a shot constant
alamb
reviewed
Aug 15, 2026
| ree: r_ree_info.as_ref().map(|(_, info)| info), | ||
| }; | ||
|
|
||
| // Equality against an inlined constant is a scan of fixed-width integers |
Contributor
There was a problem hiding this comment.
maybe you could also mention this is a special case for comparing short strings with the inlined prefix of a view array
Contributor
|
I reviewed this code and it looks good and correct to me. I would just like to get some confirmation on the performance from our benchmark runner and then I think we cna merge it in. Thank you @giladkl |
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.
Which issue does this PR close?
Rationale for this change
col = 'x' / col <> '' over a Utf8View or BinaryView column is one of the hottest kernels in an analytical scan, and today the compare walks the full 128-bit view through a sequence of branches even when the scalar is small.
For a short constant almost none of that is needed. A constant of four bytes or fewer is described entirely by a view's low 64 bits, which hold the length and the first four bytes, so masking those and comparing them against the constant resolved once up front settles a row with a single narrow integer compare over the flat &[u128] view slice. That loop is branch-free and vectorizes.
What changes are included in this PR?
Constants wider than four bytes are deliberately left alone. They need the whole 128-bit view, and comparing that measured slower than the generic path's early exit on a length mismatch (a six-byte constant regressed 11.8%), so the cap is a measured limit rather than an arbitrary one.
Are these changes tested?
Yes. Three tests are added alongside the existing byte-view comparison tests:
The existing arrow-ord suite (272 tests) passes unchanged.
Benchmark evidence, cargo bench --bench comparison_kernels -- stringview_scalar_eq on an Intel Xeon @ 2.80GHz (66 MiB L3):
┌───────────┬─────────┬─────────┬─────────┬────────┐
│ rows │ views │ before │ after │ change │
├───────────┼─────────┼─────────┼─────────┼────────┤
│ 65,536 │ 1 MiB │ 111.4us │ 47.7us │ -57.2% │
├───────────┼─────────┼─────────┼─────────┼────────┤
│ 1,048,576 │ 16 MiB │ 2.04ms │ 983.2us │ -51.7% │
├───────────┼─────────┼─────────┼─────────┼────────┤
│ 8,388,608 │ 128 MiB │ 19.71ms │ 14.85ms │ -24.7% │
└───────────┴─────────┴─────────┴─────────┴────────┘
The win narrows at the largest size, where the views no longer fit in cache and the kernel becomes bound by memory bandwidth rather than by the comparison.
Are there any user-facing changes?
Nope :)