Repository navigation
perf(arrow-buffer): Format i256 values without num-bigint - #11000
Conversation
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.
|
Note that |
|
run benchmark cast_kernels |
|
whoops wrong command 😞 |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as outdated.
This comment was marked as outdated.
|
@Rich-T-kid no benchmarks for formatting decimals until that PR lands, anyway |
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).
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
left a comment
There was a problem hiding this comment.
this is pretty neat. learning a lot from these decimal related PR's
|
run benchmark format_decimal |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
🤖 Arrow criterion benchmark completed (GKE) | trigger Instance: Comparing neilc/i256-display-fast (1b2e492) to 738e69c (merge-base) diff Run configurationrun benchmark format_decimal
env:
BENCH_FILTER: "decimal256"CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
thanks @neilconway & @Rich-T-kid some nice numbers here 👀 |
|
Thanks for the reviews! @Jefffrey @Rich-T-kid |
…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]>
# 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]>
…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]>
# 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]>
…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]>
Which issue does this PR close?
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:
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.