Skip to content

perf(parquet): compare byte-array statistics 8 bytes at a time - #12460

Closed
shanielh wants to merge 1 commit into
apache:mainfrom
shanielh:byte-array-stats-u64-compare
Closed

shanielh wants to merge 1 commit into
apache:mainfrom
shanielh:byte-array-stats-u64-compare

Conversation

@shanielh

@shanielh shanielh commented Nov 25, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

This PR makes each byte-array comparison in the per-row min/max statistics cheaper. #12401 makes dictionary arrays run fewer of those comparisons: it folds each dictionary value into the page statistics once per page instead of once per row. The two changes touch different functions (#12401 changes the callers of is_greater, this PR changes is_greater itself), so they merge in either order. Their gains add up: see "Combined with #12401" below.

Rationale for this change

For each row of a Utf8/Binary/LargeUtf8/LargeBinary/view column, or of a dictionary of those, the arrow writer updates page statistics with two slice comparisons: min > val and val > max. Each one is an out-of-line memcmp call followed by a length comparison. On random strings this is most of the roughly 97 instructions per row that statistics cost. Most values already differ in their first 8 bytes, and a single integer comparison can order them.

What changes are included in this PR?

is_greater in parquet/src/arrow/arrow_writer/byte_array.rs (non-decimal branch) now calls is_greater_bytes, which handles three cases:

  1. Both values have at least 8 bytes: the first 8 bytes are compared inline as big-endian u64s. When they differ, which is most rows of most columns, no call is made.
  2. First 8 bytes equal, common length at most 64 bytes: an out-of-line leaf function compares 8-byte chunks (as_chunks), then one 8-byte load ending at the shorter value's end. That load starts with bytes already compared equal, so no shift is needed. When the common length is longer than 64 bytes, memcmp is used.
  3. A value shorter than 8 bytes: a u64 key is built from overlapping 4-byte loads (or 3 bytes for lengths 1 to 3), followed by a length comparison.

There are no public API changes. The new functions (is_greater_bytes, is_greater_after_prefix, short_key) are private. The column writer's compare_greater, used by SerializedFileWriter and the arrow writer's FixedSizeBinary path, is unchanged. The same change measured −10% instructions on 16-byte FixedSizeBinary there, which I'd leave to a follow-up.

Are these changes tested?

Yes. A new unit test, is_greater_bytes_matches_slice_order, compares is_greater_bytes(a, b) with a > b for every pair from about 1,900 values. Those values cover:

  • lengths 0 to 20, 63, 64, 65 and 72;
  • equal prefixes of different lengths;
  • values shorter than 8 bytes;
  • one byte at every position set to 0x00, 0x7f, 0x80 and 0xff.

The existing arrow_writer statistics tests and test_dictionary_array_writes_same_file_as_its_values cover the written files.

Benchmarks

Targeted criterion benchmarks in parquet/benches/arrow_writer.rs:

  • Random strings (poorly predicted): string, string_dictionary, string_dictionary_low_cardinality_{20,100,400} and string_dictionary_1pct_cardinality.
  • Constant-length sequential values (well predicted): short_string_non_null.
  • Long shared prefixes (the inputs that can get slower): large_string_non_null, small_string_shared_prefix and small_string_partial_prefix.
cargo bench -p parquet --bench arrow_writer -- 'string'

