Jefffrey commented on code in PR #19530:
URL: https://github.com/apache/datafusion/pull/19530#discussion_r2650124989
##########
datafusion/functions/src/string/replace.rs:
##########
@@ -185,17 +193,48 @@ fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) ->
Result<ArrayRef> {
let from_array = as_generic_string_array::<T>(&args[1])?;
let to_array = as_generic_string_array::<T>(&args[2])?;
- let result = string_array
+ let mut builder = GenericStringBuilder::<T>::new();
+ let mut buffer = String::new();
+
+ for ((string, from), to) in string_array
.iter()
.zip(from_array.iter())
.zip(to_array.iter())
- .map(|((string, from), to)| match (string, from, to) {
- (Some(string), Some(from), Some(to)) => Some(string.replace(from,
to)),
- _ => None,
- })
- .collect::<GenericStringArray<T>>();
+ {
+ match (string, from, to) {
+ (Some(string), Some(from), Some(to)) => {
+ buffer.clear();
+ replace_into_string(&mut buffer, string, from, to);
+ builder.append_value(&buffer);
+ }
+ _ => builder.append_null(),
+ }
+ }
+
+ Ok(Arc::new(builder.finish()) as ArrayRef)
+}
- Ok(Arc::new(result) 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) {
Review Comment:
I wonder if we lost other optimization tricks that the Rust `replace`
function provides; for example looking at the code:
https://doc.rust-lang.org/src/alloc/str.rs.html#268
It has a `Fast path for replacing a single ASCII character with another`. I
wonder if we capture this in our benchmarks?
--
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]