Skip to content

Commit b818f93

Browse files
authored
perf: Improve performance of md5 (#19568)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of #19569 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> I ran microbenchmarks comparing DataFusion with DuckDB for string functions (see apache/datafusion-benchmarks#26) and noticed that DF was very slow for `md5`. This PR improves performance: | Benchmark | Before | After | Speedup | |----------------------------|--------|--------|-------------| | md5_array (1024 strings) | 206 µs | 100 µs | 2.1x faster | | md5_scalar (single string) | 337 ns | 221 ns | 1.5x faster | ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Avoid using `write!` with a format string and use a more efficient approach ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 79f67b8 commit b818f93

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

datafusion/functions/src/crypto/basic.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use datafusion_common::{
3333
use datafusion_expr::ColumnarValue;
3434
use md5::Md5;
3535
use sha2::{Sha224, Sha256, Sha384, Sha512};
36-
use std::fmt::{self, Write};
36+
use std::fmt;
3737
use std::str::FromStr;
3838
use std::sync::Arc;
3939

@@ -157,14 +157,18 @@ pub fn md5(args: &[ColumnarValue]) -> Result<ColumnarValue> {
157157
})
158158
}
159159

160-
/// this function exists so that we do not need to pull in the crate hex. it is only used by md5
161-
/// function below
160+
/// Hex encoding lookup table for fast byte-to-hex conversion
161+
const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef";
162+
163+
/// Fast hex encoding using a lookup table instead of format strings.
164+
/// This is significantly faster than using `write!("{:02x}")` for each byte.
162165
#[inline]
163166
fn hex_encode<T: AsRef<[u8]>>(data: T) -> String {
164-
let mut s = String::with_capacity(data.as_ref().len() * 2);
165-
for b in data.as_ref() {
166-
// Writing to a string never errors, so we can unwrap here.
167-
write!(&mut s, "{b:02x}").unwrap();
167+
let bytes = data.as_ref();
168+
let mut s = String::with_capacity(bytes.len() * 2);
169+
for &b in bytes {
170+
s.push(HEX_CHARS_LOWER[(b >> 4) as usize] as char);
171+
s.push(HEX_CHARS_LOWER[(b & 0x0f) as usize] as char);
168172
}
169173
s
170174
}

0 commit comments

Comments
 (0)