These results are instructions and cycles retired per 1M-row write (10 writes minus 0 writes, median of 5 rounds), from a fixed-work program that builds the same batches as those benchmarks and writes them with default properties (dictionary enabled), on an Apple M5 Pro. All four builds in this description (main, this PR, #12401, #12401 + this PR) were measured in one run, alternating builds within each input. Instruction counts repeat within ±0.5% between rounds on most inputs. On inputs with about a million distinct values they move by a few points between runs, because these inputs overflow the dictionary and the interner hashes with a different random seed each run: the 8-byte shared-prefix input has measured −4% to −10% across runs. Instruction counts on the two small_string_* inputs, about 15M instructions per write, spread ±3% between rounds. The machine had moderate background load during the run, so read cycles as roughly ±10 points.

input main instr (M) this PR instr (M) instr cycles
string_dictionary 329.4 291.6 −11.5% −18.2%
string_dictionary_low_cardinality_20 145.9 98.0 −32.8% −56.8%
string_dictionary_low_cardinality_100 141.4 95.4 −32.6% −51.8%
string_dictionary_low_cardinality_400 138.9 94.8 −31.7% −63.4%
dictionary of 400 values, random keys 139.7 94.9 −32.1% −64.3%
dictionary of 1024 values, random keys 142.9 96.3 −32.6% −67.5%
dictionary of 1024 values, one key per 1024 rows 156.8 150.0 −4.3% −11.7%
string_dictionary_1pct_cardinality (value_00000123, 14 bytes) 163.6 152.3 −6.9% −25.1%
dictionary of 1M random values, random keys 202.4 154.3 −23.7% −55.4%
dictionary of 2M random values, random keys 201.8 154.4 −23.5% −50.9%
Utf8, 100 random values round-robin 296.0 242.8 −18.0% −36.0%
Utf8, random 1 to 7 bytes 224.6 201.3 −10.4% −13.5%
Utf8, 8-byte shared prefix + 4 to 11 random bytes 220.6 205.7 −6.7% −12.6%
Utf8, 32-byte shared prefix + 4 to 11 random bytes 281.7 266.8 −5.3% −3.8%
short_string_non_null 194.0 143.8 −25.9% −37.8%
large_string_non_null (identical 256 KiB values) 609.4 609.9 +0.1% −1.0%
large_string_shared_prefix (2 MiB) 1132.5 1124.2 −0.7% −7.8%
small_string_shared_prefix (1 KiB, 1016 shared bytes) 15.5 15.4 −0.5% −24.2%
small_string_partial_prefix (1 KiB, 512 shared bytes) 11.4 11.5 +0.3% −3.2%
16-byte FixedSizeBinary (other code path) 357.9 358.2 +0.1% +3.5%

With statistics disabled, instructions change by −1.4% to +0.3%.

Combined with #12401

This is this PR applied on top of the #12401 head, compared with the #12401 head alone (same method). #12401 already removes most comparisons for dictionaries with fewer distinct values than rows per page, so the low-cardinality dictionaries gain little more. Plain columns and dictionaries with many distinct values keep the full gain.

input #12401 instr (M) #12401 + this PR instr (M) instr cycles
string_dictionary 329.7 292.1 −11.4% −20.4%
string_dictionary_low_cardinality_20 46.6 46.4 −0.4% −6.7%
string_dictionary_low_cardinality_100 46.9 46.6 −0.7% +0.8%
string_dictionary_low_cardinality_400 48.4 47.4 −2.2% −3.2%
dictionary of 400 values, random keys 50.5 49.5 −1.9% −8.5%
dictionary of 1024 values, random keys 59.4 56.7 −4.6% −12.8%
dictionary of 1024 values, one key per 1024 rows 61.5 61.5 0.0% −3.1%
string_dictionary_1pct_cardinality (value_00000123, 14 bytes) 154.5 143.9 −6.9% −10.7%
dictionary of 1M random values, random keys 204.2 156.4 −23.4% −49.5%
dictionary of 2M random values, random keys 202.5 155.7 −23.1% −56.2%
Utf8, 100 random values round-robin 284.7 231.2 −18.8% −34.6%
Utf8, random 1 to 7 bytes 211.7 191.3 −9.6% −11.1%
Utf8, 8-byte shared prefix + 4 to 11 random bytes 213.0 191.2 −10.2% −10.4%
Utf8, 32-byte shared prefix + 4 to 11 random bytes 266.8 254.7 −4.5% −5.7%
short_string_non_null 180.7 132.2 −26.8% −25.9%
large_string_non_null (identical 256 KiB values) 608.9 609.2 0.0% +6.7%
large_string_shared_prefix (2 MiB) 1132.5 1130.0 −0.2% −3.1%
small_string_shared_prefix (1 KiB, 1016 shared bytes) 15.6 15.6 −0.4% −8.9%
small_string_partial_prefix (1 KiB, 512 shared bytes) 11.2 11.5 +2.8% +7.9%
16-byte FixedSizeBinary (other code path) 358.1 358.3 +0.1% +2.6%

The small_string_* rows are within their ±3% spread between rounds. Cycles in the large_string_non_null row (+6.7%) are noise: its instructions are unchanged, and its per-round cycles range from 115 to 135 for both builds.

Which part gives the gain

I measured each part on its own, on top of #12401 except for the 64-byte bound, which was measured on main:

  • The inline first-8-byte comparison gives almost all of the gain on random values. On its own, it is −19% instructions on 100 random values round-robin and −24% on the 1M-value dictionary. Inlining more than this costs gains: an inline chunk loop, with either split_first_chunk or as_chunks, is 2–4 points slower on random strings. The chunk split runs before the first comparison.
  • The out-of-line chunk loop turns values with equal first 8 bytes from a loss into a gain. With the first-8-byte comparison alone, string_dictionary_1pct_cardinality was +1.3% instructions. Using memcmp on the remaining bytes instead was +11–18% on those inputs.
  • short_key takes random 1 to 7 byte values from −3.6% to −10%.
  • The 64-byte bound: without it, long shared prefixes are slower than memcmp. large_string_non_null was +22% and small_string_shared_prefix +26%. The bound is applied before the call so that the out-of-line function stays a leaf. Bounding inside it gave the function a stack frame and lost the 1pct gain.

Slower inputs

Values whose first 8 bytes are equal and whose common length exceeds 64 bytes cost one extra 8-byte comparison before the same memcmp. On small_string_shared_prefix and small_string_partial_prefix this has measured −0.5% to +0.8% instructions against main across runs, and +2.8% on top of #12401 in the run above. All of these are within the ±3% spread of those two inputs. Columns of long values sharing a long prefix (URLs, paths) hit this. The cost is a few instructions per row against a memcmp of at least 64 bytes.

Are there any significant user-facing changes?

No. Statistics and file bytes are unchanged.

AI usage disclosure

Claude Code (Claude Opus 5.5) wrote the implementation and the unit test. It also built and measured the variants listed above under my direction.

🤖 Generated with Claude Code

Order the min/max of byte-array statistics by the first 8 bytes of each
value as a big-endian u64, which decides most comparisons without a call
to memcmp. Values whose first 8 bytes are equal continue in an
out-of-line function comparing 8-byte chunks when their common length is
at most 64 bytes, and in memcmp when it is longer. Values shorter than 8
bytes use a u64 key built from overlapping loads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mbutrovich

Copy link
Copy Markdown
Contributor

Thanks @shanielh for the detailed measurements. I have opened #12467 since I am growing concerned at the number of these PRs that seem to repeat this same pattern. I feel like there has to be a more general and maintainable solution.

@shanielh

Copy link
Copy Markdown
Author

@mbutrovich, thank you, I'm closing this due to your new issue.

@shanielh shanielh closed this Nov 26, 2026
mbutrovich added a commit to mbutrovich/arrow-rs that referenced this pull request Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet parquet crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants