Skip to content

perf(arrow-cast): Scan integer and fractional decimal digits in separate loops - #10974

Merged
Jefffrey merged 1 commit into
apache:mainfrom
neilconway:neilc/decimal-parser-perf
Sep 4, 2026
Merged

Jefffrey merged 1 commit into
apache:mainfrom
neilconway:neilc/decimal-parser-perf

Conversation

@neilconway

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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

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%
@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-cast labels Sep 3, 2026
@neilconway

Copy link
Copy Markdown
Contributor Author

CSV reader benchmark:

    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:

    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%

@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark csv_reader

@adriangbot

This comment was marked as duplicate.

@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark cast_kernels
env:
BENCH_FILTER: decimal

@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing neilc/decimal-parser-perf (65f997b) to 32ff76c (merge-base) diff

Run configuration
run benchmark csv_reader
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                             main                                   neilc_decimal-parser-perf
-----                                                                             ----                                   -------------------------
4096 StringView(10, 0) - 1024                                                     1.02     94.1±2.69µs        ? ?/sec    1.00     92.6±0.30µs        ? ?/sec
4096 StringView(10, 0) - 128                                                      1.00    102.8±0.47µs        ? ?/sec    1.00    103.1±0.36µs        ? ?/sec
4096 StringView(10, 0) - 4096                                                     1.00     91.8±0.38µs        ? ?/sec    1.05     96.3±0.32µs        ? ?/sec
4096 StringView(100, 0) - 1024                                                    1.00    296.2±0.79µs        ? ?/sec    1.00    297.6±0.32µs        ? ?/sec
4096 StringView(100, 0) - 128                                                     1.00    307.6±0.45µs        ? ?/sec    1.00    307.0±0.33µs        ? ?/sec
4096 StringView(100, 0) - 4096                                                    1.00    292.6±0.35µs        ? ?/sec    1.00    292.7±0.32µs        ? ?/sec
4096 StringView(100, 0.5) - 1024                                                  1.00    203.2±0.39µs        ? ?/sec    1.00    202.7±0.33µs        ? ?/sec
4096 StringView(100, 0.5) - 128                                                   1.01    222.1±0.46µs        ? ?/sec    1.00    220.5±0.45µs        ? ?/sec
4096 StringView(100, 0.5) - 4096                                                  1.00    199.7±0.44µs        ? ?/sec    1.00    199.4±0.34µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), StringView(100, 0), i64(0) - 1024    1.00    677.1±0.82µs        ? ?/sec    1.01    682.1±0.66µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), StringView(100, 0), i64(0) - 128     1.00    725.5±0.99µs        ? ?/sec    1.01    730.3±0.76µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), StringView(100, 0), i64(0) - 4096    1.00    682.0±0.94µs        ? ?/sec    1.03    700.1±8.23µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), f64(0), i64(0) - 1024                1.00    533.8±0.86µs        ? ?/sec    1.00    534.9±0.42µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), f64(0), i64(0) - 128                 1.01    589.8±0.78µs        ? ?/sec    1.00    585.3±0.48µs        ? ?/sec
4096 StringView(20, 0.5), StringView(30, 0), f64(0), i64(0) - 4096                1.00    526.9±0.85µs        ? ?/sec    1.00    526.5±0.61µs        ? ?/sec
4096 StringView(30, 0) - 1024                                                     1.00    127.4±0.75µs        ? ?/sec    1.00    127.2±0.48µs        ? ?/sec
4096 StringView(30, 0) - 128                                                      1.00    138.9±0.33µs        ? ?/sec    1.00    138.5±0.11µs        ? ?/sec
4096 StringView(30, 0) - 4096                                                     1.00    125.3±0.15µs        ? ?/sec    1.08   134.9±11.26µs        ? ?/sec
4096 decimal128(10, 2) 10 digits(0) - 1024                                        1.00    143.5±0.16µs        ? ?/sec    1.03    147.3±0.93µs        ? ?/sec
4096 decimal128(10, 2) 10 digits(0) - 128                                         1.06    156.4±0.17µs        ? ?/sec    1.00    147.2±0.17µs        ? ?/sec
4096 decimal128(10, 2) 10 digits(0) - 4096                                        1.06    142.4±0.14µs        ? ?/sec    1.00    133.7±0.10µs        ? ?/sec
4096 decimal128(10, 2) 5 digits(0) - 1024                                         1.08    117.9±0.52µs        ? ?/sec    1.00    108.9±0.24µs        ? ?/sec
4096 decimal128(10, 2) 5 digits(0) - 128                                          1.06    128.4±0.28µs        ? ?/sec    1.00    120.7±0.14µs        ? ?/sec
4096 decimal128(10, 2) 5 digits(0) - 4096                                         1.00    113.8±1.50µs        ? ?/sec    1.00    113.7±0.35µs        ? ?/sec
4096 decimal128(10, 2), decimal128(18, 6), decimal128(10, 1)(0) - 1024            1.03    435.9±0.28µs        ? ?/sec    1.00    424.8±6.62µs        ? ?/sec
4096 decimal128(10, 2), decimal128(18, 6), decimal128(10, 1)(0) - 128             1.06    463.2±0.26µs        ? ?/sec    1.00    437.9±0.30µs        ? ?/sec
4096 decimal128(10, 2), decimal128(18, 6), decimal128(10, 1)(0) - 4096            1.06    434.3±0.39µs        ? ?/sec    1.00    409.1±0.59µs        ? ?/sec
4096 decimal128(18, 6) 18 digits(0) - 1024                                        1.03    200.9±2.99µs        ? ?/sec    1.00    194.6±0.96µs        ? ?/sec
4096 decimal128(18, 6) 18 digits(0) - 128                                         1.03    211.3±0.25µs        ? ?/sec    1.00    205.4±0.12µs        ? ?/sec
4096 decimal128(18, 6) 18 digits(0) - 4096                                        1.01    199.9±3.18µs        ? ?/sec    1.00    198.4±4.73µs        ? ?/sec
4096 decimal128(38, 10) 38 digits(0) - 1024                                       1.06    344.7±0.28µs        ? ?/sec    1.00    326.0±1.30µs        ? ?/sec
4096 decimal128(38, 10) 38 digits(0) - 128                                        1.05    358.3±0.22µs        ? ?/sec    1.00    340.7±0.33µs        ? ?/sec
4096 decimal128(38, 10) 38 digits(0) - 4096                                       1.09    367.0±1.21µs        ? ?/sec    1.00    336.2±3.44µs        ? ?/sec
4096 decimal256(76, 10) 76 digits(0) - 1024                                       1.11    735.3±0.95µs        ? ?/sec    1.00    664.2±5.20µs        ? ?/sec
4096 decimal256(76, 10) 76 digits(0) - 128                                        1.11    750.5±1.43µs        ? ?/sec    1.00    675.2±1.64µs        ? ?/sec
4096 decimal256(76, 10) 76 digits(0) - 4096                                       1.11    732.4±0.88µs        ? ?/sec    1.00    661.0±1.44µs        ? ?/sec
4096 decimal32(9, 2) 9 digits(0) - 1024                                           1.02    139.9±0.34µs        ? ?/sec    1.00    136.6±0.10µs        ? ?/sec
4096 decimal32(9, 2) 9 digits(0) - 128                                            1.03    151.5±0.39µs        ? ?/sec    1.00    147.7±0.20µs        ? ?/sec
4096 decimal32(9, 2) 9 digits(0) - 4096                                           1.00    138.7±0.23µs        ? ?/sec    1.08    149.9±0.91µs        ? ?/sec
4096 decimal64(18, 2) 18 digits(0) - 1024                                         1.05    193.2±0.16µs        ? ?/sec    1.00    184.1±0.08µs        ? ?/sec
4096 decimal64(18, 2) 18 digits(0) - 128                                          1.04    204.8±0.19µs        ? ?/sec    1.00    196.8±0.22µs        ? ?/sec
4096 decimal64(18, 2) 18 digits(0) - 4096                                         1.08    198.6±6.71µs        ? ?/sec    1.00    184.1±0.07µs        ? ?/sec
4096 f32(0) - 1024                                                                1.00    149.1±0.36µs        ? ?/sec    1.02    151.6±0.50µs        ? ?/sec
4096 f32(0) - 128                                                                 1.00    163.9±0.26µs        ? ?/sec    1.00    164.5±0.42µs        ? ?/sec
4096 f32(0) - 4096                                                                1.00    147.4±0.38µs        ? ?/sec    1.02    150.0±0.93µs        ? ?/sec
4096 f32_small(0) - 1024                                                          1.00    126.3±0.29µs        ? ?/sec    1.00    126.5±0.34µs        ? ?/sec
4096 f32_small(0) - 128                                                           1.01    139.9±0.32µs        ? ?/sec    1.00    138.6±0.40µs        ? ?/sec
4096 f32_small(0) - 4096                                                          1.00    124.7±0.34µs        ? ?/sec    1.00    124.7±0.38µs        ? ?/sec
4096 f64(0) - 1024                                                                1.01    168.6±0.36µs        ? ?/sec    1.00    167.2±0.53µs        ? ?/sec
4096 f64(0) - 128                                                                 1.02    186.8±0.36µs        ? ?/sec    1.00    183.9±0.49µs        ? ?/sec
4096 f64(0) - 4096                                                                1.01    167.9±0.59µs        ? ?/sec    1.00    166.3±0.55µs        ? ?/sec
4096 f64_small(0) - 1024                                                          1.02    126.5±0.32µs        ? ?/sec    1.00    124.5±0.47µs        ? ?/sec
4096 f64_small(0) - 128                                                           1.00    139.6±0.36µs        ? ?/sec    1.00    139.4±0.38µs        ? ?/sec
4096 f64_small(0) - 4096                                                          1.02    125.0±0.33µs        ? ?/sec    1.00    123.0±0.33µs        ? ?/sec
4096 i32(0) - 1024                                                                1.03    157.1±0.64µs        ? ?/sec    1.00    153.2±0.64µs        ? ?/sec
4096 i32(0) - 128                                                                 1.03    171.7±0.51µs        ? ?/sec    1.00    167.1±0.45µs        ? ?/sec
4096 i32(0) - 4096                                                                1.04    156.1±0.67µs        ? ?/sec    1.00    150.7±0.47µs        ? ?/sec
4096 i32_small(0) - 1024                                                          1.01     98.6±0.57µs        ? ?/sec    1.00     97.9±0.38µs        ? ?/sec
4096 i32_small(0) - 128                                                           1.01    112.1±0.74µs        ? ?/sec    1.00    111.4±0.43µs        ? ?/sec
4096 i32_small(0) - 4096                                                          1.00     96.7±0.51µs        ? ?/sec    1.00     96.7±0.39µs        ? ?/sec
4096 i64(0) - 1024                                                                1.00    207.4±0.52µs        ? ?/sec    1.00    207.3±0.37µs        ? ?/sec
4096 i64(0) - 128                                                                 1.00    225.7±0.38µs        ? ?/sec    1.00    225.5±0.55µs        ? ?/sec
4096 i64(0) - 4096                                                                1.00    206.5±0.38µs        ? ?/sec    1.00    206.7±0.36µs        ? ?/sec
4096 i64_small(0) - 1024                                                          1.00    114.3±0.46µs        ? ?/sec    1.01    115.5±0.50µs        ? ?/sec
4096 i64_small(0) - 128                                                           1.00    128.2±0.35µs        ? ?/sec    1.03    132.4±0.81µs        ? ?/sec
4096 i64_small(0) - 4096                                                          1.00    112.9±0.60µs        ? ?/sec    1.00    113.3±0.42µs        ? ?/sec
4096 string(10, 0) - 1024                                                         1.00     79.7±0.20µs        ? ?/sec    1.01     80.5±0.06µs        ? ?/sec
4096 string(10, 0) - 128                                                          1.00     98.3±0.21µs        ? ?/sec    1.00     98.2±0.11µs        ? ?/sec
4096 string(10, 0) - 4096                                                         1.05     84.8±0.67µs        ? ?/sec    1.00     80.4±1.41µs        ? ?/sec
4096 string(100, 0) - 1024                                                        1.01    295.4±0.52µs        ? ?/sec    1.00    293.8±0.51µs        ? ?/sec
4096 string(100, 0) - 128                                                         1.00    314.4±0.42µs        ? ?/sec    1.01    317.1±0.59µs        ? ?/sec
4096 string(100, 0) - 4096                                                        1.00    291.1±0.49µs        ? ?/sec    1.03    299.6±6.68µs        ? ?/sec
4096 string(100, 0.5) - 1024                                                      1.00    208.2±0.89µs        ? ?/sec    1.00    208.7±0.53µs        ? ?/sec
4096 string(100, 0.5) - 128                                                       1.00    228.1±0.94µs        ? ?/sec    1.03    234.3±0.54µs        ? ?/sec
4096 string(100, 0.5) - 4096                                                      1.00    206.9±0.75µs        ? ?/sec    1.00    206.9±0.55µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), f64(0), i64(0) - 1024                        1.00    532.3±0.65µs        ? ?/sec    1.00    531.5±0.41µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), f64(0), i64(0) - 128                         1.00    591.1±1.73µs        ? ?/sec    1.00    593.0±0.55µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), f64(0), i64(0) - 4096                        1.00    522.0±0.61µs        ? ?/sec    1.00    523.7±0.49µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), string(100, 0), i64(0) - 1024                1.00    671.4±1.02µs        ? ?/sec    1.01    677.9±0.79µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), string(100, 0), i64(0) - 128                 1.00    724.8±1.07µs        ? ?/sec    1.02    736.2±0.88µs        ? ?/sec
4096 string(20, 0.5), string(30, 0), string(100, 0), i64(0) - 4096                1.00    666.0±1.29µs        ? ?/sec    1.01    675.2±0.88µs        ? ?/sec
4096 string(30, 0) - 1024                                                         1.00    124.9±0.08µs        ? ?/sec    1.00    125.5±0.07µs        ? ?/sec
4096 string(30, 0) - 128                                                          1.02    143.8±0.15µs        ? ?/sec    1.00    140.8±0.14µs        ? ?/sec
4096 string(30, 0) - 4096                                                         1.00    123.3±0.06µs        ? ?/sec    1.07    131.4±1.75µs        ? ?/sec
4096 u64(0) - 1024                                                                1.00    201.2±0.40µs        ? ?/sec    1.00    202.0±0.40µs        ? ?/sec
4096 u64(0) - 128                                                                 1.00    217.4±0.38µs        ? ?/sec    1.01    220.0±0.50µs        ? ?/sec
4096 u64(0) - 4096                                                                1.00    200.6±0.34µs        ? ?/sec    1.00    200.7±0.41µs        ? ?/sec
4096 u64_small(0) - 1024                                                          1.00     92.5±0.37µs        ? ?/sec    1.01     93.0±0.45µs        ? ?/sec
4096 u64_small(0) - 128                                                           1.02    109.2±0.52µs        ? ?/sec    1.00    106.8±0.38µs        ? ?/sec
4096 u64_small(0) - 4096                                                          1.00     91.3±0.39µs        ? ?/sec    1.00     91.7±0.43µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 915.2s
Peak memory 13.6 MiB
Avg memory 11.8 MiB
CPU user 911.8s
CPU sys 0.1s
Peak spill 0 B

