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

   # Which issue does this PR close?
   
   None. This revives #12282 by @dtenedor, which was closed without being 
merged and without a stated reason. Its commit is kept as authored; one commit 
on top fixes a small regression it introduced on the per-row path.
   
   # Rationale for this change
   
   When the `ArrowWriter` writes an Arrow dictionary array of strings or 
binary, it updates the page statistics by comparing every row's value with the 
running min and max: two `memcmp` calls per row. With the dictionary cache from 
#11982, interning is already one lookup per row, so these comparisons are now 
most of the cost of writing a low-cardinality dictionary column.
   
   A batch of rows can only reference values from the dictionary, so its min 
and max are the min and max of the values it references, however many rows 
reference each one.
   
   # What changes are included in this PR?
   
   - The original commit from #12282, unchanged: `update_dictionary_statistics` 
marks the keys a mini-batch's rows use, then compares each referenced 
dictionary value once. It is used when the dictionary has no more values than 
the mini-batch has rows. The per-row computation is kept for larger 
dictionaries, decimals, geospatial statistics, and a key outside the 
dictionary. The statistics, and so the file, are unchanged. See #12282 for the 
full description and its byte-identical output checks.
   - A follow-up commit: moving the per-row path's min/max update into the 
shared `update_min_max` made that path +0.50% ± 0.13% instructions on 
`string_dictionary_1pct_cardinality` (a dictionary larger than a mini-batch, 
where the change otherwise does nothing). `#[inline(always)]` on 
`update_min_max` restores main's count (−0.02%).
   
   # Are these changes tested?
   
   The new test from #12282 (dictionary arrays of 1 to 100,000 values, with 
null keys and null values, written byte-identical to their values cast to 
`Utf8`) and the existing `arrow_writer` tests pass on current main (122 passed).
   
   ### Before / after
   
   `arrow_writer` bench (`string_dictionary*` cases), GCP `c4a-standard-16` 
spot VM (Google Axion, aarch64, 16 vCPU), Debian 12, rust stable, otherwise 
idle; median of two alternating rounds per tree (main, PR, main, PR):
   
   | `arrow_writer` bench, `default` | main | PR | Speedup |
   | :--- | ---: | ---: | ---: |
   | `string_dictionary_low_cardinality_20` | 11.524 ms | 4.102 ms | 2.81x |
   | `string_dictionary_low_cardinality_100` | 11.589 ms | 4.613 ms | 2.51x |
   | `string_dictionary_low_cardinality_400` | 11.680 ms | 6.177 ms | 1.89x |
   | `string_dictionary_1pct_cardinality` (per-row path) | 13.057 ms | 12.898 
ms | 1.01x |
   | `string_dictionary` (per-row path) | 42.605 ms | 42.606 ms | 1.00x |
   
   The other property sets follow the same pattern: low-cardinality 
dictionaries are 1.83x to 2.81x faster with `bloom_filter`, `parquet_2`, `zstd` 
and `zstd_parquet_2`, 1.69x to 2.18x with `cdc`, and 1.19x to 1.27x with 
`number_distinct_values`; the per-row cases are 1.00x to 1.01x.
   
   <details>
   <summary>All 35 cases</summary>
   
   | `arrow_writer` bench | main | PR | Speedup |
   | :--- | ---: | ---: | ---: |
   | `string_dictionary_low_cardinality_20/default` | 11.524 ms | 4.102 ms | 
2.81x |
   | `string_dictionary_low_cardinality_20/bloom_filter` | 11.669 ms | 4.209 ms 
| 2.77x |
   | `string_dictionary_low_cardinality_20/parquet_2` | 11.537 ms | 4.113 ms | 
2.81x |
   | `string_dictionary_low_cardinality_20/zstd` | 11.705 ms | 4.260 ms | 2.75x 
|
   | `string_dictionary_low_cardinality_20/zstd_parquet_2` | 11.657 ms | 4.231 
ms | 2.76x |
   | `string_dictionary_low_cardinality_20/cdc` | 14.066 ms | 6.459 ms | 2.18x |
   | `string_dictionary_low_cardinality_20/number_distinct_values` | 34.700 ms 
| 27.334 ms | 1.27x |
   | `string_dictionary_low_cardinality_100/default` | 11.589 ms | 4.613 ms | 
2.51x |
   | `string_dictionary_low_cardinality_100/bloom_filter` | 11.732 ms | 4.743 
ms | 2.47x |
   | `string_dictionary_low_cardinality_100/parquet_2` | 11.593 ms | 4.616 ms | 
2.51x |
   | `string_dictionary_low_cardinality_100/zstd` | 11.956 ms | 4.966 ms | 
2.41x |
   | `string_dictionary_low_cardinality_100/zstd_parquet_2` | 11.931 ms | 4.940 
ms | 2.42x |
   | `string_dictionary_low_cardinality_100/cdc` | 14.084 ms | 6.895 ms | 2.04x 
