lyne7-sc commented on code in PR #22029:
URL: https://github.com/apache/datafusion/pull/22029#discussion_r3199437877
##########
datafusion/functions/src/string/replace.rs:
##########
@@ -230,71 +226,77 @@ fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) ->
Result<ArrayRef> {
let string = unsafe { string_array.value_unchecked(i) };
let from = unsafe { from_array.value_unchecked(i) };
let to = unsafe { to_array.value_unchecked(i) };
- buffer.clear();
- replace_into_string(&mut buffer, string, from, to);
- builder.append_value(&buffer);
+ apply_replace(&mut builder, string, from, to);
}
} else {
for i in 0..len {
// SAFETY: i < len, and no input has a null buffer.
let string = unsafe { string_array.value_unchecked(i) };
let from = unsafe { from_array.value_unchecked(i) };
let to = unsafe { to_array.value_unchecked(i) };
- buffer.clear();
- replace_into_string(&mut buffer, string, from, to);
- builder.append_value(&buffer);
+ apply_replace(&mut builder, string, from, to);
}
}
Ok(Arc::new(builder.finish(nulls)?) as ArrayRef)
}
-/// Helper function to perform string replacement into a reusable String buffer
#[inline]
-fn replace_into_string(buffer: &mut String, string: &str, from: &str, to:
&str) {
+fn apply_replace<B: BulkNullStringArrayBuilder>(
+ builder: &mut B,
+ string: &str,
+ from: &str,
+ to: &str,
+) {
if from.is_empty() {
- // When from is empty, insert 'to' at the beginning, between each
character, and at the end
- // This matches the behavior of str::replace()
- buffer.push_str(to);
- for ch in string.chars() {
- buffer.push(ch);
- buffer.push_str(to);
- }
+ // Empty `from`: insert `to` before each character and at both ends
+ builder.append_with(|w| {
+ w.write_str(to);
+ for ch in string.chars() {
+ w.write_char(ch);
Review Comment:
Wondering about the `from.is_empty()` branch — the per-write trade-off looks
unusually unfavorable here. Is it worth adding a `from_empty` benchmark case?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]