Skip to content

perf(arrow-buffer): Format i256 values without num-bigint - #11000

Merged
Jefffrey merged 2 commits into
apache:mainfrom
neilconway:neilc/i256-display-fast
Sep 6, 2026
Merged

Jefffrey merged 2 commits into
apache:mainfrom
neilconway:neilc/i256-display-fast

Conversation

@neilconway

@neilconway neilconway commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

Display for i256 converted the value to a num-bigint value and formatted that, which allocates the BigInt's digit vector and runs a generic arbitrary-precision conversion.

By formatting the value ourselves, we can do better because we don't need to support arbitrary precision, and we can also avoid the heap allocation and type conversion overhead.

format_decimal benchmark (#10997), Apple M4 Max:

case                              before     after   change
decimal256 (76, 10) 38 digits    1730.13    958.33   -44.6%
decimal256 (76, 10) 76 digits    2088.60   1760.90   -15.7%

The win is bigger for small Decimal256 values because we can do those entirely in i128; in principle num-bigint could implement a similar optimization for small values, but it currently does not. Decimal256 values larger than i128 are split into three chunks and formatted as i128; this is still faster than going through num-bigint.

In practice, this improves the performance of writing out decimal values as CSV and JSON, as well as pretty-printing them.

What changes are included in this PR?

See above.

Are these changes tested?

Yes; new test added, existing tests pass.

Are there any user-facing changes?

No; decimal format is unchanged.

AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex Astra 6. I reviewed, revised, and understand the resulting code.

Display for i256 converted the value to a num-bigint value and formatted
that, which allocates the BigInt's digit vector and runs a generic
arbitrary-precision conversion.

By formatting the value ourselves, we can do better because we don't
need to support arbitrary precision, and we can also avoid the heap
allocation and type conversion overhead.

format_decimal benchmark (apache#10997), Apple M4 Max:

    case                              before     after   change
    decimal256 (76, 10) 38 digits    1730.13    958.33   -44.6%
    decimal256 (76, 10) 76 digits    2088.60   1760.90   -15.7%

The win is bigger for small Decimal256 values because we can do those
entirely in i128; in principle num-bigint could implement a similar
optimization for small values, but it currently does not. Decimal256
values larger than i128 are split into three chunks and formatted as
i128; this is still faster than going through num-bigint.

In practice, this improves the performance of writing out decimal values
as CSV and JSON, as well as pretty-printing them.
@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-buffer labels Sep 5, 2026
@neilconway

Copy link
Copy Markdown
Contributor Author

Note that Display and Debug for i256 ignored formatting flags (#10999); I've preserved that behavior for now, but once this perf fix lands I'll send another PR to honor formatting flags.

@Rich-T-kid

Copy link
Copy Markdown
Contributor

run benchmark cast_kernels
env:
BENCH_FILTER: parse_decimal

@Rich-T-kid

Copy link
Copy Markdown
Contributor

whoops wrong command 😞

@adriangbot

This comment was marked as duplicate.

@adriangbot

This comment was marked as outdated.

@neilconway

Copy link
Copy Markdown
Contributor Author

@Rich-T-kid no benchmarks for formatting decimals until that PR lands, anyway

neilconway added a commit to neilconway/arrow-rs that referenced this pull request Sep 5, 2026
Formatting a decimal value allocated two heap strings: the unscaled
value was converted with to_string, and format_decimal_str then built a
second String with the decimal point inserted.

arrow-data gains three functions that share one implementation of the
formatting rules:

- write_decimal_str (private) takes the digits of an unscaled value,
  with an optional sign, and writes them to a fmt::Write with the decimal
  point inserted `scale` digits from the right, adding leading zeros as
  needed.
- write_decimal is for callers that have an output to write to, such as
  ArrayFormatter: it formats the native value into a stack buffer of
  digits and passes them to write_decimal_str.
- format_decimal is for callers that need an owned String, such as
  DecimalType::format_decimal: it formats the digits the same way, then
  allocates a String of the required capacity and writes into it through
  write_decimal_str.

write_decimal and format_decimal accept the native value of a decimal
type, i32, i64, i128 or i256, named by the new sealed DecimalNativeType
trait. DecimalType already names these types, but it lives in
arrow-array, which depends on arrow-data, so it cannot be used here.

The existing entry points are now implemented on top of write_decimal
and format_decimal. This reduces the number of heap allocations in
ArrayFormatter from 2 -> 0 (which improves performance writing CSV and
JSON output), and from 2 -> 1 for PrimitiveArray::value_as_string.

format_decimal benchmarks, M4 Max:

    case                            before    after   change
    decimal32 (9, 2) 9 digits       593.45   220.13   -62.9%
    decimal64 (18, 6) 18 digits     726.44   243.84   -66.4%
    decimal128 (10, 2) 1 digit      486.70   190.16   -60.9%
    decimal128 (10, 2) 5 digits     480.75   209.84   -56.4%
    decimal128 (38, 10) 38 digits   776.75   303.65   -60.9%
    decimal256 (76, 10) 38 digits  1751.70  1070.30   -38.9%
    decimal256 (76, 10) 76 digits  2097.40  1507.80   -28.1%

The relative improvement for decimal256 is smaller because that case had
additional overhead; that has been addressed in a concurrent
PR (apache#11000).
neilconway added a commit to neilconway/arrow-rs that referenced this pull request Sep 5, 2026
Formatting a decimal value allocated two heap strings: the unscaled
value was converted with to_string, and format_decimal_str then built a
second String with the decimal point inserted.

arrow-data gains three functions that share one implementation of the
formatting rules:

- write_decimal_str (private) takes the digits of an unscaled value,
  with an optional sign, and writes them to a fmt::Write with the decimal
  point inserted `scale` digits from the right, adding leading zeros as
  needed.
- write_decimal is for callers that have an output to write to, such as
  ArrayFormatter: it formats the native value into a stack buffer of
  digits and passes them to write_decimal_str.
- format_decimal is for callers that need an owned String, such as
  DecimalType::format_decimal: it formats the digits the same way, then
  allocates a String of the required capacity and writes into it through
  write_decimal_str.

write_decimal and format_decimal accept the native value of a decimal
type, i32, i64, i128 or i256, named by the new sealed DecimalNativeType
trait. DecimalType already names these types, but it lives in
arrow-array, which depends on arrow-data, so it cannot be used here.

The existing entry points are now implemented on top of write_decimal
and format_decimal. This reduces the number of heap allocations in
ArrayFormatter from 2 -> 0 (which improves performance writing CSV and
JSON output), and from 2 -> 1 for PrimitiveArray::value_as_string.

format_decimal benchmarks, M4 Max:

    case                            before    after   change
    decimal32 (9, 2) 9 digits       593.45   220.13   -62.9%
    decimal64 (18, 6) 18 digits     726.44   243.84   -66.4%
    decimal128 (10, 2) 1 digit      486.70   190.16   -60.9%
    decimal128 (10, 2) 5 digits     480.75   209.84   -56.4%
    decimal128 (38, 10) 38 digits   776.75   303.65   -60.9%
    decimal256 (76, 10) 38 digits  1751.70  1070.30   -38.9%
    decimal256 (76, 10) 76 digits  2097.40  1507.80   -28.1%

The relative improvement for decimal256 is smaller because that case had
additional overhead; that has been addressed in a concurrent
PR (apache#11000).

@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.

this is pretty neat. learning a lot from these decimal related PR's

@Jefffrey

Jefffrey commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

run benchmark format_decimal
env:
BENCH_FILTER: decimal256

@Jefffrey Jefffrey added the performance Speed or memory optimizations label Sep 6, 2026
@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/i256-display-fast (1b2e492) to 738e69c (merge-base) diff

Run configuration
run benchmark format_decimal
env:
  BENCH_FILTER: "decimal256"
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_i256-display-fast
-----                                           ----                                   -----------------------
format_decimal/decimal256/(76, 10) 38 digits    1.80      2.2±0.01ms  3.5 MElem/sec    1.00   1234.9±2.78µs  6.3 MElem/sec
format_decimal/decimal256/(76, 10) 76 digits    1.22      3.1±0.00ms  2.5 MElem/sec    1.00      2.6±0.00ms  3.1 MElem/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 20.0s
Peak memory 9.8 MiB
Avg memory 6.5 MiB
CPU user 17.8s
CPU sys 0.0s
Peak spill 0 B

branch

Metric Value
Wall time 25.0s
Peak memory 10.6 MiB
Avg memory 6.6 MiB
CPU user 21.0s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@Jefffrey
Jefffrey merged commit 37775f3 into apache:main Sep 6, 2026
41 checks passed
@Jefffrey

Jefffrey commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

thanks @neilconway & @Rich-T-kid

some nice numbers here 👀

@neilconway

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! @Jefffrey @Rich-T-kid

@neilconway
neilconway deleted the neilc/i256-display-fast branch September 6, 2026 14:11
alamb pushed a commit that referenced this pull request Sep 17, 2026
…11002)

# Which issue does this PR close?

- N/A

# Rationale for this change

Formatting a decimal value allocated two heap strings: the unscaled
value was converted with `to_string`, and `format_decimal_str` then
built a second String with the decimal point inserted.

arrow-data gains three functions that share one implementation of the
formatting rules:

- `write_decimal_str` (private) takes the digits of an unscaled value,
with an optional sign, and writes them to a `fmt::Write` with the
decimal point inserted `scale` digits from the right, adding leading
zeros as needed.
- `write_decimal` is for callers that have an output to write to, such
as `ArrayFormatter`: it formats the native value into a stack buffer of
digits and passes them to `write_decimal_str`.
- `format_decimal` is for callers that need an owned String, such as
`DecimalType::format_decimal`: it formats the digits the same way, then
allocates a String of the required capacity and writes into it through
`write_decimal_str`.

`write_decimal` and `format_decimal` accept the native value of a
decimal type, i32, i64, i128 or i256, named by the new sealed
`DecimalNativeType` trait. `DecimalType` already names these types, but
it lives in arrow-array, which depends on arrow-data, so it cannot be
used here.

The existing entry points are now implemented on top of write_decimal
and format_decimal. This reduces the number of heap allocations in
`ArrayFormatter` from 2 -> 0 (which improves performance writing CSV and
JSON output), and from 2 -> 1 for `PrimitiveArray::value_as_string`.

`format_decimal` benchmarks, M4 Max:

    case                            before    after   change
    decimal32 (9, 2) 9 digits       593.45   220.13   -62.9%
    decimal64 (18, 6) 18 digits     726.44   243.84   -66.4%
    decimal128 (10, 2) 1 digit      486.70   190.16   -60.9%
    decimal128 (10, 2) 5 digits     480.75   209.84   -56.4%
    decimal128 (38, 10) 38 digits   776.75   303.65   -60.9%
    decimal256 (76, 10) 38 digits  1751.70  1070.30   -38.9%
    decimal256 (76, 10) 76 digits  2097.40  1507.80   -28.1%

The relative improvement for decimal256 is smaller because that case had
additional overhead; that has been addressed in a concurrent PR
(#11000).

# What changes are included in this PR?

See above.

# Are these changes tested?

Yes; existing tests pass, new test added.

# Are there any user-facing changes?

No. Decimal output format is unchanged.

# AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex GPT-6 Astra. I
reviewed, revised, and understand the resulting code.

---------

Co-authored-by: Jeffrey Vo <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
# Which issue does this PR close?

- N/A

# Rationale for this change

Display for i256 converted the value to a num-bigint value and formatted
that, which allocates the BigInt's digit vector and runs a generic
arbitrary-precision conversion.

By formatting the value ourselves, we can do better because we don't
need to support arbitrary precision, and we can also avoid the heap
allocation and type conversion overhead.

format_decimal benchmark (apache#10997), Apple M4 Max:

    case                              before     after   change
    decimal256 (76, 10) 38 digits    1730.13    958.33   -44.6%
    decimal256 (76, 10) 76 digits    2088.60   1760.90   -15.7%

The win is bigger for small Decimal256 values because we can do those
entirely in i128; in principle num-bigint could implement a similar
optimization for small values, but it currently does not. Decimal256
values larger than i128 are split into three chunks and formatted as
i128; this is still faster than going through num-bigint.

In practice, this improves the performance of writing out decimal values
as CSV and JSON, as well as pretty-printing them.

# What changes are included in this PR?

See above.

# Are these changes tested?

Yes; new test added, existing tests pass.

# Are there any user-facing changes?

No; decimal format is unchanged.

# AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex Astra 6. I
reviewed, revised, and understand the resulting code.

Co-authored-by: Jeffrey Vo <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…pache#11002)

# Which issue does this PR close?

- N/A

# Rationale for this change

Formatting a decimal value allocated two heap strings: the unscaled
value was converted with `to_string`, and `format_decimal_str` then
built a second String with the decimal point inserted.

arrow-data gains three functions that share one implementation of the
formatting rules:

- `write_decimal_str` (private) takes the digits of an unscaled value,
with an optional sign, and writes them to a `fmt::Write` with the
decimal point inserted `scale` digits from the right, adding leading
zeros as needed.
- `write_decimal` is for callers that have an output to write to, such
as `ArrayFormatter`: it formats the native value into a stack buffer of
digits and passes them to `write_decimal_str`.
- `format_decimal` is for callers that need an owned String, such as
`DecimalType::format_decimal`: it formats the digits the same way, then
allocates a String of the required capacity and writes into it through
`write_decimal_str`.

`write_decimal` and `format_decimal` accept the native value of a
decimal type, i32, i64, i128 or i256, named by the new sealed
`DecimalNativeType` trait. `DecimalType` already names these types, but
it lives in arrow-array, which depends on arrow-data, so it cannot be
used here.

The existing entry points are now implemented on top of write_decimal
and format_decimal. This reduces the number of heap allocations in
`ArrayFormatter` from 2 -> 0 (which improves performance writing CSV and
JSON output), and from 2 -> 1 for `PrimitiveArray::value_as_string`.

`format_decimal` benchmarks, M4 Max:

    case                            before    after   change
    decimal32 (9, 2) 9 digits       593.45   220.13   -62.9%
    decimal64 (18, 6) 18 digits     726.44   243.84   -66.4%
    decimal128 (10, 2) 1 digit      486.70   190.16   -60.9%
    decimal128 (10, 2) 5 digits     480.75   209.84   -56.4%
    decimal128 (38, 10) 38 digits   776.75   303.65   -60.9%
    decimal256 (76, 10) 38 digits  1751.70  1070.30   -38.9%
    decimal256 (76, 10) 76 digits  2097.40  1507.80   -28.1%

The relative improvement for decimal256 is smaller because that case had
additional overhead; that has been addressed in a concurrent PR
(apache#11000).

# What changes are included in this PR?

See above.

# Are these changes tested?

Yes; existing tests pass, new test added.

# Are there any user-facing changes?

No. Decimal output format is unchanged.

# AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex GPT-6 Astra. I
reviewed, revised, and understand the resulting code.

---------

Co-authored-by: Jeffrey Vo <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
# Which issue does this PR close?

- N/A

# Rationale for this change

Display for i256 converted the value to a num-bigint value and formatted
that, which allocates the BigInt's digit vector and runs a generic
arbitrary-precision conversion.

By formatting the value ourselves, we can do better because we don't
need to support arbitrary precision, and we can also avoid the heap
allocation and type conversion overhead.

format_decimal benchmark (apache#10997), Apple M4 Max:

    case                              before     after   change
    decimal256 (76, 10) 38 digits    1730.13    958.33   -44.6%
    decimal256 (76, 10) 76 digits    2088.60   1760.90   -15.7%

The win is bigger for small Decimal256 values because we can do those
entirely in i128; in principle num-bigint could implement a similar
optimization for small values, but it currently does not. Decimal256
values larger than i128 are split into three chunks and formatted as
i128; this is still faster than going through num-bigint.

In practice, this improves the performance of writing out decimal values
as CSV and JSON, as well as pretty-printing them.

# What changes are included in this PR?

See above.

# Are these changes tested?

Yes; new test added, existing tests pass.

# Are there any user-facing changes?

No; decimal format is unchanged.

# AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex Astra 6. I
reviewed, revised, and understand the resulting code.

Co-authored-by: Jeffrey Vo <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…pache#11002)

# Which issue does this PR close?

- N/A

# Rationale for this change

Formatting a decimal value allocated two heap strings: the unscaled
value was converted with `to_string`, and `format_decimal_str` then
built a second String with the decimal point inserted.

arrow-data gains three functions that share one implementation of the
formatting rules:

- `write_decimal_str` (private) takes the digits of an unscaled value,
with an optional sign, and writes them to a `fmt::Write` with the
decimal point inserted `scale` digits from the right, adding leading
zeros as needed.
- `write_decimal` is for callers that have an output to write to, such
as `ArrayFormatter`: it formats the native value into a stack buffer of
digits and passes them to `write_decimal_str`.
- `format_decimal` is for callers that need an owned String, such as
`DecimalType::format_decimal`: it formats the digits the same way, then
allocates a String of the required capacity and writes into it through
`write_decimal_str`.

`write_decimal` and `format_decimal` accept the native value of a
decimal type, i32, i64, i128 or i256, named by the new sealed
`DecimalNativeType` trait. `DecimalType` already names these types, but
it lives in arrow-array, which depends on arrow-data, so it cannot be
used here.

The existing entry points are now implemented on top of write_decimal
and format_decimal. This reduces the number of heap allocations in
`ArrayFormatter` from 2 -> 0 (which improves performance writing CSV and
JSON output), and from 2 -> 1 for `PrimitiveArray::value_as_string`.

`format_decimal` benchmarks, M4 Max:

    case                            before    after   change
    decimal32 (9, 2) 9 digits       593.45   220.13   -62.9%
    decimal64 (18, 6) 18 digits     726.44   243.84   -66.4%
    decimal128 (10, 2) 1 digit      486.70   190.16   -60.9%
    decimal128 (10, 2) 5 digits     480.75   209.84   -56.4%
    decimal128 (38, 10) 38 digits   776.75   303.65   -60.9%
    decimal256 (76, 10) 38 digits  1751.70  1070.30   -38.9%
    decimal256 (76, 10) 76 digits  2097.40  1507.80   -28.1%

The relative improvement for decimal256 is smaller because that case had
additional overhead; that has been addressed in a concurrent PR
(apache#11000).

# What changes are included in this PR?

See above.

# Are these changes tested?

Yes; existing tests pass, new test added.

# Are there any user-facing changes?

No. Decimal output format is unchanged.

# AI usage

Developed with Claude Code Fable 5.1; reviewed with Codex GPT-6 Astra. I
reviewed, revised, and understand the resulting code.

---------

Co-authored-by: Jeffrey Vo <[email protected]>
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-buffer performance Speed or memory optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants