bvolpato opened a new pull request, #55731:
URL: https://github.com/apache/arrow/pull/55731

   ### Rationale for this change
   
   `PrimitiveFilterImpl::Exec()` spends most of its time on branch 
mispredictions and bit-at-a-time manipulation at medium selectivity (~50%) and 
whenever nulls are present:
   
   - On the no-nulls path, `VisitSetBitRunsVoid` emits very short 1 to 2 
element runs at ~50% selectivity, leading to tens of thousands of tiny `memcpy` 
calls and mispredicted run loops.
   - Whenever data or filter nulls are present, the kernel falls back to 
per-element `GetBit` branches and per-bit read-modify-write `SetBit` calls.
   
   ### What changes are included in this PR?
   
   - Process filter and validity bitmaps 64 bits at a time in 
`PrimitiveFilterImpl` via `BitmapWordReader<uint64_t>`.
   - Use branchless compaction (`CompactTypedBlock64`) for 1, 2, 4, 8, and 
16-byte fixed-width primitives when a 64-bit word has more than 8 set bits and 
`out_position_ + 64 <= out_length_` (guaranteeing all speculative writes stay 
within the allocated output buffer, and any trailing speculative slot is 
overwritten by subsequent selected elements). Sparse words (`popcount <= 8`), 
other byte widths, and final tail blocks use `std::countr_zero` iteration.
   - Keep `VisitSetBitRunsVoid` for the no-nulls path when selectivity exceeds 
93.75% (`out_length_ > values_length_ - (values_length_ >> 4)`) so near-100% 
bulk copies stay untouched, and coalesce consecutive all-1 words in the word 
loop into bulk `CopyBitmap` and `memcpy` segments.
   - Compact boolean values and output validity bits in 64-bit registers 
(`CompactBitmapWord` and exact byte-bounded `WriteBits64`), pre-fill 
`out_is_valid_` once up front when `DROP` produces zero output nulls, and 
remove the unused `DropNullCounter`.
   
   Benchmarks on Linux x86_64 (AMD Ryzen 9 9950X3D, GCC 13.3.0, 
`RelWithDebInfo`, pinned to one core, median of 5 repetitions, back-to-back A/B 
against `main`):
   
   | Benchmark (`1048576` bytes) | Before | After | Speedup |
   | :--- | ---: | ---: | ---: |
   | `FilterInt64FilterWithNulls/1048576/1` (5% mask nulls, 50% sel) | 443.77 
us | 48.62 us | 9.13x |
   | `FilterInt64FilterNoNulls/1048576/4` (0.1% data nulls, 50% sel) | 431.66 
us | 51.27 us | 8.42x |
   | `FilterInt64FilterWithNulls/1048576/4` (0.1% data, 5% mask, 50% sel) | 
449.35 us | 58.70 us | 7.65x |
   | `FilterInt64FilterNoNulls/1048576/7` (1% data nulls, 50% sel) | 443.97 us 
| 73.54 us | 6.04x |
   | `FilterInt64FilterWithNulls/1048576/7` (1% data, 5% mask, 50% sel) | 
477.99 us | 79.13 us | 6.04x |
   | `FilterInt64FilterWithNulls/1048576/10` (10% data, 5% mask, 50% sel) | 
468.74 us | 95.45 us | 4.91x |
   | `FilterInt64FilterNoNulls/1048576/10` (10% data nulls, 50% sel) | 444.31 
us | 92.16 us | 4.82x |
   | `FilterInt64FilterNoNulls/1048576/1` (0% nulls, 50% sel) | 167.55 us | 
40.36 us | 4.15x |
   | `FilterInt64FilterWithNulls/1048576/2` (5% mask nulls, 1% sel) | 44.82 us 
| 11.99 us | 3.74x |
   | `FilterInt64FilterWithNulls/1048576/0` (5% mask nulls, 99.9% sel) | 164.57 
us | 48.95 us | 3.36x |
   | `FilterInt64FilterNoNulls/1048576/2` (0% nulls, 1% sel) | 6.20 us | 3.45 
us | 1.80x |
   | `FilterInt64FilterNoNulls/1048576/0` (0% nulls, 99.9% sel) | 16.26 us | 
16.19 us | 1.00x |
   
   `FilterFixedSizeBinary` shows similar gains at 50% selectivity with mask 
nulls (9.19x on 8-byte values and 4.85x on 9-byte arbitrary-width values).
   
   ### Are these changes tested?
   
   Yes. Added a filter test in `vector_selection_test.cc` covering `boolean`, 
`int8`, `int16`, `int32`, `int64`, `decimal128`, `decimal256`, and 
`fixed_size_binary(3)` across boundary lengths up to 1025, sliced data and 
sliced filter masks, data and filter nulls, selectivities from 0% to 100%, and 
both `DROP` and `EMIT_NULL`, comparing every output element and null bit 
against a scalar reference and checking `ValidateFull()`. Verified locally with 
the vector selection unit test suite, `clang` warning checks, and `pre-commit` 
C++ format/lint checks on Linux x86_64 (relying on CI for ASAN/UBSAN, ARM64, 
and MSVC).
   
   ### Are there any user-facing changes?
   
   No.
   
   ### Was AI used for this PR?
   
   In accordance to the [AI generation 
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
 please disclose below whether and how AI was used in this PR.
   
   AI coding assistance was used to draft the word-level compaction helpers and 
edge-case tests; I reviewed the bounds invariants, code, and benchmarks before 
opening the PR.
   
   **PR code and description written by:**
   
   - [x] Human
   - [x] AI
   
   **Reviewed before submission by:**
   
   - [x] Human
   - [ ] AI
   - [ ] Not reviewed
   
   * GitHub Issue: (filled in automatically from the PR title)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to