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

Cache HMAC object and fix memory leak #77

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

qmuntal
Copy link
Collaborator

@qmuntal qmuntal commented Jun 15, 2023

This PR improves NewHMAC so it reuses the same EVP_MAC object instead of fetching it every time.

Doing so also fixed a memory leak: the object returned by EVP_MAC_fetch should have been freed using EVP_MAC_free. The new approach doesn't require freeing that object, as it is kept as a global variable to be reused later on.

Thanks to caching, NewHMAC is now ~9% faster:

goos: linux
goarch: amd64
pkg: github.com/golang-fips/openssl
cpu: AMD EPYC 7763 64-Core Processor                
           │   old.txtnew.txt               │
           │   sec/opsec/op     vs baseNewHMAC-16   4.616µ ± 1%   4.199µ ± 1%  -9.04% (p=0.000 n=20)

Copy link
Collaborator

@simo5 simo5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qmuntal qmuntal merged commit 40832d6 into wip/combined-codebase-staging Jun 16, 2023
8 checks passed
@qmuntal qmuntal deleted the wip/hmacfetch branch June 16, 2023 07:38
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.

None yet

4 participants