Skip to content

perf: avoid intermediate slice allocation in Spark slice function#23481

Merged
comphead merged 1 commit into
apache:mainfrom
andygrove:spark-slice-value-length
Jul 11, 2026
Merged

perf: avoid intermediate slice allocation in Spark slice function#23481
comphead merged 1 commit into
apache:mainfrom
andygrove:spark-slice-value-length

Conversation

@andygrove

Copy link
Copy Markdown
Member

Which issue does this PR close?

  • N/A (small performance improvement)

Rationale for this change

The Spark slice function's calculate_start_end helper reads the length of each list element in a per-row hot loop via values.value(row).len(). GenericListArray::value(row) materializes a new ArrayRef for the sublist on every iteration purely to call .len() on it. The length is already available from the offset buffer, so this allocation is pure overhead.

Replacing it with values.value_length(row) reads the length directly from the offsets with no allocation.

Benchmarked with the existing datafusion/spark/benches/slice.rs over 1M rows:

case before after improvement
List(Int64), array args 54.0 ms 40.0 ms ~26% faster
List(Int64), scalar args 88.4 ms 69.8 ms ~21% faster

What changes are included in this PR?

A single-line change in datafusion/spark/src/function/array/slice.rs replacing values.value(row).len() as i64 with values.value_length(row) as i64.

Are these changes tested?

Covered by existing unit tests in datafusion/spark/src/function/array/slice.rs and slt coverage for the Spark slice function; behavior is unchanged. The performance impact was measured with the existing slice criterion benchmark.

Are there any user-facing changes?

No.

Use GenericListArray::value_length(row) instead of value(row).len() to
read the sublist length from the offset buffer directly, avoiding a
per-row ArrayRef allocation in the hot loop of calculate_start_end.
@github-actions github-actions Bot added the spark label Jul 11, 2026
@andygrove andygrove requested review from Jefffrey and comphead July 11, 2026 14:08
@comphead comphead added this pull request to the merge queue Jul 11, 2026
Merged via the queue into apache:main with commit 2fbc758 Jul 11, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants