Share word-at-a-time primitives instead of optimizing each call site separately

Aperta
#12,467 10 commenti 3 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@mbutrovich ci sta già lavorando.

Dal 25/11/2026.

  • #12470 di @mbutrovich — aperta

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

This is a design proposal, not a starter task: the open question is whether arrow-buffer is the right home and whether parquet may take a required dependency on it. Read arrow-buffer/src/util/bit_util.rs, bit_chunk_iterator.rs and parquet/src/util/prefix.rs to see the existing primitives. Done means a maintainer settles the placement question; the first concrete step is a separate PR per primitive with benchmarks.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Is your feature request related to a problem or challenge?

Many recent performance PRs make the same kind of change at one call site: process bytes or bits a word at a time instead of a byte at a time or through a libc call. Each PR tunes, tests, and benchmarks its own copy. This has three costs.

The tunings conflict. common_prefix_length in parquet (source, from #10549) compares 32-byte blocks inline because an out-of-line bcmp at 64 bytes measured slower. #12460 compares 8-byte chunks and hands values with a common length above 64 bytes to memcmp because memcmp measured faster there. Both run in the same file of the parquet arrow writer.

Gains don't reach the other copies. #11818 sped up UnalignedBitChunk::count_ones on aarch64. parquet has two count_set_bits functions of its own. The one in the arrow reader (source) calls UnalignedBitChunk::count_ones and got the gain. The one used to skip bit-packed definition levels (source) builds without the arrow feature, where arrow-buffer isn't available, so it has its own loop and didn't get the gain. compare_greater in the parquet column writer (source) still compares slices with a > b while #12460 speeds up the arrow writer's copy of the same comparison.

Work gets duplicated. #12024 and #12042 wrote the same multiply-based bool packing for BooleanBufferBuilder::append_slice at the same time. arrow-buffer has two identical read_u64 functions (bit_util.rs, bit_chunk_iterator.rs). byte_view_key in arrow-ord rank (source) and is_greater_bytes in #12460 (source) each build their own order-preserving integer key from a byte prefix.

Describe the solution you'd like

Give these primitives one home in arrow_buffer::util, next to bit_util and bit_chunk_iterator. Each primitive has one implementation, a test against a naive reference, and benchmark cases for both well-predicted and random input. Add a paragraph to the Performance Improvements section of CONTRIBUTING.md: before adding a word-at-a-time loop at a call site, check arrow_buffer::util and extend or add a primitive there. A PR that tunes a primitive shows results on both x86-64 and aarch64, because every caller inherits the tuning.

For byte comparison, the copies above share a structure with two parts that need different tuning. Most values that differ do so in their first word, so that word should be compared inline in the caller's loop. Values with a long shared prefix need a scan tuned for throughput, kept out of line so it doesn't bloat every caller. #12460 found both effects: inlining its chunk loop cost 2 to 4 points on random strings, and its out-of-line function had to stay a leaf to keep its gain. That suggests three functions:

  1. prefix_key(s: &[u8]) -> u64 returns the first 8 bytes as a big-endian integer, zero-padded when s is shorter. prefix_key(a) < prefix_key(b) implies a < b. Equal keys say nothing. A u128 variant covers 16-byte keys like byte_view_key. Only the u128 variant has a caller outside cmp_bytes so far, so the u64 one could stay private until it gets one.
  2. common_prefix_len(a: &[u8], b: &[u8]) -> usize is the out-of-line scan. The choice between inline blocks and memcmp, and its bound per target, gets made here once. The DELTA_BYTE_ARRAY encoders, which inline today's scan, would make one call per value.
  3. cmp_bytes(a: &[u8], b: &[u8]) -> Ordering compares the first 8 bytes inline when both values have that many. Shorter values and ties go to an out-of-line function that compares the prefix keys, then scans with common_prefix_len and compares the byte at the mismatch or the lengths. Its result matches a.cmp(b) for every input.

A prototype showed that this split has to be forced with attributes, because #[inline] alone doesn't hold across crates. With the default release profile, LLVM declined to inline a #[inline] cmp_bytes into parquet's ByteArrayEncoder::write_gather, which inlines the statistics loop of every array type, and kept 24 calls to an out-of-line copy. Marking all of cmp_bytes #[inline(always)] grew write_gather by 18%. Marking only the first-word comparison #[inline(always)], with the rest #[inline(never)], kept it at its current size. With every inlining decision set by an attribute, the split shouldn't depend on each consumer's LTO and codegen-unit settings, though I have checked it only without LTO. The out-of-line scan also lost its leaf status in the prototype to a bounds check whose panic path needed a stack frame. Details like these are easy to break at one call site, which is another reason to review them in one place.

For bitmaps, UnalignedBitChunk and BitChunks already exist. Once parquet can depend on the shared primitives without the arrow feature, it can keep one count_set_bits that calls UnalignedBitChunk::count_ones. arrow-buffer can keep one read_u64.

Each move would be its own PR with benchmark results.

Describe alternatives you've considered

Keeping the current approach costs nothing up front, and each PR stays small. The costs above grow with every new copy.

A generic chunked-scan abstraction, such as a const-generic block width with a closure per block, would cover more cases with one type. It would leave the inlining decisions above to each caller's instantiation, so each caller's generated code would need its own review, and it would be a larger public API to keep stable.

A new crate for these primitives would keep them out of arrow-buffer's API. It would be one more crate to version and release, and parquet would still need it without the arrow feature.

Each crate could keep pub(crate) copies and consolidate within the crate. That avoids new public API, but parquet, arrow-ord, and arrow-buffer would still tune separately.

Additional context

Anything shared between crates has to be pub, so each primitive becomes permanent API under the stability policy. That argues for a few concrete functions with results defined for every input.

parquet uses these primitives in code that builds without the arrow feature, where arrow-buffer is optional. arrow-buffer depends only on bytes, num-bigint, num-traits, and half (Cargo.toml), and parquet already depends on all four without the feature (Cargo.toml), so a required dependency would add no third-party crates.

Is arrow-buffer the right home, and is a required arrow-buffer dependency acceptable for parquet?

cc @alamb @Jefffrey @etseidl @Rich-T-kid @neilconway @adriangb @YUZHEthefool @viirya @waynexia @mightsleep @shanielh

Lingua principale
Rust
Stelle
3.7k
Fork
1.5k
Merge medio
2g 16h
PR unite (30g)
183

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/arrow-rs

Tutte le issue di apache/arrow-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.