comphead opened a new pull request, #12371: URL: https://github.com/apache/arrow-rs/pull/12371
# Which issue does this PR close? - Closes https://github.com/apache/arrow-rs/issues/12368 # Rationale for this change `PrimitiveArray::validate_decimal_precision` read the validity bit of every slot and made an out-of-line call to `DecimalType::validate_decimal_precision` for every valid value. In a samply profile of `main` without LTO, for 8192 `Decimal128` values that fit, 91.6% of the samples are self time in that callee, and 66% of all samples are on its prologue and epilogue, which save and restore eight registers. With 10% random nulls, the callee takes 62% and the loop takes 38%. # What changes are included in this PR? - `validate_decimal_precision` scans the values buffer, null slots included, with the inline `is_valid_decimal_precision`. A null slot can hold any value, so it reads a validity bit only at a value that doesn't fit. It returns the same error as before, for the first valid value that doesn't fit. The `unsafe` block goes away. - The first commit adds `decimal_overflow` cases whose values fit precision 10 at scale 2, so `validate_decimal_precision(10)` reads every row: `fits`, `fits nulls` (10% random nulls), `null slots overflow` (the same nulls, each holding a value that doesn't fit) and `last row overflow`, for `Decimal128` and `Decimal256`. The existing cases return at the precision-below-scale check before they read a value. They stay as they are. # Are these changes tested? `test_decimal_validate_precision_ignores_null_slots` covers null slots that hold values that don't fit, including one before the first valid value that doesn't fit, and sliced arrays, for all four decimal types. It passes against the previous implementation and fails on one that also checks null slots. `decimal_overflow` on linux/arm64, a 4-CPU VM on an Apple M5 Max, with the bot's toolchain, features and thin LTO. It used a local variant of the Docker setup in https://github.com/apache/arrow-rs/pull/12351 that takes both trees from the build context, because the baseline needs the new cases. `base` is the first commit: ``` group base changed ----- ---- ------- null_if_overflow_precision_128 1.00 4.1±0.05µs ? ?/sec 1.00 4.1±0.06µs ? ?/sec null_if_overflow_precision_256 1.00 7.4±0.09µs ? ?/sec 1.01 7.5±0.09µs ? ?/sec validate_decimal_precision_128 1.00 22.1±0.38ns ? ?/sec 1.00 22.1±0.47ns ? ?/sec validate_decimal_precision_128 fits 3.39 11.1±0.24µs ? ?/sec 1.00 3.3±0.04µs ? ?/sec validate_decimal_precision_128 fits nulls 3.86 14.6±0.10µs ? ?/sec 1.00 3.8±0.04µs ? ?/sec validate_decimal_precision_128 last row overflow 3.35 11.3±0.08µs ? ?/sec 1.00 3.4±0.07µs ? ?/sec validate_decimal_precision_128 null slots overflow 2.03 14.6±0.25µs ? ?/sec 1.00 7.2±0.21µs ? ?/sec validate_decimal_precision_256 1.03 22.7±0.47ns ? ?/sec 1.00 22.0±0.33ns ? ?/sec validate_decimal_precision_256 fits 2.16 13.2±0.12µs ? ?/sec 1.00 6.1±0.09µs ? ?/sec validate_decimal_precision_256 fits nulls 2.86 17.3±0.15µs ? ?/sec 1.00 6.1±0.07µs ? ?/sec validate_decimal_precision_256 last row overflow 2.15 13.4±0.17µs ? ?/sec 1.00 6.2±0.09µs ? ?/sec validate_decimal_precision_256 null slots overflow 1.76 17.3±0.13µs ? ?/sec 1.00 9.8±0.15µs ? ?/sec ``` To see which part produces the gain, I compared against `#[inline]` on the four `DecimalType::validate_decimal_precision` implementations, the alternative in the issue. macOS aarch64, no LTO, median of three alternating rounds: | Case | `main` | `#[inline]` only | This PR | | --- | ---: | ---: | ---: | | 128 fits | 10.94 µs | 3.21 µs | 3.23 µs | | 128 fits nulls | 14.44 µs | 7.22 µs | 3.23 µs | | 128 null slots overflow | 14.47 µs | 7.27 µs | 6.75 µs | | 256 fits | 13.36 µs | 6.10 µs | 6.08 µs | | 256 fits nulls | 17.69 µs | 7.06 µs | 5.99 µs | | 256 null slots overflow | 17.92 µs | 6.92 µs | 9.73 µs | Without nulls, all of the gain comes from removing the call. With nulls, skipping the per-slot validity read makes `Decimal128` another 2.2x faster. Inputs that get slower: arrays where many null slots hold values that don't fit, because the scan takes its slow path at each of them. In the same setup, at precision 8: | Case | `main` | This PR | | --- | ---: | ---: | | 128, 50% nulls that don't fit | 8.71 µs | 14.58 µs | | 128, 90% nulls that don't fit | 3.47 µs | 8.03 µs | | 128, all null, none fit | 2.79 µs | 4.43 µs | | 256, 90% nulls that don't fit | 3.85 µs | 10.66 µs | | 256, all null, none fit | 2.79 µs | 7.17 µs | The builders, `unary_opt` (and so `null_if_overflow_precision`) and `try_unary` write zeros into null slots. This shape needs a values buffer kept under a new null mask, for example from `nullif` or `PrimitiveArray::new`, with many of those slots out of range. With zeros in the null slots, 50% nulls is 2.6x faster (8.70 µs to 3.30 µs). `Decimal32` and `Decimal64` are about 5x faster without nulls and 7x faster with 10% nulls. # Are there any significant user-facing changes? No. # AI usage disclosure Claude Code wrote the change, the test and the bench cases, profiled with samply, and ran the benchmarks. I reviewed the changes and the results. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
