Skip to content

Optimize performance for large-scale parallelism #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025

Conversation

wangrunji0408
Copy link
Contributor

@wangrunji0408 wangrunji0408 commented May 26, 2025

This PR addresses performance bottlenecks in multithreaded scenarios by refactoring how Regex objects are handled.

The crate currently uses a globally shared Regex object, which internally maintains a cache pool. This implementation causes significant contention overhead when accessed by multiple threads simultaneously.

This PR changes Regex objects from global to thread-local storage. Each thread now compiles its own Regex upon first access and maintains an independent cache. We also add a new multithreaded benchmark to demonstrate the impact.

On a 11-core Macbook M3 Pro:

main:
multithreaded/multi_thread
                        time:   [118.90 µs 119.68 µs 120.63 µs]
                        thrpt:  [971.50 KiB/s 979.16 KiB/s 985.63 KiB/s]

this PR:
multithreaded/multi_thread
                        time:   [98.642 µs 99.572 µs 100.53 µs]
                        thrpt:  [1.1384 MiB/s 1.1493 MiB/s 1.1602 MiB/s]
                 change:
                        time:   [-18.014% -17.382% -16.666%] (p = 0.00 < 0.05)
                        thrpt:  [+20.000% +21.039% +21.972%]

On a 192-core x86_64 server:

main:
multithreaded/multi_thread
                        time:   [1.1241 ms 1.1523 ms 1.2042 ms]
                        thrpt:  [95.038 MiB/s 99.318 MiB/s 101.81 MiB/s]

this PR:
multithreaded/multi_thread
                        time:   [257.17 µs 258.42 µs 259.53 µs]
                        thrpt:  [440.96 MiB/s 442.85 MiB/s 445.01 MiB/s]
                 change:
                        time:   [-77.636% -77.141% -76.776%] (p = 0.00 < 0.05)
                        thrpt:  [+330.59% +337.47% +347.15%]

Tips: Hide whitespace for better review experience

Copy link

codecov bot commented May 26, 2025

Codecov Report

Attention: Patch coverage is 97.43590% with 2 lines in your changes missing coverage. Please review.

Project coverage is 84.25%. Comparing base (e6d6d9a) to head (41c215e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/hmm.rs 95.23% 1 Missing ⚠️
src/lib.rs 98.24% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #122      +/-   ##
==========================================
- Coverage   85.76%   84.25%   -1.51%     
==========================================
  Files           9        9              
  Lines        2065     1912     -153     
==========================================
- Hits         1771     1611     -160     
- Misses        294      301       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

codspeed-hq bot commented May 26, 2025

CodSpeed Performance Report

Merging #122 will not alter performance

Comparing wangrunji0408:optimize-regex (41c215e) with main (e6d6d9a)

Summary

✅ 10 untouched benchmarks
🆕 2 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 multi_thread N/A 29.9 ms N/A
🆕 single_thread N/A 28.6 ms N/A

@messense messense merged commit 2f06908 into messense:main May 26, 2025
9 of 10 checks passed
@wangrunji0408 wangrunji0408 deleted the optimize-regex branch May 30, 2025 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants