Skip to content

Add ArrayData::sliced to avoid allocations when slicing owned ArrayData - #11433

Merged
alamb merged 2 commits into
apache:mainfrom
memosmorgan:fix/9140-owned-array-data-slice
Oct 9, 2026
Merged

alamb merged 2 commits into
apache:mainfrom
memosmorgan:fix/9140-owned-array-data-slice

Conversation

@memosmorgan

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Replaces #11432.

Rationale for this change

Owned ArrayData can be sliced without allocating new buffer or child vectors.

AI assistance: OpenAI Codex generated the implementation and tests, ran the local checks, and reviewed the diff.

What changes are included in this PR?

Add ArrayData::sliced, which consumes the array and reuses its allocations, including nested struct children. Use it when converting owned data to StructArray.

Are these changes tested?

Regression tests cover equivalence with borrowed slicing, offsets, nested structs, lists, nulls, empty/end slices, and bounds. They also check that vector allocations are retained.

A local release-mode allocation counter measured the following for 1,024-element inputs sliced at offset 1 for 512 elements, excluding input construction:

Data slice allocations sliced allocations
Int32 1 0
Nested Struct 3 0
List 3 0

Are there any significant user-facing changes?

No breaking changes. Borrowed slice retains its existing behavior.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-data arrow-array labels Oct 7, 2026

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

Thank you @memosmorgan -- this looks good to me

Comment thread arrow-data/src/data.rs
///
/// Panics if `offset + length` overflows or is greater than `self.len()`.
pub fn slice(&self, offset: usize, length: usize) -> ArrayData {
self.clone().sliced(offset, length)

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.

it is pretty minor but this could call slice_in_place and skip a function call in sliced

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.

We can always do this as a follow on PR too

@alamb alamb changed the title Avoid allocations when slicing owned ArrayData Add ArrayData::sliced to avoid allocations when slicing owned ArrayData Oct 8, 2026
@alamb
alamb added this pull request to the merge queue Oct 9, 2026
Merged via the queue into apache:main with commit fa96419 Oct 9, 2026
36 checks passed
@Jefffrey Jefffrey added the enhancement New feature/addition label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-array arrow-data enhancement New feature/addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API to avoid allocations when using ArrayData::slice() on owned ArrayData

3 participants