branch

Metric Value
Wall time 910.2s
Peak memory 12.8 MiB
Avg memory 11.6 MiB
CPU user 907.0s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing neilc/decimal-parser-perf (65f997b) to 32ff76c (merge-base) diff

Run configuration
run benchmark cast_kernels
env:
  BENCH_FILTER: "decimal"
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                              main                                   neilc_decimal-parser-perf
-----                                                              ----                                   -------------------------
"cast decimal128 to float64"                                       1.00     27.1±0.02µs        ? ?/sec    1.00     27.1±0.01µs        ? ?/sec
"cast decimal128 to int64"                                         1.00     47.9±0.47µs        ? ?/sec    1.01     48.3±0.62µs        ? ?/sec
"cast decimal128 to int8"                                          1.00     62.3±1.65µs        ? ?/sec    1.01     63.0±2.37µs        ? ?/sec
"cast decimal256 to float64"                                       1.00     68.5±0.09µs        ? ?/sec    1.00     68.6±0.07µs        ? ?/sec
"cast decimal256 to int64"                                         1.00    146.0±0.95µs        ? ?/sec    1.01    147.3±1.18µs        ? ?/sec
"cast float64 to decimal128(32, 3)"                                1.00     32.2±0.02µs        ? ?/sec    1.00     32.3±0.02µs        ? ?/sec
"cast invalid float64 to to decimal128(32, 3)"                     1.00     15.6±0.01µs        ? ?/sec    1.00     15.7±0.01µs        ? ?/sec
"cast string to decimal128(38, 3)"                                 1.13     98.0±1.11µs        ? ?/sec    1.00     86.7±0.39µs        ? ?/sec
"cast string to decimal256(76, 3)"                                 1.04    150.5±1.00µs        ? ?/sec    1.00    144.8±0.66µs        ? ?/sec
cast decimal128 to decimal128 512                                  1.00      6.9±0.01µs        ? ?/sec    1.00      6.9±0.00µs        ? ?/sec
cast decimal128 to decimal128 512 lower precision                  1.00     19.8±0.02µs        ? ?/sec    1.01     20.0±0.04µs        ? ?/sec
cast decimal128 to decimal128 512 with lower scale (infallible)    1.00     45.9±0.06µs        ? ?/sec    1.00     45.9±0.06µs        ? ?/sec
cast decimal128 to decimal128 512 with same scale                  1.00     75.0±0.54ns        ? ?/sec    1.01     75.8±1.07ns        ? ?/sec
cast decimal128 to decimal256 512                                  1.00     26.2±0.02µs        ? ?/sec    1.00     26.2±0.01µs        ? ?/sec
cast decimal256 to decimal128 512                                  1.00    318.3±0.84µs        ? ?/sec    1.00    319.6±0.31µs        ? ?/sec
cast decimal256 to decimal256 512                                  1.00     82.6±0.26µs        ? ?/sec    1.00     82.5±0.17µs        ? ?/sec
cast decimal256 to decimal256 512 with same scale                  1.00     75.2±0.93ns        ? ?/sec    1.02     76.5±1.48ns        ? ?/sec
cast int64 to decimal32(9, -1) 512                                 1.00      3.4±0.00µs        ? ?/sec    1.00      3.4±0.00µs        ? ?/sec
cast int64 to decimal32(9, 0) 512                                  1.00   1286.1±1.42ns        ? ?/sec    1.00   1279.9±1.87ns        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 190.0s
Peak memory 18.2 MiB
Avg memory 16.7 MiB
CPU user 183.9s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 190.0s
Peak memory 17.7 MiB
Avg memory 16.7 MiB
CPU user 185.2s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@Rich-T-kid Rich-T-kid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jefffrey Jefffrey added the performance Speed or memory optimizations label Sep 3, 2026
@Jefffrey
Jefffrey merged commit 2f03e22 into apache:main Sep 4, 2026
44 of 45 checks passed
@Jefffrey

Jefffrey commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

thanks @neilconway & @Rich-T-kid

Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…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.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-cast performance Speed or memory optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize parse_decimal: specialize loop for pre vs. post decimal points

4 participants