This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new b818f93416 perf: Improve performance of `md5` (#19568)
b818f93416 is described below
commit b818f93416d18d06374a0707f5ef571f8a384070
Author: Andy Grove <[email protected]>
AuthorDate: Wed Dec 31 02:24:34 2025 -0700
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 https://github.com/apache/datafusion/issues/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 https://github.com/apache/datafusion-benchmarks/pull/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.
-->
---
datafusion/functions/src/crypto/basic.rs | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/datafusion/functions/src/crypto/basic.rs
b/datafusion/functions/src/crypto/basic.rs
index 5a7d891e0c..bda16684c8 100644
--- a/datafusion/functions/src/crypto/basic.rs
+++ b/datafusion/functions/src/crypto/basic.rs
@@ -33,7 +33,7 @@ use datafusion_common::{
use datafusion_expr::ColumnarValue;
use md5::Md5;
use sha2::{Sha224, Sha256, Sha384, Sha512};
-use std::fmt::{self, Write};
+use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
@@ -157,14 +157,18 @@ pub fn md5(args: &[ColumnarValue]) ->
Result<ColumnarValue> {
})
}
-/// this function exists so that we do not need to pull in the crate hex. it
is only used by md5
-/// function below
+/// Hex encoding lookup table for fast byte-to-hex conversion
+const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef";
+
+/// Fast hex encoding using a lookup table instead of format strings.
+/// This is significantly faster than using `write!("{:02x}")` for each byte.
#[inline]
fn hex_encode<T: AsRef<[u8]>>(data: T) -> String {
- let mut s = String::with_capacity(data.as_ref().len() * 2);
- for b in data.as_ref() {
- // Writing to a string never errors, so we can unwrap here.
- write!(&mut s, "{b:02x}").unwrap();
+ let bytes = data.as_ref();
+ let mut s = String::with_capacity(bytes.len() * 2);
+ for &b in bytes {
+ s.push(HEX_CHARS_LOWER[(b >> 4) as usize] as char);
+ s.push(HEX_CHARS_LOWER[(b & 0x0f) as usize] as char);
}
s
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]