Repository navigation
perf(arrow-cast): bench Int64 to temporal - #12318
Conversation
Jefffrey
left a comment
There was a problem hiding this comment.
is it because of the root cause here?
arrow-rs/arrow-cast/src/cast/mod.rs
Lines 631 to 634 in 44a4bdb
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?
|
Yep,
Also found a possible improvement for |
Jefffrey
left a comment
There was a problem hiding this comment.
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:
- #9295
- #8794 (comment)
- this comment explicitly called out this cast usage too
but i guess it stalled. cc @scovich
|
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
|
9a1f97e to
d567ea7
Compare
alamb
left a comment
There was a problem hiding this comment.
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 🤔
| // 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 |
There was a problem hiding this comment.
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).
Jefffrey
left a comment
There was a problem hiding this comment.
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)
I was trying to actually recover the Arc from a (**) Of course, (***) I didn't explore the possibility of |
A few observation as I dust off memories:
|
I was mostly thinking that the key usecase is a function that takes |
Yeah, sadly I don't think there's any safe rust way of doing that. Once |
To be clear I am not advocating for anything crazy / |
|
@MassivePizza if we merge #12420 from @scovich do you think we can avoid the special case code match arms in this PR? |
# 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.
816c307 to
a0c861a
Compare
|
Just benched |
|
in that case, do we still need this PR? |
|
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 |
Which issue does this PR close?
finishinstead of throughArrayData#11717Rationale 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 avoidArrayData.Usesuperseded by Define and useArray::sliceinstead ofmake_array(array.to_data)Array::to_arc#12420Are these changes tested?
(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.