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


##########
native/spark-expr/benches/check_overflow.rs:
##########
@@ -85,6 +97,41 @@ fn criterion_benchmark(c: &mut Criterion) {
     c.bench_function("check_overflow: ansi no overflow", |b| {
         b.iter(|| black_box(ansi.evaluate(black_box(&no_overflow)).unwrap()))
     });
+    c.bench_function("check_overflow: ansi no overflow, with nulls", |b| {
+        b.iter(|| 
black_box(ansi.evaluate(black_box(&no_overflow_nulls)).unwrap()))
+    });
+
+    // One overflow in the last row, so the scan reads the whole batch before 
the slow path runs.
+    // `sparse overflow` and `dense overflow` overflow at row 0 and stop in 
the first chunk.

Review Comment:
   Applied. Both shapes have no nulls and put a value that doesn't fit in row 
0, so `fits_precision` returns there.
   



##########
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 {

Review Comment:
   Filed apache/arrow-rs#12368 and linked it from `fits_precision`. I 
prototyped the port on arrow-rs `main` first, which already includes 
apache/arrow-rs#12342. For 8192 `Decimal128` values that fit, 
`validate_decimal_precision` takes 11.3 µs and the scan 3.4 µs, or 2.9 µs with 
the shifted compare. `#[inline]` on the `validate_decimal_precision` 
implementations alone matches the scan without nulls but not with them, so the 
issue lists it as the alternative. DataFusion calls 
`validate_decimal_precision` only from test code, so outside Comet the faster 
check helps direct callers of the API.
   



##########
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:
   Thanks for checking x86-64 and the released binary. The description now 
cites your numbers, credited to you, and still says x86-64 timing is not 
measured. apache/arrow-rs#12342 removes that per-value call once Comet's arrow 
includes it, so I also timed `main`'s loop built against arrow-rs `main` with 
that change and no LTO. The scan is still 14% to 42% faster, and those numbers 
are in the description too.
   



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