Repository navigation
perf(arrow-cast): Scan integer and fractional decimal digits in separate loops - #10974
Conversation
…ate loops
parse_decimal scanned the mantissa in a single loop that picked, for every
digit, which counter to compare against its limit and increment: the
integer one before the decimal point, the fractional one after it. The
selection is a data-dependent choice between two `&mut` references, which
keeps both counters in memory and puts a load, a select and a store on the
path of every digit.
Scan the integer digits in one loop and the fractional digits in another,
so each loop works on one counter that stays in a register. The chunk state
the loops share moves into a small accumulator, whose `push` folds the
chunk into the value whenever it fills. The accepted syntax and the errors
reported are unchanged: a second decimal point or any other unexpected byte
is still an invalid format, and an exponent marker still stops the scan at
its offset.
The fold is now reachable from three places (both loops and the final
flush) instead of two, which was enough for LLVM to stop inlining it for
Decimal256 and to stop inlining the digit push for Decimal64, putting a call
on the path of every digit. Both are marked `#[inline(always)]` so the digit
loops stay call-free, as they were before.
CSV reader benchmark (arrow/benches/csv_reader.rs, decimal cases at batch
size 4096, Apple M4 Max), time per batch and change versus the parser
without this change:
4096 decimal32(9, 2) 9 digits(0) 92.652 µs -13.4%
4096 decimal64(18, 2) 18 digits(0) 126.83 µs -3.7%
4096 decimal128(10, 2) 5 digits(0) 71.066 µs -4.8%
4096 decimal128(10, 2) 10 digits(0) 84.078 µs -10.5%
4096 decimal128(18, 6) 18 digits(0) 114.63 µs -13.2%
4096 decimal128(38, 10) 38 digits(0) 194.74 µs -18.4%
4096 decimal256(76, 10) 76 digits(0) 380.25 µs -14.1%
4096 decimal128(10, 2), decimal128(18, 6), decimal128(10, 1)(0) 246.16 µs -12.7%
parse_decimal microbenchmark (arrow-cast/benches/parse_decimal.rs, random
digits per input), µs per 1024 inputs before and after, mean of two
alternated runs of each build:
123.123 9.48 7.90 -16.7%
123.1234 12.95 11.35 -12.3%
123.1 12.08 11.22 -7.1%
123 10.95 10.49 -4.2%
-123.123 9.60 8.17 -15.0%
-123.1234 12.97 11.54 -11.0%
-123.1 12.02 11.47 -4.6%
-123 11.10 10.70 -3.6%
0.0000123 11.40 8.24 -27.7%
12. 10.61 10.27 -3.2%
-12. 10.62 10.52 -0.9%
00.1 11.46 10.78 -5.9%
-00.1 11.54 11.04 -4.3%
12345678912345678.1234 28.09 24.53 -12.7%
-12345678912345678.1234 27.90 24.70 -11.5%
99999999999999999.999 24.28 19.93 -17.9%
-99999999999999999.999 24.02 20.28 -15.6%
.123 7.73 6.35 -17.8%
-.123 7.99 6.61 -17.4%
123. 11.33 10.70 -5.5%
-123. 11.38 10.95 -3.8%
string decimal128 short 10.34 8.40 -18.7%
string decimal128 integer 15.82 11.46 -27.6%
string decimal128 exact scale 15.86 11.64 -26.6%
string decimal128 padded scale 15.11 10.84 -28.2%
string decimal128 rounded scale 19.46 15.42 -20.7%
string decimal128 signed 19.45 15.40 -20.9%
string decimal128 38 digits 32.98 24.31 -26.3%
string decimal128 exponent 24.91 20.50 -17.7%
string decimal128 negative exponent 28.35 18.96 -33.1%
string decimal128 negative scale 22.22 18.47 -16.9%
string decimal128 long fraction 29.11 22.56 -22.5%
string decimal256 76 digits 71.40 59.15 -17.2%
string decimal256 rounded scale 67.08 55.99 -16.5%
string decimal32 short 11.56 8.51 -26.4%
string decimal32 9 digits 14.32 10.56 -26.3%
string decimal64 short 8.19 7.49 -8.5%
string decimal64 18 digits 17.14 12.32 -28.1%
|
CSV reader benchmark: parse_decimal microbenchmark: |
|
run benchmark csv_reader |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
run benchmark cast_kernels |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing neilc/decimal-parser-perf (65f997b) to 32ff76c (merge-base) diff Run configurationrun benchmark csv_readerCPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing neilc/decimal-parser-perf (65f997b) to 32ff76c (merge-base) diff Run configurationrun benchmark cast_kernels
env:
BENCH_FILTER: "decimal"CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
Rich-T-kid
left a comment
There was a problem hiding this comment.
Benchmarks locally look good
ci fail doesnt seem related https://github.com/apache/arrow-rs/actions/runs/33764645565/job/100679297208?pr=10974
|
thanks @neilconway & @Rich-T-kid |
…ate loops (apache#10974) # Which issue does this PR close? - Closes apache#10961. # Rationale for this change We recently consolidated the decimal parsing code to use a single, more correct implementation (apache#10850); however, this resulted in regression performance somewhat. `parse_decimal` used a single loop to scan each digit of the input. Digits before and after the decimal point are treated very differently; profiling the code revealed that doing a data-dependent branch for each digit resulted in worse codegen. Instead, we can split the parsing logic to use two loops, in sequence: first look for digits that precede the decimal point, then those that follow. This avoids the data-dependent branch and seems to significantly improve codegen. On M4 Max, this improves the end-to-end CSV parsing benchmark by 5-15%, and the `parse_decimal` microbenchmark by 5-33%. # What changes are included in this PR? * Split decimal parsing logic into two sequential loops * Refactor digit accumulation logic to avoid duplication * Mark `fold_decimal_chunk` as `inline(always)` -- the refactor resulted in LLVM deciding not to inline this call inside the hot loop, which regressed performance significantly. # Are these changes tested? Yes, covered by existing tests. # Are there any user-facing changes? No. # Tool usage Explored optimization ideas and developed this optimization with Claude Code (Fable 5.1). I revised and understand the resulting code.
…ate loops (apache#10974) # Which issue does this PR close? - Closes apache#10961. # Rationale for this change We recently consolidated the decimal parsing code to use a single, more correct implementation (apache#10850); however, this resulted in regression performance somewhat. `parse_decimal` used a single loop to scan each digit of the input. Digits before and after the decimal point are treated very differently; profiling the code revealed that doing a data-dependent branch for each digit resulted in worse codegen. Instead, we can split the parsing logic to use two loops, in sequence: first look for digits that precede the decimal point, then those that follow. This avoids the data-dependent branch and seems to significantly improve codegen. On M4 Max, this improves the end-to-end CSV parsing benchmark by 5-15%, and the `parse_decimal` microbenchmark by 5-33%. # What changes are included in this PR? * Split decimal parsing logic into two sequential loops * Refactor digit accumulation logic to avoid duplication * Mark `fold_decimal_chunk` as `inline(always)` -- the refactor resulted in LLVM deciding not to inline this call inside the hot loop, which regressed performance significantly. # Are these changes tested? Yes, covered by existing tests. # Are there any user-facing changes? No. # Tool usage Explored optimization ideas and developed this optimization with Claude Code (Fable 5.1). I revised and understand the resulting code.
Which issue does this PR close?
parse_decimal: specialize loop for pre vs. post decimal points #10961.Rationale for this change
We recently consolidated the decimal parsing code to use a single, more correct implementation (#10850); however, this resulted in regression performance somewhat.
parse_decimalused a single loop to scan each digit of the input. Digits before and after the decimal point are treated very differently; profiling the code revealed that doing a data-dependent branch for each digit resulted in worse codegen.Instead, we can split the parsing logic to use two loops, in sequence: first look for digits that precede the decimal point, then those that follow. This avoids the data-dependent branch and seems to significantly improve codegen.
On M4 Max, this improves the end-to-end CSV parsing benchmark by 5-15%, and the
parse_decimalmicrobenchmark by 5-33%.What changes are included in this PR?
fold_decimal_chunkasinline(always)-- the refactor resulted in LLVM deciding not to inline this call inside the hot loop, which regressed performance significantly.Are these changes tested?
Yes, covered by existing tests.
Are there any user-facing changes?
No.
Tool usage
Explored optimization ideas and developed this optimization with Claude Code (Fable 5.1). I revised and understand the resulting code.