Skip to content

perf(arrow-cast): bench Int64 to temporal - #12318

Merged
alamb merged 5 commits into
apache:mainfrom
massive-com:reinterpret-int64-temporal
Nov 27, 2026
Merged

alamb merged 5 commits into
apache:mainfrom
massive-com:reinterpret-int64-temporal

Conversation

@MassivePizza

@MassivePizza MassivePizza commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

make_array(array.to_data()) is a bit expensive (allocates buffer list, validates, drops).

What changes are included in this PR?

  • Special-case i64 casts to avoid ArrayData.
  • Use Array::slice instead of make_array(array.to_data) superseded by Define and use Array::to_arc #12420
  • Two new benches and a test for i64 duration cast.

Are these changes tested?

bench main f626d10 change
cast i64 to timestamp_ns 512 129.2 ns 36.2 ns −72%
cast i64 to duration_ns 512 119.2 ns 33.1 ns −72%

(the array length does not matter, the overhead is per array cast)
The bench applies within 5% after #12420.

Are there any significant user-facing changes?

No.

@boring-cyborg boring-cyborg Bot added arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) labels Nov 18, 2026
@MassivePizza MassivePizza changed the title cast Int64 to temporal without ArrayData overhead perf(arrow-cast): Int64 to temporal without ArrayData overhead Nov 18, 2026
@MassivePizza
MassivePizza marked this pull request as ready for review November 18, 2026 16:59

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

is it because of the root cause here?

// clone array if types are the same
if from_type == to_type {
return Ok(make_array(array.to_data()));
}

would it be feasible to change it to something like

    // clone array if types are the same
    if from_type == to_type {
        return Ok(array.slice(0, array.len()));
    }

to avoid the arraydata roundtrip and avoid needing to specialize like this PR does, or would it still be noticeably slower?

@MassivePizza

Copy link
Copy Markdown
Contributor Author

Yep, make_array(array.to_data()) is not ideal.
Tested slicing and I'm a little disappointed with it on i64->temporal, but slicing improves like 5 other casts so I'd like to keep both.

bench main PR change
cast i64 to timestamp_ns 512 129.2 ns 63.7 ns −51%
cast i64 to duration_ns 512 119.2 ns 65.9 ns −45%

Also found a possible improvement for NullBuffer::slice while checking this.

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

could you elaborate on why these specific ones are being special cased? i can see the performance benefits, but it does open a wider question of if we should do these for more conversions (which can lead to more bloat)

a more general solution would be to figure a way to recover ArrayRef from &dyn Array, and there was some prior art for it here:

but i guess it stalled. cc @scovich

Comment thread arrow-cast/tests/cast/interval.rs Resolved
Comment thread arrow-cast/src/cast/mod.rs Resolved
@MassivePizza

Copy link
Copy Markdown
Contributor Author

This path was being hit when using ptars (protobuf to arrow), which often casts i64 to timestamp. This only showed up on profiles of small batches, and the extra arms felt like the cheapest fix. Maybe you're right that it's unfair to add arms for this but not other cases :)

I also actually attempted to add Array::to_arc(&self) -> Arc<dyn Array>, initially thinking it was pointless, but didn't realize that cast(&dyn Array) could call &Arc<dyn Array> (I thought Arc would deref into the inner dyn Array).
I don't see what's blocking Array::to_arc, so I added TODO above the slice.

to_arc (just cloning) should also be slightly faster than slice as slicing NullBuffer does a popcount, and I didn't find any good way to avoid that without regressing all NullBuffer::slice calls.

@MassivePizza
MassivePizza force-pushed the reinterpret-int64-temporal branch 2 times, most recently from 9a1f97e to d567ea7 Compare November 20, 2026 22:34

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

Thanks @MassivePizza this seems like an improvement to me, though I think we could avoid the need for more code and speed up other cases with some more fanciness

The more I see this the more I am thinking the to_arc() API would help a lot to avoid calling make_array 🤔

