Skip to content

GH-55254: [C++][Compute] Prefix-aware early rejection for StringView equality kernels - #55425

Open
Awesome06 wants to merge 3 commits into
apache:mainfrom
Awesome06:gh-55254-stringview-prefix-early-reject
Open

Awesome06 wants to merge 3 commits into
apache:mainfrom
Awesome06:gh-55254-stringview-prefix-early-reject

Conversation

@Awesome06

@Awesome06 Awesome06 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

In #55171, direct comparison kernels were added between StringView/BinaryView and offset-based string types. Those kernels relied on applicator::ScalarBinary with ArrayIterator<BinaryViewType>, which eagerly calls util::FromBinaryView and resolves variadic payload pointers for every element regardless of match outcome.

As discussed in #55254, equality comparisons (equal, not_equal) can check the view's 4-byte inline prefix and string length before accessing payload buffers, rejecting mismatches early.

What changes are included in this PR?

  1. util::EqualBinaryViewString: Added in arrow/util/binary_view_util.h to compare a BinaryViewType::c_type with contiguous string data, checking length and 4-byte prefix before dereferencing view variadic buffers.
  2. Dedicated equality kernels: Implemented ViewCompareViewEqual and ViewCompareStringEqual in scalar_compare.cc, replacing eager iterator resolution for equal and not_equal.
  3. Register-pinned scalar comparisons: Unpacked scalar length and prefix in registers outside the loop to avoid repeated scalar lookups.
  4. Tests: Extended TestBinaryViewCompareKernel.ArrayArray and TestBinaryViewCompareKernel.ArrayScalar with cases covering prefix mismatch, out-of-line equality, null bytes, and long strings.

