mbutrovich commented on code in PR #7658:
URL: https://github.com/apache/datafusion-comet/pull/7658#discussion_r4634240039


##########
native/spark-expr/src/math_funcs/internal/checkoverflow.rs:
##########
@@ -249,6 +248,38 @@ impl PhysicalExpr for CheckOverflow {
     }
 }
 
+/// Values per chunk in the no-null scan of [`fits_precision`].
+const FITS_PRECISION_CHUNK: usize = 64;
+
+/// Whether every non-null value of `array` fits `precision`, the check that
+/// `Decimal128Type::is_valid_decimal_precision` makes for one value.
+#[inline]
+fn fits_precision(array: &Decimal128Array, precision: u8) -> bool {
+    let bounds = (
+        MIN_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+        MAX_DECIMAL128_FOR_EACH_PRECISION.get(precision as usize),
+    );
+    match bounds {
+        (Some(&min), Some(&max)) if array.null_count() == 0 => {
+            // Without nulls every slot holds a value, so scan the values 
buffer directly instead
+            // of reading the null bitmap per slot. Shifting by `min` turns 
the two bounds checks
+            // into one unsigned comparison. The check is branchless within a 
chunk, which lets
+            // the compiler unroll it, and the scan still stops at the first 
chunk that holds an
+            // overflow.
+            let range = max.wrapping_sub(min) as u128;
+            array.values().chunks(FITS_PRECISION_CHUNK).all(|chunk| {
+                chunk.iter().fold(true, |fits, &v| {
+                    fits & (v.wrapping_sub(min) as u128 <= range)
+                })
+            })

Review Comment:
   > I don't have an x86-64 machine, so that side is unmeasured.
   
   The x86-64 codegen gives no reason to expect a smaller gain than on aarch64, 
though x86-64 timing is still unmeasured. I cross-compiled this PR and `main` 
for `x86_64-apple-darwin` with the `release` profile and LTO off, which is how 
`libcomet` builds (apache/arrow-rs#12326).
   
   This PR's scan has the same shape on x86-64 as on aarch64. It is a scalar 
loop of 12 instructions per value with no stack loads or stores. Its one 
data-dependent branch leaves the loop only at a value that doesn't fit, so it 
is predicted correctly until then.
   
   `main`'s loop in the same build costs about three times as many instructions 
per value. It doesn't inline `is_valid_decimal_precision`, so every value pays 
for a call into a 21-instruction function, on top of 13 instructions in the 
loop. The call also clobbers the registers that hold the array and the 
precision, so the loop reloads them from the stack on every value:
   
   ```asm
   mov   rax, qword ptr [rbp - 64]       # reload the array from the stack
   mov   rax, qword ptr [rax + 32]       # its values pointer
   mov   rdi, qword ptr [rax + r13]      # low half of v
   mov   rsi, qword ptr [rax + r13 + 8]  # high half
   mov   edx, dword ptr [rbp - 80]       # reload the precision from the stack
   call  <Decimal128Type as DecimalType>::is_valid_decimal_precision
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to