Skip to content
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

Speed up use of PBKDF2 when building in Debug configuration #315

Merged
merged 4 commits into from
Aug 1, 2023
Merged

Speed up use of PBKDF2 when building in Debug configuration #315

merged 4 commits into from
Aug 1, 2023

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented Jul 28, 2023

MongoKitten users (including those using the Fluent 4 driver built on it) sometimes experience significant connection delays during development due to the compiler not optimizing the PBKDF2 implementation. With a little help from Instruments, these delays can be massively reduced. The follow shows a timing comparison between the original code and that provided in this PR:

Comparisons

10,000 iterations of each hash are repeated 10x, and the total times averaged. Each test is run in Debug and Release configurations, against the original and revised implementations. The first number is the runtime average of the original code; the second number is the runtime average of the new code. The third number expresses how much of the original implementation's runtime is used by the revised one.

macOS (Xcode 15 beta 5)

Config MD5 SHA-1 SHA-256
debug 5.736s / 0.256s (4.6%) 10.268s / 0.491s (4.8%) 13.799s / 0.483s (3.5%)
release 0.066s / 0.064s (97.0%) 0.099s / 0.089s (90.0%) 0.126s / 0.116s (92.1%)

Linux (swift:5.8-jammy)

Config MD5 SHA-1 SHA-256
debug 1.711s / 0.297s (17.4%) 2.088s / 0.634s (30.2%) 3.135s / 0.631s (20.1%)
release 0.119s / 0.067s (56.3%) 0.083s / 0.082s (98.8%) 0.100s / 0.095s (95.0%)

Linux (5.9 nightly - swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a)

Config MD5 SHA-1 SHA-256
debug 1.379s / 0.290s (21.0%) 1.435s / 0.565s (39.4%) 1.625s / 0.715s (44.0%)
release 0.121s / 0.070s (57.9%) 0.083s / 0.085s (102%) 0.130s / 0.124s (95.4%)

gwynne added 3 commits July 28, 2023 09:42
…s to massively improve performance in Debug builds.
…#if DEBUG wrapping to a few tests that rely on @testable so the performance tests can be run in Release configuration.
@gwynne
Copy link
Contributor Author

gwynne commented Jul 28, 2023

For comparison, here's a quick set of performance numbers for using the SwiftCrypto Insecure.MD5, Insecure.SHA1, and SHA256 implementations:

macOS (Xcode 15 beta 5)

Config MD5 SHA-1 SHA-256
debug 0.141s 0.119s 0.120s
release 0.087s 0.067s 0.067s

Linux (swift:5.8-jammy)

Config MD5 SHA-1 SHA-256
debug 0.340s 0.237s 0.258s
release 0.076s 0.063s 0.065s

Linux (5.9 nightly - swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a)

Config MD5 SHA-1 SHA-256
debug 0.352s 0.248s 0.280s
release 0.074s 0.062s 0.064s

Conclusions:

  • SwiftCrypto's MD5 implementation is oddly slow. (This might just be a side effect of MD5 having relatively little internal state, or possibly the M1 just handles SHA-1 and 2 better 🤷‍♀️)
  • SwiftCrypto's SHA-1 implementation is much faster than us.
  • SwiftCrypto's SHA-2 implementation is massively faster than us.

Might be the numbers would change all around on an Intel machine, though.

@Joannis Joannis merged commit 34929f3 into orlandos-nl:master/7.0 Aug 1, 2023
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