shanielh opened a new pull request, #12416: URL: https://github.com/apache/arrow-rs/pull/12416
# Which issue does this PR close? None. Found while measuring #12401, which leaves this cost in place. # Rationale for this change With `set_write_row_group_number_distinct_values(true)`, the `ArrowWriter` counts a column's distinct values by hashing them into a set. For an Arrow dictionary array it maps each non-null row to its dictionary value and hashes that value once per row, so a batch of 1M rows over 20 values hashes 1M times to find 20 values. In the `arrow_writer` bench this hashing is most of the write time of the `number_distinct_values` cases on low-cardinality dictionaries. # What changes are included in this PR? When the dictionary has no more values than the batch has non-null rows, the writer marks the keys the rows use and hashes each referenced value once. A larger dictionary keeps the per-row path, so the pass over the dictionary is bounded by the number of rows. The set receives the same values either way, so the distinct count is unchanged. # Are these changes tested? The existing dictionary NDV tests (duplicate rows, unreferenced values, keys that change meaning across batches) take the new path. A new test checks that dictionaries smaller and larger than the batch, with null keys, null values and duplicate values, report the same distinct count as their values cast to `Utf8`. ### Benchmarks Targeted bench: `arrow_writer`, the `string_dictionary*/number_distinct_values` cases. Other property sets do not reach this code. Measured by retired instructions and cycles 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), plus random-key and large-dictionary variants, and writes each one 10 times to an empty sink; the cost per write is the count of 10 writes minus that of 0, divided by 10. Median of five alternating rounds; instruction counts repeat within about ±0.3%. | Batch, `number_distinct_values` | Instructions, main | PR | Change | Cycles, main | PR | Change | | :--- | ---: | ---: | ---: | ---: | ---: | ---: | | `string_dictionary_low_cardinality_20` | 423.8 M | 162.0 M | -61.8% | 90.1 M | 38.7 M | -57.1% | | `string_dictionary_low_cardinality_100` | 419.8 M | 157.6 M | -62.5% | 88.6 M | 37.0 M | -58.2% | | `string_dictionary_low_cardinality_400` | 417.2 M | 155.3 M | -62.8% | 96.6 M | 43.8 M | -54.6% | | 400 values, random keys | 418.1 M | 156.1 M | -62.7% | 129.2 M | 48.5 M | -62.5% | | `string_dictionary_1pct_cardinality` (10,485 values) | 375.4 M | 181.3 M | -51.7% | 75.1 M | 34.9 M | -53.5% | | 1M values, random keys | 629.4 M | 546.6 M | -13.2% | 756.2 M | 476.9 M | -36.9% | | 2M values, random keys (per-row path) | 633.7 M | 633.5 M | -0.0% | 892.4 M | 915.7 M | +2.6% | | `string_dictionary` (every value unique) | 801.8 M | 812.6 M | +1.3% | 290.6 M | 294.5 M | +1.3% | The gain comes from no longer hashing a value once per row; the round-robin `low_cardinality` benches and the same dictionaries with random keys gain the same. The distinct count covers the whole written batch rather than a 1024-row mini-batch, so `string_dictionary_1pct_cardinality` takes the new path too. **Slower input:** `string_dictionary` is random strings cast to a dictionary, so every value is referenced exactly once. It takes the new path because the dictionary is smaller than the non-null rows, but there is nothing to deduplicate, and marking and scanning the keys costs +1.3%. Real workloads hit this with high-cardinality dictionary columns such as identifiers and this option enabled. A stricter threshold would also drop the 1M-value random-key gain, which cannot be told apart from this case without that same pass. The 2M-value case's +2.6% cycles is within its round-to-round spread; its instruction count is unchanged. **With the option off** the new code does not run and instruction counts are unchanged, but this build measured 4-7% more cycles on the low-cardinality cases. Main rebuilt in the PR's worktree matched main within 0.6%, and building both trees with `RUSTFLAGS="-C llvm-args=-align-all-functions=6"` removed the difference (-1.0% to +0.8%), so it is a code-alignment effect of the rebuild rather than added work. # Are there any significant user-facing changes? No. # AI usage disclosure An AI coding agent (Claude Code) found this while measuring #12401, wrote the change and the test, extended the measurement program, ran the measurements, including the alignment check, 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]
