comphead opened a new issue, #12326:
URL: https://github.com/apache/arrow-rs/issues/12326

   ### Is your feature request related to a problem or challenge?
   
   `DecimalType::is_valid_decimal_precision` is a bounds check that callers run 
once per value. Its implementations for `Decimal32Type`, `Decimal64Type`, 
`Decimal128Type`, and `Decimal256Type` are not `#[inline]` ([`types.rs` 
L1493](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-array/src/types.rs#L1493-L1495),
 
[L1525](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-array/src/types.rs#L1525-L1527),
 
[L1557](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-array/src/types.rs#L1557-L1559),
 
[L1589](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-array/src/types.rs#L1589-L1591)).
 They are not generic, so a caller in another crate depends on LTO to inline 
them. The arrow-data functions they wrap, such as 
`is_validate_decimal_precision`, are already `#[inline]` ([`decimal.rs` 
L1031](https://github.com/apache/arrow-rs/blob/eb62e55752b8376
 bcedc926e2e37547828ec82f9/arrow-data/src/decimal.rs#L1031-L1032)).
   
   In DataFusion Comet the call stays out of line even in release builds with 
thin LTO. The grouped decimal SUM and AVG accumulators call `<Decimal128Type as 
DecimalType>::is_valid_decimal_precision` on every row, both in the published 
1.2.0 snapshot builds of `libcomet` for linux/amd64 and linux/aarch64, and in a 
macOS aarch64 release build. From `SumDecimalGroupsAccumulator::update_single` 
in linux/amd64:
   
   ```
   callq  *0x4dab27c(%rip)   # 0x6d5e9a0, a GOT slot holding <Decimal128Type as 
DecimalType>::is_valid_decimal_precision
   ```
   
   In a TPC-H Q1 profile that function took 11.6% of CPU samples. In a 
criterion bench, where the call is otherwise inlined, forcing it out of line 
made those accumulators 10% to 37% slower (apache/datafusion-comet#7660). Comet 
now looks up the bounds itself (apache/datafusion-comet#7674).
   
   arrow-cast makes the same cross-crate call per value in its decimal casts 
([`cast/decimal.rs` 
L269](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-cast/src/cast/decimal.rs#L269),
 
[L354](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-cast/src/cast/decimal.rs#L354),
 
[L998](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-cast/src/cast/decimal.rs#L998),
 
[L1033](https://github.com/apache/arrow-rs/blob/eb62e55752b8376bcedc926e2e37547828ec82f9/arrow-cast/src/cast/decimal.rs#L1033)).
 I have not measured those.
   
   ### Describe the solution you'd like
   
   Add `#[inline]` to the four `is_valid_decimal_precision` implementations.
   
   ### Describe alternatives you've considered
   
   A caller can look up `MIN_DECIMAL128_FOR_EACH_PRECISION` and 
`MAX_DECIMAL128_FOR_EACH_PRECISION` once and compare against them, as Comet now 
does. That works, but every caller repeats the check.
   


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

Reply via email to