perf: Use bulk-NULL builder in replace#21849
Merged
alamb merged 7 commits intoapache:mainfrom Apr 28, 2026
Merged
Conversation
Jefffrey
approved these changes
Apr 27, 2026
alamb
reviewed
Apr 27, 2026
Comment on lines
+168
to
+171
| let nulls = NullBuffer::union( | ||
| NullBuffer::union(string_array.nulls(), from_array.nulls()).as_ref(), | ||
| to_array.nulls(), | ||
| ); |
Contributor
There was a problem hiding this comment.
A good use for the new NullBuffer::union_many eventually:
Contributor
There was a problem hiding this comment.
Filed a ticket to track that improvement:
alamb
reviewed
Apr 27, 2026
| } | ||
| _ => builder.append_null(), | ||
| for i in 0..len { | ||
| if nulls.as_ref().is_some_and(|n| n.is_null(i)) { |
Contributor
There was a problem hiding this comment.
We can probably improve this more by pulling the check for null buffer out and making special loops for when there are nulls and when there are no nulls
Contributor
Author
There was a problem hiding this comment.
Yep; I've been doing this in some cases but not always. Having Claude look at the generated assembly, seems like LLVM does the hoist / loop duplication for us for the replace + Utf8/LargeUtf8 case, but not for Utf8View.
57f27f4 to
e4c68e7
Compare
alamb
approved these changes
Apr 28, 2026
Contributor
alamb
left a comment
There was a problem hiding this comment.
LGTM -- thanks @neilconway
| replace_into_string(&mut buffer, string, from, to); | ||
| builder.append_value(&buffer); | ||
| // Hoist the nulls.is_some() check out of the loop. LLVM unswitches this | ||
| // automatically today, but kept explicit so the no-nulls fast path is not |
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?
replaceto use bulk-NULL string builder #21848.Rationale for this change
We can use the new bulk-NULL string builder APIs to reduce NULL handling overhead in
replace. There is a further opportunity for optimization once arrow/arrow-rs#9692 lands.Benchmarks:
replace size=1024/replace_string_ascii_single [str_len=32]: 17.42 µs → 14.35 µs, −17.71%replace size=1024/replace_string_view [str_len=32]: 21.23 µs → 18.89 µs, −11.05%replace size=1024/replace_string [str_len=32]: 20.42 µs → 18.51 µs, −9.36%replace size=1024/replace_large_string [str_len=32]: 20.83 µs → 18.42 µs, −11.60%replace size=1024/replace_string_ascii_single [str_len=128]: 10.61 µs → 10.37 µs, −2.22%replace size=1024/replace_string_view [str_len=128]: 28.37 µs → 28.03 µs, −1.21%replace size=1024/replace_string [str_len=128]: 27.96 µs → 28.03 µs, +0.25% (noise)replace size=1024/replace_large_string [str_len=128]: 28.15 µs → 27.95 µs, −0.70% (noise)replace size=4096/replace_string_ascii_single [str_len=32]: 68.40 µs → 56.15 µs, −17.94%replace size=4096/replace_string_view [str_len=32]: 85.95 µs → 76.84 µs, −10.61%replace size=4096/replace_string [str_len=32]: 82.79 µs → 74.71 µs, −9.76%replace size=4096/replace_large_string [str_len=32]: 85.32 µs → 75.14 µs, −11.94%replace size=4096/replace_string_ascii_single [str_len=128]: 38.35 µs → 39.10 µs, +1.96% (small regression)replace size=4096/replace_string_view [str_len=128]: 132.77 µs → 128.08 µs, −3.53%replace size=4096/replace_string [str_len=128]: 127.71 µs → 128.29 µs, +0.46% (noise)replace size=4096/replace_large_string [str_len=128]: 131.97 µs → 128.68 µs, −2.49%What changes are included in this PR?
replaceAre these changes tested?
Yes, covered by existing tests.
Are there any user-facing changes?
No.