|
   | `string_dictionary_low_cardinality_100/number_distinct_values` | 34.660 ms 
| 27.734 ms | 1.25x |
   | `string_dictionary_low_cardinality_400/default` | 11.680 ms | 6.177 ms | 
1.89x |
   | `string_dictionary_low_cardinality_400/bloom_filter` | 11.831 ms | 6.291 
ms | 1.88x |
   | `string_dictionary_low_cardinality_400/parquet_2` | 11.694 ms | 6.184 ms | 
1.89x |
   | `string_dictionary_low_cardinality_400/zstd` | 12.188 ms | 6.669 ms | 
1.83x |
   | `string_dictionary_low_cardinality_400/zstd_parquet_2` | 12.139 ms | 6.637 
ms | 1.83x |
   | `string_dictionary_low_cardinality_400/cdc` | 14.177 ms | 8.403 ms | 1.69x 
|
   | `string_dictionary_low_cardinality_400/number_distinct_values` | 34.915 ms 
| 29.432 ms | 1.19x |
   | `string_dictionary_1pct_cardinality/default` | 13.057 ms | 12.898 ms | 
1.01x |
   | `string_dictionary_1pct_cardinality/bloom_filter` | 13.504 ms | 13.431 ms 
| 1.01x |
   | `string_dictionary_1pct_cardinality/parquet_2` | 12.931 ms | 12.872 ms | 
1.00x |
   | `string_dictionary_1pct_cardinality/zstd` | 15.748 ms | 15.676 ms | 1.00x |
   | `string_dictionary_1pct_cardinality/zstd_parquet_2` | 15.634 ms | 15.619 
ms | 1.00x |
   | `string_dictionary_1pct_cardinality/cdc` | 16.108 ms | 16.418 ms | 0.98x |
   | `string_dictionary_1pct_cardinality/number_distinct_values` | 31.861 ms | 
31.804 ms | 1.00x |
   | `string_dictionary/default` | 42.605 ms | 42.606 ms | 1.00x |
   | `string_dictionary/bloom_filter` | 93.483 ms | 93.292 ms | 1.00x |
   | `string_dictionary/parquet_2` | 48.608 ms | 48.474 ms | 1.00x |
   | `string_dictionary/zstd` | 215.190 ms | 215.414 ms | 1.00x |
   | `string_dictionary/zstd_parquet_2` | 193.010 ms | 192.491 ms | 1.00x |
   | `string_dictionary/cdc` | 47.762 ms | 47.586 ms | 1.00x |
   | `string_dictionary/number_distinct_values` | 100.015 ms | 99.778 ms | 
1.00x |
   
   `string_dictionary_1pct_cardinality/cdc` read 0.98x in these two rounds, one 
of the PR's being slow (16.625 ms against 16.211 ms). Five more alternating 
rounds of that case alone gave 16.19 ms (main) and 16.22 ms (PR), within main's 
own spread of 16.00 to 16.50 ms.
   
   </details>
   
   ### Retired instructions
   
   Wall-clock benchmarks on a loaded development machine were too noisy to 
read, so the change was also measured by retired instructions on Apple M5 Pro 
(aarch64, macOS): a small fixed-work program builds the same batches as the 
benches (same generators, seed and 1M-row size) and writes each one 10 times to 
an empty sink with the bench's writer properties; the write cost is the 
instruction count of 10 writes minus that of 0. Counts repeat within about 
±0.3% run to run.
   
   | Batch, `default` | main | PR | Change |
   | :--- | ---: | ---: | ---: |
   | `string_dictionary_low_cardinality_20` | 145.9 M | 57.9 M | -60.3% |
   | `string_dictionary_low_cardinality_100` | 141.3 M | 65.7 M | -53.5% |
   | `string_dictionary_low_cardinality_400` | 139.4 M | 93.6 M | -32.9% |
   | `string_dictionary_1pct_cardinality` | 163.3 M | 163.3 M | +0.0% |
   | `string_dictionary` | 328.5 M | 323.3 M | -1.6% |
   
   
   This is how the per-row regression fixed by the second commit was found: 
+0.50% ± 0.13% on `string_dictionary_1pct_cardinality` (10 alternating rounds) 
before it, −0.02% after.
   
   ### A production workload
   
   On a Parquet sort-compaction workload (about 230 columns, most of them 
low-cardinality strings written from dictionary arrays), the change applied to 
parquet 60.2.0 reduced the compaction's total retired instructions by 6.2% (862 
G to 809 G, three runs each) and its CPU time by 5.8%, with identical output.
   
   # Are there any significant user-facing changes?
   
   No.
   
   # AI usage disclosure
   
   The original commit was written as described in #12282. For this revival, an 
AI coding agent (Claude Code) rebased it, found and fixed the per-row 
regression, wrote the measurement program, ran the measurements and drafted 
this description, which the submitter reviewed.
   
   🤖 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