viirya commented on code in PR #19530:
URL: https://github.com/apache/datafusion/pull/19530#discussion_r2650156890
##########
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:
Benchmark Results (Single ASCII Character Replacement):
- size=1024, str_len=32: 29.5 µs → 21.4 µs (27% faster)
- size=1024, str_len=128: 73.9 µs → 23.4 µs (68% faster)
- size=4096, str_len=32: 121.8 µs → 85.6 µs (30% faster)
- size=4096, str_len=128: 316.9 µs → 83.8 µs (74% faster)
--
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]