Skip to content

perf: optimize spark_cast_float64_to_utf8 (~40% faster)#4918

Open
andygrove wants to merge 1 commit into
apache:mainfrom
andygrove:auto-opt/spark_cast_float64_to_utf8-datafusion-comet-20260714-045616
Open

perf: optimize spark_cast_float64_to_utf8 (~40% faster)#4918
andygrove wants to merge 1 commit into
apache:mainfrom
andygrove:auto-opt/spark_cast_float64_to_utf8-datafusion-comet-20260714-045616

Conversation

@andygrove

@andygrove andygrove commented Jul 14, 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?

Format float-to-string casts straight into a GenericStringBuilder with a reused scratch buffer instead of allocating a String per row (and a second one in the scientific-notation path), removing one malloc/free and one copy per value.

How are these changes tested?

Existing tests.

Benchmark (criterion):

  • cast_f32_to_utf8: 42.453% faster (base 408097ns -> cand 234847ns)
  • cast_f64_to_utf8: 41.57% faster (base 418096ns -> cand 244294ns)

Full criterion output:

cast_float_to_string/cast_f64_to_utf8
                        time:   [243.85 µs 244.03 µs 244.23 µs]
                        change: [−41.767% −41.570% −41.425%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
cast_float_to_string/cast_f32_to_utf8
                        time:   [234.71 µs 234.86 µs 235.08 µs]
                        change: [−42.515% −42.453% −42.390%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  2 (2.00%) low mild
  1 (1.00%) high mild
  3 (3.00%) high severe

@andygrove andygrove changed the title perf: optimize spark_cast_float64_to_utf8 in datafusion-comet-spark-expr perf: optimize spark_cast_float64_to_utf8 (~40% faster) Jul 14, 2026
@andygrove andygrove marked this pull request as ready for review July 14, 2026 14:42
@andygrove andygrove requested a review from coderfender July 14, 2026 14:42

@mbutrovich mbutrovich 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.

First pass, thanks @andygrove!

// is allocated per row.
let mut builder = GenericStringBuilder::<OffsetSize>::with_capacity(
array.len(),
array.len() * 8,

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.

GenericStringBuilder::<OffsetSize>::with_capacity(array.len(), array.len() * 8). Eight bytes
per value under-provisions typical fractional output like 1234.5678 (9 bytes) or the
scientific forms this cast produces (4.9E-324 is 8, -1.4E-45 is 8, longer with more
mantissa digits), so the value buffer will still reallocate on realistic data. arrow-rs itself
uses an AVERAGE_STRING_LENGTH of 16 for its own capacity helper
(arrow-array/src/builder/generic_bytes_builder.rs:376).

Suggested change: raise the hint to array.len() * 16 to match arrow-rs's own average and
avoid mid-loop growth on ordinary float text. The cost of a slightly large preallocation is
negligible per that same arrow-rs doc note.

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