Skip to content

Improve MD5 with SIMD - #105161

Merged
alexey-milovidov merged 12 commits into
ClickHouse:masterfrom
jh0x:jh-md5
May 19, 2026
Merged

alexey-milovidov merged 12 commits into
ClickHouse:masterfrom
jh0x:jh-md5

Conversation

@jh0x

@jh0x jh0x commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Related: #104887

Approach is similar to isa-l but very simplified.

Factored out the function into a separate TU to reduce the clutter.
Also removed OpenSSL dependency for MD5.

SELECT sum(reinterpretAsUInt64(substring(MD5(toString(number)), 1, 8))) FROM numbers(100000000)

- 1 row in set. Elapsed: 14.175 sec. Processed 100.00 million rows, 800.00 MB (7.05 million rows/s., 56.44 MB/s.)
+ 1 row in set. Elapsed: 3.659 sec. Processed 100.00 million rows, 800.00 MB (27.33 million rows/s., 218.66 MB/s.)
SELECT ignore(MD5(toString(1000000000+number))) FROM numbers(100000000) FORMAT Null

old            : 0 rows in set. Elapsed: 13.567 sec. Processed 100.00 million rows, 800.00 MB ( 7.37 million rows/s.,  58.97 MB/s.)
new-nomultiarch: 0 rows in set. Elapsed: 10.625 sec. Processed 100.00 million rows, 800.00 MB ( 9.41 million rows/s.,  75.29 MB/s.)
new-multiarch  : 0 rows in set. Elapsed:  3.601 sec. Processed 100.00 million rows, 800.00 MB (27.77 million rows/s., 222.14 MB/s.)

And with the newly added  register hygiene commit:
new-multiarch  : 0 rows in set. Elapsed: 2.946 sec. Processed 100.00 million rows, 800.00 MB (33.95 million rows/s., 271.58 MB/s.)

CPU: Intel(R) Xeon(R) Platinum 8160 CPU @ 2.10GHz
In Proxmox VM.

Results from: tests/performance/scripts/perf.py --runs 30 ../ClickHouse3/tests/performance/cryptographic_hashes.xml | egrep '(name|median)' > res_XXX.txt:

Results from: tests/performance/scripts/perf.py --runs 20 ../ClickHouse3/tests/performance/md5.xml | egrep '(name|median)' > res_XXX.txt:

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add SIMD implementation of MD5 that hashes multiple inputs in parallel using AVX2/AVX512, improving throughput.

Version info

  • Merged into: 26.5.1.862

@Algunenano Algunenano self-assigned this May 17, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [021ec77]

Summary: ✅


AI Review

Summary

This PR replaces MD5 with a multi-buffer implementation (default scalar + x86 AVX2/AVX-512 variants), keeps FIPS gating when USE_SSL is enabled, moves registration/docs into FunctionMD5.cpp, and adds focused stateless/performance/unit coverage. I re-checked the full current diff plus all prior PR discussion threads; the previously open bot concerns are addressed in current code (runtime-gated SIMD gtests and clarified/extended test coverage), and I did not find new correctness, safety, or compatibility regressions.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 17, 2026
Comment thread src/Functions/FunctionMD5.cpp Resolved
Comment thread src/Functions/FunctionMD5.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Thanks! I like the implementation.

This is exactly what I wanted in #58394, see "Data parallel implementation of hash functions for fixed-size strings", but I was afraid of temporary copies to align widths.

Hope after we merge this, we will extend it to SipHash, SHA-1, and SHA-2.

@alexey-milovidov alexey-milovidov self-assigned this May 17, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

No visible improvements on performance tests. Only suspected degradations:

Screenshot_20260518_005109

PS. Does it optimize halfMD5?

@vyalamar

Copy link
Copy Markdown
Contributor

@jh0x @alexey-milovidov I opened a small follow-up against this branch for AArch64 ASIMD, using the same dependency-free Ops / multi-buffer structure:

https://github.com/jh0x/ClickHouse/pull/2

The idea is to keep the direction of this PR, but cover ARM / Apple / Linux AArch64 without adding ISA-L Crypto. I validated it locally on Linux arm64 against OpenSSL for RFC-style vectors, 55/56/63/64/65-byte padding boundaries, partial 8-row batches, mixed sizes, and multi-batch execution.

Standalone backend benchmark on Apple M4 Pro / Linux arm64 showed about 4x over OpenSSL scalar for the tested row shapes. This is not a full ClickHouse binary benchmark yet, but it looked promising enough to check whether the ARM backend is worth folding into this PR.

@jh0x

jh0x commented May 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov

No visible improvements on performance tests. Only suspected degradations:

I added one perf test and it doesn't appear in the workflow run:

