Repository navigation
Conversation
…e rows reference Writing an Arrow dictionary array compared every row's value against the page's min and max. When the dictionary is no larger than the batch, mark the keys the rows use and compare each referenced value once instead. Decimals, geospatial statistics and larger dictionaries keep the per-row computation. Co-authored-by: Isaac <[email protected]>
Moving the per-row statistics' min/max update into `update_min_max` cost the per-row path +0.50% ± 0.13% instructions on `string_dictionary_1pct_cardinality` (macOS aarch64, retired instructions over 10 alternating rounds), where the dictionary is larger than a mini-batch and the change otherwise does nothing. Forcing it inline restores main's count (−0.02%). Co-Authored-By: Claude Opus 5.5 <[email protected]>
f0cf440 to
c58117b
Compare
|
run benchmark arrow_writer |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Benchmark completed (GKE) | trigger Target: arrow_writer Comparing revive-dict-array-statistics (c58117b) to 6747ecd (merge-base) diff Run configurationrun benchmark arrow_writer
env:
BENCH_FILTER: string_dictionary
shards: 1
DetailsPer-runner informationarrow_writer — shard 1/1Node: gk3-benchmark-cluster-nap-iv9lhwbu-d3ef1614-wm5p Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arrow_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
run benchmark arrow_writer |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Benchmark completed (GKE) | trigger Target: arrow_writer Comparing revive-dict-array-statistics (c58117b) to 6747ecd (merge-base) diff Run configurationrun benchmark arrow_writer
env:
BENCH_FILTER: string_dictionary
shards: 1
DetailsPer-runner informationarrow_writer — shard 1/1Node: gk3-benchmark-cluster-nap-1xvn17if-a66c6c5a-8rvb Instance: c4a-highmem-16 (12 vCPU / 65 GiB) uname: BENCH_COMMAND: cargo bench --features=arrow,async,test_common,experimental,object_store --bench arrow_writerCPU Details (lscpu)Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@Jefffrey Thanks for the review, I've addressed your comments. |
|
ill keep it open for a bit in case others might wanna take a look, cc @Rich-T-kid & @etseidl |
etseidl
left a comment
There was a problem hiding this comment.
Looks like an improvement to me, thanks!
alamb
left a comment
There was a problem hiding this comment.
Thanks -- this is a neat idea
I do worry if it will slow down the common case where all values in the dictionary are used
I have some comments. A cheaper potentially faster thing to do could be considering if we could special case seeing the same dictionary
aka if the next batch was the same Arc<...> values we could simply skip updating min/max values as it has already been processed
However, if we are only updating min/max values that are referenced then that might be more complicated.
| // compare numerically, so values with different bytes can tie, and which one becomes | ||
| // the statistic depends on the order of the rows | ||
| if encoder.statistics_enabled == EnabledStatistics::None | ||
| || encoder.geo_stats_accumulator.is_some() |
There was a problem hiding this comment.
why not also with geo stats?
| } | ||
|
|
||
| let keys = values.keys().values(); | ||
| let mut referenced = vec![false; dictionary_values.len()]; |
There was a problem hiding this comment.
I think the bitmaps (aka BooleanBuffer_ in this crate are far more efficient than Vec<bool> for this sort of thing -- did you consider using them?
We could also avoid this new allocation by saving a BooleanBuffer on the byte array encoder perhaps
| if let Some(first) = referenced_values.next() { | ||
| let (min, max) = referenced_values.fold((first, first), |(min, max), value| { | ||
| ( | ||
| if min.as_ref() > value.as_ref() { |
There was a problem hiding this comment.
the old code uses is_greater -- is there a reason to do something different here?
There was a problem hiding this comment.
Changed. The dictionary path now passes the values its rows reference for the first time to the same compute_min_max as the per-row path, so both use is_greater (144e02d).
| for &idx in indices { | ||
| match referenced.get_mut(keys[idx].as_usize()) { | ||
| Some(referenced) => *referenced = true, | ||
| // A key outside the dictionary reads as an empty value |
There was a problem hiding this comment.
this is an error -- in a valid arrow array there should never be an key that is outside the dictionary
There was a problem hiding this comment.
Agreed. The fallback is gone. A key outside the dictionary now panics when its value is read, like any out-of-bounds index.
|
Or maybe we could have a fast path for when the dictionary is small (64 values? 128 values?) |
…er page While a page's rows reference the same arrow dictionary, remember which of its values were already folded into the page's statistics, and fold only the values the rows reference for the first time. Once every value is folded, later batches skip the statistics entirely. The newly referenced values go through the same `compute_min_max` and geospatial accumulator as the per-row path, so geospatial and decimal columns take this path too, and a key outside the dictionary panics as it does when indexing the dictionary. `update_statistics` is marked `#[inline(never)]`: with `encode` its only caller per array type, LLVM inlined it and the per-row loop took more instructions than main's, where it is a function of its own. Out of line, `update_statistics` and `encode` compile to the same machine code as main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
f6049c4 to
144e02d
Compare
|
@Jefffrey Anything else before we can this merged? Thanks! 🙏 |
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
ArrowWriterwrites 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: twomemcmpcalls 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?
While a page's rows reference the same arrow dictionary (the same
Arc), the encoder remembers in a bitmap which of its values were already folded into the page's statistics, and folds only the values the rows reference for the first time. Once every value is folded, later mini-batches skip the statistics entirely. The bitmap is reset when the page is flushed or the dictionary changes.Folding only a value's first row in the page gives the same statistics as folding every row, because the comparisons are strict and a repeat of a value already folded never replaces it. So the newly referenced values go through the same
compute_min_max(andis_greater) and geospatial accumulator as the per-row path, and geospatial and decimal columns take this path too. It runs whenever the writer uses its dictionary cache from #11982; the statistics, and so the file, are unchanged.Commits:
#[inline(always)]onupdate_min_max: moving the per-row min/max update into it made the per-row path +0.50% ± 0.13% instructions onstring_dictionary_1pct_cardinality; this restores main's count (−0.02%).StdRngand no 100,000-value dictionary.Arc, replacing the per-mini-batchVec<bool>, as described above. A key outside the dictionary now panics when its value is read, instead of falling back to the per-row path.update_statisticsis#[inline(never)]: withencodeits only caller per array type, LLVM inlined it and the per-row loop took more instructions than on main. Out of line, everyupdate_statisticsandencodecompiles to the same machine code as main (checked by disassembling both builds).Are these changes tested?
test_dictionary_array_writes_same_file_as_its_values(from perf(parquet): compute dictionary array statistics from the values the rows reference #12282): dictionary arrays of 1 to 2,000 values with null keys and null values, written as the same dictionary twice and then a different one, produce the same file as their values cast toUtf8. Removing the reset when the page is flushed, or when the dictionary changes, fails it.test_write_statistics_arrow_dictionary(new,geospatialfeature): a dictionary of WKB points writes the same file, including its geospatial statistics, as its values cast toBinary.parquettests pass with--all-features, apart from the threeinvalid_utf8reader tests, which fail the same way on main.Before / after
Measured before review, at c58117b; the retired-instruction tables below are for the current head.
arrow_writerbench (string_dictionary*cases), GCPc4a-standard-16spot VM (Google Axion, aarch64, 16 vCPU), Debian 12, rust stable, otherwise idle; median of two alternating rounds per tree (main, PR, main, PR):arrow_writerbench,defaultstring_dictionary_low_cardinality_20string_dictionary_low_cardinality_100string_dictionary_low_cardinality_400string_dictionary_1pct_cardinality(per-row path)string_dictionary(per-row path)The other property sets follow the same pattern: low-cardinality dictionaries are 1.83x to 2.81x faster with
bloom_filter,parquet_2,zstdandzstd_parquet_2, 1.69x to 2.18x withcdc, and 1.19x to 1.27x withnumber_distinct_values; the per-row cases are 1.00x to 1.01x.All 35 cases
arrow_writerbenchstring_dictionary_low_cardinality_20/defaultstring_dictionary_low_cardinality_20/bloom_filterstring_dictionary_low_cardinality_20/parquet_2string_dictionary_low_cardinality_20/zstdstring_dictionary_low_cardinality_20/zstd_parquet_2string_dictionary_low_cardinality_20/cdcstring_dictionary_low_cardinality_20/number_distinct_valuesstring_dictionary_low_cardinality_100/defaultstring_dictionary_low_cardinality_100/bloom_filterstring_dictionary_low_cardinality_100/parquet_2string_dictionary_low_cardinality_100/zstdstring_dictionary_low_cardinality_100/zstd_parquet_2string_dictionary_low_cardinality_100/cdcstring_dictionary_low_cardinality_100/number_distinct_valuesstring_dictionary_low_cardinality_400/defaultstring_dictionary_low_cardinality_400/bloom_filterstring_dictionary_low_cardinality_400/parquet_2string_dictionary_low_cardinality_400/zstdstring_dictionary_low_cardinality_400/zstd_parquet_2string_dictionary_low_cardinality_400/cdcstring_dictionary_low_cardinality_400/number_distinct_valuesstring_dictionary_1pct_cardinality/defaultstring_dictionary_1pct_cardinality/bloom_filterstring_dictionary_1pct_cardinality/parquet_2string_dictionary_1pct_cardinality/zstdstring_dictionary_1pct_cardinality/zstd_parquet_2string_dictionary_1pct_cardinality/cdcstring_dictionary_1pct_cardinality/number_distinct_valuesstring_dictionary/defaultstring_dictionary/bloom_filterstring_dictionary/parquet_2string_dictionary/zstdstring_dictionary/zstd_parquet_2string_dictionary/cdcstring_dictionary/number_distinct_valuesstring_dictionary_1pct_cardinality/cdcread 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.Retired instructions
Wall-clock benchmarks on a loaded development machine were too noisy to read, so the change is also 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 with the bench's writer properties. The cost per write is the count of 10 writes minus that of 0. Median of five alternating rounds; instruction counts repeat within about ±0.5% (±1% for the two largest dictionaries, whose interner hashing varies run to run), cycles within about ±5% except for the two largest dictionaries, which drift by up to 40% from round to round in both trees.
defaultstring_dictionary_low_cardinality_20string_dictionary_low_cardinality_100string_dictionary_low_cardinality_400string_dictionary_1pct_cardinalitystring_dictionary(every value unique)Utf8, 100 values (no dictionary array)The statistics' cost is the extra instructions with statistics on (
default) relative toEnabledStatistics::Noneon the same tree.Where the gain comes from
The gain is the per-row statistics comparisons the dictionary path no longer makes: each value is compared once per page, and a mini-batch whose dictionary values are all folded does no statistics work. Before review the PR compared each referenced value once per mini-batch, which left 13 M to 74 M instructions of statistics' cost on the low-cardinality and 1024-value cases above; keeping the folded values across mini-batches takes that to 1 M to 18 M.
What remains is the bit test per row while a page still has values left to fold (the 1024-value cases, where each mini-batch references new values), and per-row work on dictionaries where most rows reference a value for the first time (
string_dictionary, 1M values).Slower inputs: none measured beyond noise. The 1M-value dictionary is 17 MB, so it overflows the 1 MiB dictionary page and falls back to plain encoding after 51,200 rows; the other 95% of its rows take main's per-row path, whose machine code is unchanged. The rows before the fallback reference a value for the first time almost every time, so the bitmap only adds a bit test and a push per row there: +0.4% instructions, within that case's ±1%.
A production workload
Measured before review, at c58117b: 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 (including the random-key and boundary cases), ran the measurements and drafted this description, which the submitter reviewed. In review it made the changes listed above, choosing with the submitter how to address each comment.
🤖 Generated with Claude Code