Skip to content

Avoid synchronization by cloning a prototype Mac and reallocating buffers #42

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
Jul 31, 2022

Conversation

jchambers
Copy link
Owner

I've been thinking back on the conversation in #34, and I think I've found a best-of-both-worlds approach. To recap, the suggestion in #34 was to allocate a new buffer and instantiate a new Mac on each call to generateOneTimePassword to avoid synchronization, leading to higher throughput. The problem was that instantiating a new Mac was pretty slow.

It turns out that cloning a Mac is actually pretty fast, though! We can't guarantee that cloning a Mac will be possible in all setups—some provider might have a non-cloneable HmacSHA512 implementation, for example—but it'll probably be true in most setups. This lets us make generateOneTimePassword non-synchronized while still retaining thread safety, which seems like a win.

This may come at the cost of a modest throughput decrease in the single-threaded case, but if it does, it's on the order of 1%, and I'd say that's a worthwhile price to pay.

@jchambers jchambers added this to the 0.4.0 milestone Jul 25, 2022
@jchambers jchambers force-pushed the clone_mac branch 3 times, most recently from 4cb1e82 to 2a09306 Compare July 25, 2022 01:38
@jchambers jchambers changed the title Avoid synchronization by cloning a prototype Mac and reallocating buffers Avoid synchronization by cloning a prototype Mac and reallocating buffers Jul 25, 2022
@jchambers jchambers merged commit 2146875 into master Jul 31, 2022
@jchambers jchambers deleted the clone_mac branch July 31, 2022 15: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.

1 participant