Comment thread arrow-cast/src/cast/mod.rs Outdated
// clone array if types are the same
if from_type == to_type {
return Ok(make_array(array.to_data()));
// TODO: Array::as_arc? https://github.com/apache/arrow-rs/issues/8794#issuecomment-3563886963

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.

that would be nice 🤔

@MassivePizza MassivePizza Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think to_arc would also bring the time down since slicing recounts bits in NullBuffer::slice (see #12318 (comment), which seems to be the source of 60ns vs 30ns diff).

Comment thread arrow-cast/src/cast/mod.rs Resolved
@Jefffrey Jefffrey added the performance Speed or memory optimizations label Nov 22, 2026

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

fine with these new specific arms, but would be good in a followup to see if we can make it more generic (and hopefully remove these arms if so)

@MassivePizza MassivePizza changed the title perf(arrow-cast): Int64 to temporal without ArrayData overhead [X-4561] perf(arrow-cast): Int64 to temporal without ArrayData overhead Nov 23, 2026
@MassivePizza MassivePizza changed the title [X-4561] perf(arrow-cast): Int64 to temporal without ArrayData overhead perf(arrow-cast): Int64 to temporal without ArrayData overhead Nov 23, 2026
@scovich

scovich commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

a more general solution would be to figure a way to recover ArrayRef from &dyn Array, and there was some prior art for it here... but I guess it stalled

The more I see this the more I am thinking the to_arc() API would help a lot to avoid calling make_array 🤔

to_arc (just cloning) should also be slightly faster than slice as slicing NullBuffer does a popcount, and I didn't find any good way to avoid that without regressing all NullBuffer::slice calls.

I was trying to actually recover the Arc from a &dyn Array, but that's just not possible in the common case (**). If the goal is merely to specialize Array::slice as Array::to_arc, that should be quite straightforward (***). I'd be tempted to call itArray::clone_as_arc since that's what it actually does, but that doesn't roll off the tongue as nicely as to_arc and the other to_foo helpers already set a precedent for simpler naming.

(**) Of course, impl Array for ArrayRef should return Arc::clone(self), and impl<T: Array> Array for &T should forward to underlying... but most call sites really do pass ArrayRef::as_ref() and there's no way to recover the original arc.

(***) I didn't explore the possibility of Array::slice as a starting point before.

@scovich

scovich commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

I didn't explore the possibility of Array::slice as a starting point before

A few observation as I dust off memories:

  1. No way to downcast Arc<dyn Array> to Arc<T: Array> #8794 is specifically trying to recover an Arc<T: Array> from Arc<dyn Array> without cloning (a downcast of sorts), which is a different problem than Array::to_arc would solve.
  2. If we're ok with cloning, then we don't even need to start from Array::slice. Every in-tree T: Array is also T: Clone, so to_arc for all the concrete array types is just Arc::new(self.clone()). If we wanted a provided to_arc so the new method isn't a breaking change, that would have to delegate to Array::slice because we don't require Array: Clone.

@scovich

scovich commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@alamb

alamb commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I didn't explore the possibility of Array::slice as a starting point before

A few observation as I dust off memories:

  1. No way to downcast Arc to Arc<T: Array> #8794 is specifically trying to recover an Arc<T: Array> from Arc<dyn Array> without cloning (a downcast of sorts), which is a different problem than Array::to_arc would solve.
  2. If we're ok with cloning, then we don't even need to start from Array::slice. Every in-tree T: Array is also T: Clone, so to_arc for all the concrete array types is just Arc::new(self.clone()). If we wanted a provided to_arc so the new method isn't a breaking change, that would have to delegate to Array::slice because we don't require Array: Clone.

I was mostly thinking that the key usecase is a function that takes &dyn Array or &dyn Datum as input but whose signature must return an Arc<&dyn Array> (aka ArrayRef) as is the case here. Ideally that process would only require a single Arc increment rather than slice or cloneing

@scovich

scovich commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I didn't explore the possibility of Array::slice as a starting point before

A few observation as I dust off memories:

  1. No way to downcast Arc to Arc<T: Array> #8794 is specifically trying to recover an Arc<T: Array> from Arc<dyn Array> without cloning (a downcast of sorts), which is a different problem than Array::to_arc would solve.
  2. If we're ok with cloning, then we don't even need to start from Array::slice. Every in-tree T: Array is also T: Clone, so to_arc for all the concrete array types is just Arc::new(self.clone()). If we wanted a provided to_arc so the new method isn't a breaking change, that would have to delegate to Array::slice because we don't require Array: Clone.

I was mostly thinking that the key usecase is a function that takes &dyn Array or &dyn Datum as input but whose signature must return an Arc<&dyn Array> (aka ArrayRef) as is the case here. Ideally that process would only require a single Arc increment rather than slice or cloneing

Yeah, sadly I don't think there's any safe rust way of doing that. Once Arc<T>::as_ref has produced a &T, there's no going back. Even if we used unsafe code, miri would freak out because the memory region tracking would be off. And all that ignores the glaring issue that it's completely wild west if the user provided an actual &T that wasn't backed by an Arc in the first place. The closest we could do is have impl Array for Arc<dyn Array> return Arc::clone(self).

@alamb

alamb commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Yeah, sadly I don't think there's any safe rust way of doing that. Once Arc<T>::as_ref has produced a &T, there's no going back.

To be clear I am not advocating for anything crazy / unsafe

@alamb

alamb commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@MassivePizza if we merge #12420 from @scovich do you think we can avoid the special case code match arms in this PR?

mkleen pushed a commit to mkleen/arrow-rs that referenced this pull request Nov 25, 2026
# Which issue does this PR close?

- Related to apache#12318 and apache#8794

# Rationale for this change

There's no really clean way to clone a `&dyn Array`. The two most common
workarounds are:
* `make_array(array.to_data())`
* `array.slice(0, array.len())`

The latter is cheaper than the former, but both do extra work and both
are roundabout ways of obtaining a shallow copy of an array.

# What changes are included in this PR?

New `Array::to_arc` method that returns a shallow clone of its receiver:
* Provided impl is `self.slice(0, self.len())`, to avoid a breaking
change
* Concrete `T: Array` does `Arc::new(self.clone())`
* `ArrayRef` does `Arc::clone(self)`
* Wrapper types (`&T`, `TypedDictionaryArray`, `TypedRunArray`) forward
to the underlying type

Several call sites that were relying on workarounds are updated to use
the new method.

# Are these changes tested?

A new unit test validates each flavor of implementation. I'm on the
fence about how much value it really adds, but it's only ~20 LoC so I
kept it.

# Are there any significant user-facing changes?

See above.

# AI usage disclosure

This PR is a significantly cleaned up version of a vibe-coded rough
draft.
The PR description is mine alone, and I can answer for every line of the
diff.
@MassivePizza
MassivePizza force-pushed the reinterpret-int64-temporal branch from 816c307 to a0c861a Compare November 25, 2026 12:15
@MassivePizza MassivePizza changed the title perf(arrow-cast): Int64 to temporal without ArrayData overhead perf(arrow-cast): bench Int64 to temporal Nov 25, 2026
@MassivePizza

Copy link
Copy Markdown
Contributor Author

Just benched to_arc and it's only 5% slower (38ns), which is very nice. Rebased and removed the special cases 🚀

@Jefffrey

Copy link
Copy Markdown
Contributor

in that case, do we still need this PR?

@alamb
alamb added this pull request to the merge queue Nov 27, 2026
@alamb

alamb commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR is updated to be a new benchmark, so merging it in.

Thanks for pushing on this @MassivePizza and @scovich -- I think the result is better than the original

Merged via the queue into apache:main with commit aad0067 Nov 27, 2026
21 of 22 checks passed
@MassivePizza
MassivePizza deleted the reinterpret-int64-temporal branch November 27, 2026 23:06
@Jefffrey Jefffrey added development-process Internal changes; PRs with this label are excluded from changelog and removed performance Speed or memory optimizations labels Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) development-process Internal changes; PRs with this label are excluded from changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants