perf: avoid intermediate slice allocation in Spark slice function#23481
Merged
Conversation
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.
Jefffrey
approved these changes
Jul 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
The Spark
slicefunction'scalculate_start_endhelper reads the length of each list element in a per-row hot loop viavalues.value(row).len().GenericListArray::value(row)materializes a newArrayReffor 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.rsover 1M rows:What changes are included in this PR?
A single-line change in
datafusion/spark/src/function/array/slice.rsreplacingvalues.value(row).len() as i64withvalues.value_length(row) as i64.Are these changes tested?
Covered by existing unit tests in
datafusion/spark/src/function/array/slice.rsand slt coverage for the Sparkslicefunction; behavior is unchanged. The performance impact was measured with the existingslicecriterion benchmark.Are there any user-facing changes?
No.