shanielh opened a new pull request, #12460:
URL: https://github.com/apache/arrow-rs/pull/12460

   # Which issue does this PR close?
   
   None. This is a self-contained optimization of the per-row min/max 
statistics for byte arrays. It is independent of #12401, which changes only the 
callers of the comparison.
   
   # 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 `u64`s. 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. Instruction counts repeat 
within ±0.5% between rounds, ±1% on the largest dictionaries. Cycles are 
noisier.
   
   | input | main instr (M) | this PR instr (M) | instr | cycles |
   |---|---|---|---|---|
   | `string_dictionary` | 325.7 | 289.5 | −11.1% | −13.8% |
   | `string_dictionary_low_cardinality_20` | 145.6 | 97.8 | −32.8% | −56.5% |
   | `string_dictionary_low_cardinality_100` | 141.3 | 95.3 | −32.6% | −54.0% |
   | `string_dictionary_low_cardinality_400` | 138.9 | 94.8 | −31.7% | −63.5% |
   | dictionary of 400 values, random keys | 139.7 | 94.8 | −32.1% | −66.8% |
   | dictionary of 1024 values, random keys | 142.7 | 96.2 | −32.6% | −67.5% |
   | dictionary of 1024 values, one key per 1024 rows | 156.8 | 149.8 | −4.5% | 
−12.4% |
   | `string_dictionary_1pct_cardinality` (`value_00000123`, 14 bytes) | 163.4 
| 152.2 | −6.9% | −25.2% |
   | dictionary of 1M random values, random keys | 201.1 | 154.0 | −23.4% | 
−53.0% |
   | dictionary of 2M random values, random keys | 201.7 | 153.4 | −23.9% | 
−61.0% |
   | `Utf8`, 100 random values round-robin | 295.8 | 242.4 | −18.1% | −32.1% |
   | `Utf8`, random 1 to 7 bytes | 223.8 | 201.5 | −10.0% | −10.3% |
   | `Utf8`, 8-byte shared prefix + 4 to 11 random bytes | 219.3 | 210.4 | 
−4.1% | −10.9% |
   | `Utf8`, 32-byte shared prefix + 4 to 11 random bytes | 278.6 | 266.6 | 
−4.3% | −4.1% |
   | `short_string_non_null` | 193.2 | 142.9 | −26.0% | −37.3% |
   | `large_string_non_null` (identical 256 KiB values) | 607.1 | 606.9 | −0.0% 
| −0.2% |
   | `large_string_shared_prefix` (2 MiB) | 1108.3 | 1106.7 | −0.1% | −1.7% |
   | `small_string_shared_prefix` (1 KiB, 1016 shared bytes) | 15.2 | 15.3 | 
**+0.7%** | −2.8% |
   | `small_string_partial_prefix` (1 KiB, 512 shared bytes) | 11.2 | 11.2 | 
**+0.8%** | +3.8% (noise, ranges overlap) |
   | 16-byte `FixedSizeBinary` (other code path) | 356.9 | 356.9 | +0.0% | 
+0.1% |
   
   With statistics disabled, these inputs are unchanged within ±1.3%.
   
   ### 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`: +0.7–0.8% 
instructions on `small_string_shared_prefix` and `small_string_partial_prefix`. 
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](https://claude.com/claude-code)
   


-- 
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