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

Đang mở
#12,467 10 bình luận 3 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@mbutrovich đang làm issue này rồi.

Từ ngày 25/11/2026.

  • #12470 của @mbutrovich — đang mở

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
data-engineering

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Rust
Star
3.7k
Fork
1.5k
Merge trung bình
2 ngày 16 giờ
Pull request đã merge (30 ngày)
183

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/arrow-rs

Tất cả issue của apache/arrow-rs

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.