perf: Optimize split_part for Utf8View#21420
Conversation
metegenez
left a comment
There was a problem hiding this comment.
It might be worth adding unit tests for sliced StringViewArray inputs (non-zero offset) and results landing exactly at the 12-byte inline/out-of-line boundary. These are the two spots most likely to surface issues with the unsafe view construction.
Other than that, nice optimization.
| split_nth_finder(s, &finder, delimiter.len(), idx) | ||
| }); | ||
| } else { | ||
| let idx: usize = (position.unsigned_abs() - 1).try_into().map_err(|_| { |
There was a problem hiding this comment.
upstream guarantees position will not be zero, so this looks safe.
Thanks! Added some tests for those cases. |
alamb
left a comment
There was a problem hiding this comment.
Looks good to me too -- thanks @neilconway
|
Thanks everyone |
## Which issue does this PR close? - Closes apache#21410. ## Rationale for this change When `split_part` is invoked with a `StringViewArray`, we can avoid copying when constructing the return value by instead returning pointers into the view buffers of the input `StringViewArray`. This PR only applies this optimization to the code path for scalar `delimiter` and `position`, because that's the most common usage mode in practice. We could also optimize the array-args case but it didn't seem worth the extra code. Benchmarks (M4 Max): - scalar_utf8view_very_long_parts/pos_first: 102 µs → 68 µs (-33%) - scalar_utf8view_long_parts/pos_middle: 164 µs → 137 µs (-15%) - scalar_utf8_single_char/pos_first: 42.5 µs → 42.9 µs (no change) - scalar_utf8_single_char/pos_middle: 96.5 µs → 99.5 µs (noise) - scalar_utf8_single_char/pos_negative: 48.3 µs → 48.6 µs (no change) - scalar_utf8_multi_char/pos_middle: 132 µs → 132 µs (no change) - scalar_utf8_long_strings/pos_middle: 1.06 ms → 1.08 ms (noise) - array_utf8_single_char/pos_middle: 355 µs → 365 µs (noise) - array_utf8_multi_char/pos_middle: 357 µs → 360 µs (no change) ## What changes are included in this PR? * Implement optimization * Add benchmark that covers this case * Improve SLT test coverage for this code path ## Are these changes tested? Yes. ## Are there any user-facing changes? No.
Which issue does this PR close?
split_partto avoid copying via StringView #21410.Rationale for this change
When
split_partis invoked with aStringViewArray, we can avoid copying when constructing the return value by instead returning pointers into the view buffers of the inputStringViewArray.This PR only applies this optimization to the code path for scalar
delimiterandposition, because that's the most common usage mode in practice. We could also optimize the array-args case but it didn't seem worth the extra code.Benchmarks (M4 Max):
What changes are included in this PR?
Are these changes tested?
Yes.
Are there any user-facing changes?
No.