You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2025/12/30 20:26:54 UTC

[PR] perf: Improve performance of `md5` [datafusion]

andygrove opened a new pull request, #19568:
URL: https://github.com/apache/datafusion/pull/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.
   -->
   
   - Closes #.
   
   ## 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.
   -->
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Improve performance of `md5` [datafusion]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #19568:
URL: https://github.com/apache/datafusion/pull/19568#discussion_r2654161351


##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -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.

Review Comment:
   It's a bit weird to put the comment here. Maybe put on top of `HEX_CHARS_LOWER` lookup code.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Improve performance of `md5` [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #19568:
URL: https://github.com/apache/datafusion/pull/19568#discussion_r2653924382


##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -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 {

Review Comment:
   I tried this, but the difference is tiny, so it may not be worth it.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Improve performance of `md5` [datafusion]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #19568:
URL: https://github.com/apache/datafusion/pull/19568#discussion_r2653906392


##########
datafusion/functions/src/crypto/basic.rs:
##########
@@ -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 {

Review Comment:
   great, curious if that would be more performance if create a string directly from bytes without utf checks(which is redundant ) and extract casts?
   
   ```
   let bytes = data.as_ref();
   let mut out = Vec::with_capacity(bytes.len() * 2);
   
   for &b in bytes {
           out.push(HEX_CHARS_LOWER[(b >> 4) as usize]);
           out.push(HEX_CHARS_LOWER[(b & 0x0f) as usize]);
   }
   
   unsafe { String::from_utf8_unchecked(out) }
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Improve performance of `md5` [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan merged PR #19568:
URL: https://github.com/apache/datafusion/pull/19568


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org