Skip to content

perf: optimize try_arithmetic_kernel (up to 4x faster)#4910

Merged
andygrove merged 2 commits into
apache:mainfrom
andygrove:auto-opt/try_arithmetic_kernel-datafusion-comet-20260713-102905
Jul 15, 2026
Merged

perf: optimize try_arithmetic_kernel (up to 4x faster)#4910
andygrove merged 2 commits into
apache:mainfrom
andygrove:auto-opt/try_arithmetic_kernel-datafusion-comet-20260713-102905

Conversation

@andygrove

@andygrove andygrove commented Jul 13, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

N/A

Rationale for this change

Optimize existing expression.

What changes are included in this PR?

Replaced four builder-based row-by-row loops with a single generic kernel that reads operands from the raw value slices and unions the null buffers once, removing the per-element null probes and PrimitiveBuilder null-buffer appends from the hot loop.

How are these changes tested?

Existing tests + new unit tests.

Benchmark (criterion):

  • checked_sub_i64_ansi_with_nulls: 68.147% faster (base 18814ns -> cand 5992ns)
  • checked_sub_i64_ansi_no_nulls: 61.969% faster (base 12122ns -> cand 4610ns)
  • checked_div_f64_ansi_with_nulls: 70.188% faster (base 19442ns -> cand 5796ns)
  • checked_add_i32_ansi_no_nulls: 70.466% faster (base 11532ns -> cand 3405ns)
  • checked_add_i32_ansi_with_nulls: 74.668% faster (base 19018ns -> cand 4817ns)
  • checked_mul_i64_ansi_with_nulls: 62.761% faster (base 18956ns -> cand 7059ns)
  • checked_add_i32_try_with_nulls: 39.008% faster (base 18674ns -> cand 11389ns)
  • checked_add_i32_try_no_nulls: 26.572% faster (base 5111ns -> cand 3753ns)
  • checked_div_f64_ansi_no_nulls: 78.247% faster (base 15097ns -> cand 3284ns)
  • checked_mul_i64_ansi_no_nulls: 53.602% faster (base 11030ns -> cand 5117ns)

Full criterion output:

checked_arithmetic/checked_add_i32_ansi_no_nulls
                        time:   [3.3873 µs 3.3965 µs 3.4073 µs]
                        change: [−70.767% −70.466% −70.257%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
checked_arithmetic/checked_add_i32_try_no_nulls
                        time:   [3.7449 µs 3.7522 µs 3.7602 µs]
                        change: [−27.302% −26.572% −25.955%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  7 (7.00%) high mild
  5 (5.00%) high severe
checked_arithmetic/checked_sub_i64_ansi_no_nulls
                        time:   [4.6115 µs 4.6161 µs 4.6215 µs]
                        change: [−62.733% −61.969% −61.301%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  5 (5.00%) low mild
  4 (4.00%) high mild
  1 (1.00%) high severe
checked_arithmetic/checked_mul_i64_ansi_no_nulls
                        time:   [4.9912 µs 5.0081 µs 5.0288 µs]
                        change: [−54.837% −53.602% −52.449%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe
checked_arithmetic/checked_div_f64_ansi_no_nulls
                        time:   [3.2848 µs 3.2979 µs 3.3125 µs]
                        change: [−78.363% −78.247% −78.135%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) high mild
  4 (4.00%) high severe
checked_arithmetic/checked_add_i32_ansi_with_nulls
                        time:   [4.7731 µs 4.7846 µs 4.7987 µs]
                        change: [−74.790% −74.668% −74.551%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild
checked_arithmetic/checked_add_i32_try_with_nulls
                        time:   [11.292 µs 11.396 µs 11.507 µs]
                        change: [−39.443% −39.008% −38.567%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
checked_arithmetic/checked_sub_i64_ansi_with_nulls
                        time:   [6.0276 µs 6.0472 µs 6.0646 µs]
                        change: [−68.222% −68.147% −68.067%] (p = 0.00 < 0.05)
                        Performance has improved.
checked_arithmetic/checked_mul_i64_ansi_with_nulls
                        time:   [6.9766 µs 6.9971 µs 7.0202 µs]
                        change: [−62.881% −62.761% −62.647%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild
checked_arithmetic/checked_div_f64_ansi_with_nulls
                        time:   [5.7559 µs 5.7662 µs 5.7790 µs]
                        change: [−70.608% −70.188% −69.860%] (p = 0.00 < 0.05)
                        Performance has improved.

@andygrove
andygrove marked this pull request as ready for review July 13, 2026 17:54
@andygrove andygrove changed the title perf: optimize try_arithmetic_kernel in datafusion-comet-spark-expr perf: optimize try_arithmetic_kernel (up to 4x faster) Jul 13, 2026
@andygrove
andygrove requested a review from coderfender July 13, 2026 17:54

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @andygrove

@andygrove
andygrove merged commit 078fed3 into apache:main Jul 15, 2026
2 checks passed
@andygrove
andygrove deleted the auto-opt/try_arithmetic_kernel-datafusion-comet-20260713-102905 branch July 15, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants