mbutrovich opened a new issue, #12467: URL: https://github.com/apache/arrow-rs/issues/12467
### 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](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/parquet/src/util/prefix.rs#L15-L50), 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 a second `count_set_bits` ([source](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/parquet/src/util/bit_util.rs#L796-L822)) with its own loop, used to skip bit-packed definition levels, and it didn't get the gain. `compare_greater` in the parquet column writer ([source](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/parquet/src/column/writer/mod.rs#L1949-L1981)) 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](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/arrow-buffer/src/util/bit_util.rs#L341-L348), [bit_chunk_iterator.rs](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/arrow-buffer/src/util/bit_chunk_iterator.rs#L216-L222)). `byte_view_key` in arrow-ord `rank` ([source](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/arrow-ord/src/rank.rs#L255-L266)) and `GenericByteViewArray::inline_key_fast` ([source](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/arrow-array/src/array/byte_view_array.rs#L859-L862)) 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](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/CONTRIBUTING.md?plain=1#L306-L345): 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`. 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. 3. `cmp_bytes(a: &[u8], b: &[u8]) -> Ordering` compares the prefix keys and, on a tie, calls `common_prefix_len` and compares the byte at the mismatch or the lengths. Its result matches `a.cmp(b)` for every input. For bitmaps, `UnalignedBitChunk` and `BitChunks` already exist. parquet's second `count_set_bits` can call `UnalignedBitChunk::count_ones`, and `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](https://github.com/apache/arrow-rs/blob/df4dc3ede59bd43b5f71847231212381d068bb72/CONTRIBUTING.md?plain=1#L208-L213). 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](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/parquet/Cargo.toml#L27). `arrow-buffer` depends only on `bytes`, `num-bigint`, `num-traits`, and `half` ([Cargo.toml](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/arrow-buffer/Cargo.toml#L33-L36)), and parquet already depends on all four without the feature ([Cargo.toml](https://github.com/apache/arrow-rs/blob/8befa3cd899afea17b1969f0f4fd4491e3f81439/parquet/Cargo.toml#L39-L60)), 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 -- 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]