<query>SELECT sum(reinterpretAsUInt64(substring(MD5(toString(number)), 1, 8))) FROM numbers(10000000)</query>

PS. Does it optimize halfMD5?

I don't think so - wasn't looking at this one.

I should have a look in the evening with perf at the potential degradations. Not sure about the one not appearing in the report.

@jh0x

jh0x commented May 18, 2026 •

Copy link
Copy Markdown
Contributor Author

I added a commit that gets it done without using SSL:

SELECT ignore(MD5(toString(1000000000+number))) FROM numbers(100000000) FORMAT Null

old            : 0 rows in set. Elapsed: 13.567 sec. Processed 100.00 million rows, 800.00 MB ( 7.37 million rows/s.,  58.97 MB/s.)
new-nomultiarch: 0 rows in set. Elapsed: 10.625 sec. Processed 100.00 million rows, 800.00 MB ( 9.41 million rows/s.,  75.29 MB/s.)
new-multiarch  : 0 rows in set. Elapsed:  3.601 sec. Processed 100.00 million rows, 800.00 MB (27.77 million rows/s., 222.14 MB/s.)

And with the newly added  register hygiene commit:
new-multiarch  : 0 rows in set. Elapsed: 2.946 sec. Processed 100.00 million rows, 800.00 MB (33.95 million rows/s., 271.58 MB/s.)

CPU: Intel(R) Xeon(R) Platinum 8160 CPU @ 2.10GHz
In Proxmox VM.

@alexey-milovidov
As for that perf test added to xml - it appears when run locally:

tests/performance/scripts/perf.py --runs 30 tests/performance/cryptographic_hashes.xml | egrep '(name|median)' > res_xxx.txt

On the machine above I get:

  ┌─────┬─────────────────────────┬───────┬───────┬────────┐                                                                                                                                                                                                                                             
  │  #  │          Query          │  old  │  new  │ new_ma │                                                                                                                                                                                                                                             
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 0   │ MD5('') 1M single       │ 0.136 │ 0.106 │ 0.053  │
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 1   │ MD5(toString) 1M single │ 0.143 │ 0.112 │ 0.057  │                                                                                                                                                                                                                                             
  ├─────┼─────────────────────────┼───────┼───────┼────────┤
  │ 2   │ MD5('') 10M mt          │ 0.057 │ 0.040 │ 0.021  │                                                                                                                                                                                                                                             
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 3   │ MD5(toString) 10M mt    │ 0.058 │ 0.044 │ 0.024  │
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 24  │ MD5(lorem) 1M single    │ 0.199 │ 0.203 │ 0.153  │
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 25  │ MD5(lorem) 1M mt        │ 0.168 │ 0.177 │ 0.154  │
  ├─────┼─────────────────────────┼───────┼───────┼────────┤                                                                                                                                                                                                                                             
  │ 48  │ sum(MD5(toString)) 10M  │ 1.502 │ 1.144 │ 0.507  │
  └─────┴─────────────────────────┴───────┴───────┴────────┘

I should work with perf more later on this week.

Comment thread src/Functions/FunctionMD5.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Yes, the difference is visible. Only on one trivial query, though:

Screenshot_20260519_031647

@alexey-milovidov

Copy link
Copy Markdown
Member

I'd like to test on URL, Referer, Title, SearchPhrase, MobilePhoneModel from ClickBench.

@vyalamar

vyalamar commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

@jh0x @alexey-milovidov can you also please review . One for some tests and other for ARM as dicussed before.

https://github.com/jh0x/ClickHouse/pull/1
https://github.com/jh0x/ClickHouse/pull/2

@alexey-milovidov Also can I help/contribute with SIMD for other hash functions without bringing external dependencies?

@alexey-milovidov

Copy link
Copy Markdown
Member

@vyalamar, yes, it will be amazing! MD5 is used in legacy scenarios; adding SipHash and SHA-1,2 will be more practical!

@alexey-milovidov

Copy link
Copy Markdown
Member

It's harder to review PRs in forks, because of less automation... It will be ok, if you create a PR here, that is a superset of commits from this PR, and you can merge commits from each other's PRs.

@vyalamar

Copy link
Copy Markdown
Contributor

Sure will do the same. At the gym right now . Will do in few hours.

@Algunenano Algunenano removed their assignment May 19, 2026
Comment thread src/Functions/tests/gtest_md5.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Yes!

Screenshot_20260519_182110

@jh0x

jh0x commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

Need to push one more commit + will bring origin/master in.

@vyalamar

Copy link
Copy Markdown
Contributor

@alexey-milovidov since this is close to merge, perhaps will rebase to this and then bring my two PRs.

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 94.34% (200/212) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit b551d90 May 19, 2026
165 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants