alamb commented on code in PR #20344:
URL: https://github.com/apache/datafusion/pull/20344#discussion_r2805741558
##########
datafusion/functions/src/string/replace.rs:
##########
@@ -228,19 +233,21 @@ fn replace_into_string(buffer: &mut String, string: &str,
from: &str, to: &str)
return;
}
- // Fast path for replacing a single ASCII character with another single
ASCII character
- // This matches Rust's str::replace() optimization and enables
vectorization
+ // Fast path for replacing a single ASCII character with another single
ASCII character.
+ // Extends the buffer's underlying Vec<u8> directly, for performance.
if let ([from_byte], [to_byte]) = (from.as_bytes(), to.as_bytes())
&& from_byte.is_ascii()
&& to_byte.is_ascii()
{
- // SAFETY: We're replacing ASCII with ASCII, which preserves UTF-8
validity
- let replaced: Vec<u8> = string
- .as_bytes()
- .iter()
- .map(|b| if *b == *from_byte { *to_byte } else { *b })
- .collect();
- buffer.push_str(unsafe { std::str::from_utf8_unchecked(&replaced) });
+ // SAFETY: Replacing an ASCII byte with another ASCII byte preserves
UTF-8 validity.
Review Comment:
👍
--
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]