Performance results (vs merged #55171 baseline)

  • Inline strings (<= 12 B): ~1.47x speedup (539 µs -> 366 µs) by comparing length and inline data with 64-bit integer operations without memcmp.
  • Mismatches: 1.23x to 2.14x speedup on array comparisons (657 µs -> 307 µs for 100% mismatch, 643 µs -> 521 µs for 50% mismatch) via early prefix and length rejection.
  • Selective scalar equality: 4.67x speedup (400 µs -> 85.7 µs on 1% match) by pinning scalar prefix and length in registers.
  • Trade-off / slowdown: ~14-17% slowdown on identical out-of-line strings (> 12 B, 630 µs -> 736 µs) due to redundant prefix checks before memcmp.

Are these changes tested?

Yes, extended existing parameterized tests in scalar_compare_test.cc.

Are there any user-facing changes?

No.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Closes #55254

Copilot AI balanced review requested due to automatic review settings November 6, 2026 08:56

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has been automatically assigned in GitHub to PR creator.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has no components, please add labels for components.

@Awesome06

Awesome06 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@dmitry-chirkov-dremio Based on our prior conversation, here are the benchmark measurements evaluating the direct prefix path versus the adaptive sampling heuristic on GH-55254.

Benchmark Notes: Direct Prefix (Option A) vs. Adaptive Sampling (Option B)

We evaluated the adaptive heuristic prototyping 32 evenly spaced sampled pairs per batch (triggering the prefix path on >= 4 sample rejections for mixed types, >= 8 for view/view, and falling back to the baseline executor below 4,096 rows):

Comparative Benchmark Results:

Workload OG Baseline (Cast + Compare) Baseline (#55171) Option A (Current PR) Option B (Adaptive)
View vs String, 131k * 8 B, equal 2098.38 µs 539.12 µs (3.89x vs OG) 366.16 µs (5.73x vs OG) 492.86 µs (4.26x vs OG)
View vs String, 131k * 64 B, equal 2236.50 µs 629.59 µs (3.55x vs OG) 735.81 µs (3.04x vs OG) 638.10 µs (3.51x vs OG)
View vs String, 131k * 64 B, ~50% mismatch ~2240 µs 643.44 µs (3.48x vs OG) 521.43 µs (4.30x vs OG) 536.20 µs (4.18x vs OG)
View vs String, 131k * 64 B, 100% mismatch ~2240 µs 657.29 µs (3.41x vs OG) 307.06 µs (7.30x vs OG) 327.67 µs (6.84x vs OG)
View vs String, 131k * 64 B, prefix collision ~2240 µs 794.78 µs (2.82x vs OG) 670.92 µs (3.34x vs OG) 732.25 µs (3.06x vs OG)
View vs View, 131k * 64 B, ~50% mismatch — 612.80 µs 340.44 µs (1.80x vs #55171) 358.12 µs (1.71x vs #55171)
View vs String, 2k * 64 B, < 4,096 rows ~35 µs 10.27 µs (3.41x vs OG) 4.82 µs (7.26x vs OG) 10.25 µs (3.41x vs OG)
Array vs Scalar, 131k * 32 B, 1% match ~400 µs 400.32 µs (1.00x vs OG) 85.72 µs (4.67x vs OG) 85.72 µs (4.67x vs OG)

Takeaways:

  • Trade-off on Equal Strings: Fully matching out-of-line strings (> 12 B) see a minor ~14% regression in Option A because the prefix check is redundant before memcmp. In contrast, inlined strings (< 12 B) are 1.47x faster because they resolve with two 64-bit integer comparisons without calling memcmp.
  • Heuristic Overhead: Option B avoids the 14% regression on large matching strings, but the 32-sample probe and branch overhead offset gains on smaller batches (< 4k rows where it falls back) without improving mismatch throughput.
  • Simplicity: Option A delivers consistent 1.8x–2.1x speedups on array mismatches and 4.67x on selective scalar filters with cleaner, stateless dispatch logic.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has no components, please add labels for components.

…gView equality kernels

### Rationale for this change

In apache#55171, direct comparison kernels were added between StringView/BinaryView and offset-based string types. Those kernels relied on `applicator::ScalarBinary` with `ArrayIterator<BinaryViewType>`, which eagerly calls `util::FromBinaryView` and resolves variadic payload pointers for every element regardless of match outcome.

As discussed in apache#55254, equality comparisons (`equal`, `not_equal`) can check the view's 4-byte inline prefix and string length before accessing payload buffers, rejecting mismatches early.

### What changes are included in this PR?

1. `util::EqualBinaryViewString`: Added in `arrow/util/binary_view_util.h` to compare a `BinaryViewType::c_type` with contiguous string data. It checks length and 4-byte prefix before dereferencing view variadic buffers.
2. Dedicated equality kernels: Implemented `ViewCompareViewEqual` and `ViewCompareStringEqual` in `scalar_compare.cc`, replacing eager iterator resolution for `equal` and `not_equal`.
3. Register-pinned scalar comparisons: For array-scalar operations, unpacked scalar length and prefix are pinned in registers outside the loop, avoiding repeated scalar lookups.
4. Tests: Extended `TestBinaryViewCompareKernel.ArrayArray` and `TestBinaryViewCompareKernel.ArrayScalar` with cases covering prefix mismatch, out-of-line equality, null bytes, and long strings.

### Are these changes tested?

Yes, extended existing parameterized tests in `scalar_compare_test.cc`.

### Are there any user-facing changes?

No.

### Was AI used for this PR?

AI was used to assist with kernel registration and testing under human direction.

**PR code and description written by:**

- [ ] Human
- [x] AI

**Reviewed before submission by:**

- [x] Human
- [x] AI
- [ ] Not reviewed

* GitHub Issue: apache#55254
@Awesome06
Awesome06 force-pushed the gh-55254-stringview-prefix-early-reject branch from 3660d80 to de62926 Compare November 6, 2026 09:09
Copilot AI balanced review requested due to automatic review settings November 6, 2026 09:09

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…uality kernels

- Extract shared ViewArrayCompareBinaryScalarEqual helper to decouple View and String kernel structs.
- Optimize variadic payload comparisons by skipping the verified 4-byte prefix.
- Use stateless indexed offset traversal in ViewCompareStringEqual::ArrayArray.
- Clean up boolean expressions and add Doxygen API documentation in binary_view_util.h.
- Add asymmetric slice test coverage in scalar_compare_test.cc.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 10:12

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@dmitry-chirkov-dremio

dmitry-chirkov-dremio commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Should be add "OG baseline" to the comparison (i.e. cast + comparison) so we are discouraged by few perf "losses"?

p.s. what sort of feedback are you looking for with this PR?

@Awesome06

Awesome06 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@dmitry-chirkov-dremio I updated the earlier benchmark table with the OG baseline (cast + comparison). Also letting you know that #54668 has been merged and is the active baseline.

Regarding feedback for this PR:

  1. Confirming whether Option A is preferred over Option B, given the sampling overhead on smaller batches (< 4,096 rows) and branching complexity.
  2. Checking for any edge-case oversights or wrong assumptions in the implementation (EqualBinaryViewString, scalar register-pinning).
  3. Reviewing whether the test coverage (boundary lengths, prefix collisions, embedded nulls, asymmetric slices) is thorough enough or if additional cases should be added.

@dmitry-chirkov-dremio

Copy link
Copy Markdown
Contributor

@Awesome06 what is your personal recommendation?
Are mixed performance gains worth it with the simplicity lost of option A?

…lti-buffer tests

- Early return on BaseBinaryScalar length exceeding INT32_MAX to prevent 32-bit truncation in ViewArrayCompareBinaryScalarEqual.

- Add test coverage for inlined prefix collisions (abcdefgh vs abcdefgX) in ArrayArray and ArrayScalar compare kernels.

- Add MultiplePayloadBuffers test case verifying cross-buffer comparisons and buffer index dereferencing.
Copilot AI balanced review requested due to automatic review settings November 13, 2026 05:54

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Awesome06

Copy link
Copy Markdown
Contributor Author

@dmitry-chirkov-dremio Yes, I think it is well worth it.

Baseline was simple only because it used the generic executor and ignored the 4-byte prefix. In real workloads (selective filters), 90%+ of rows are mismatches or short inline strings, where Option A is 4.7x faster on scalars, 2x on mismatches, and 1.5x on inline strings.

The ~14% drop only hits when 100% of rows are identical >12 B strings, and even there it remains 3x faster than the OG baseline. Trading that corner-case for 4.7x on selective filters fulfills why we use StringView, without needing runtime heuristics like Option B.

@dmitry-chirkov-dremio

dmitry-chirkov-dremio commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Sorry, I'm slow today. I got lost in options A and B.
OG is baseline, Option A is already merged (already great wins) and this PR is option B, correct?

If yes, could you clarify the wins/losses of A vs B again?

In your earlier comment you seem to have implied you prefer A yourself, no? Or is that the impression you got from me?

@Awesome06

Awesome06 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

What Each Option Means


Why Option A is the Right Path Forward

@dmitry-chirkov-dremio dmitry-chirkov-dremio 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.

Thanks, clear now. I lean toward A. Please summarize the gains and the slowdown against the merged baseline in the PR description.

@Awesome06

Copy link
Copy Markdown
Contributor Author

Updated the PR description with the benchmark comparison and trade-off summary against the #55171 baseline.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has no components, please add labels for components.

1 similar comment
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has no components, please add labels for components.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55254 has no components, please add labels for components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Compute] Investigate prefix-aware early rejection for StringView equality kernels

4 participants