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 pub/pri keys on retrieval #254

Merged
merged 1 commit into from
Nov 30, 2021
Merged

Cache pub/pri keys on retrieval #254

merged 1 commit into from
Nov 30, 2021

Conversation

simo5
Copy link
Member

@simo5 simo5 commented Nov 30, 2021

Pyca rightfully performs consistency checks when importing keys and
these operations are rather expensive. So cache keys once generated so
that repeated uses of the same JWK do not incur undue cost of reloading
the keys from scratch for each subsequent operation.

with a simple test by hand:
$ python

from jwcrypto import jwk
def test():
... key = jwk.JWK.generate(kty='RSA', size=2048)
... for i in range(1000):
... k = key._get_private_key()
...
import timeit

Before the patch:

print(timeit.timeit("test()", setup="from main import test", number=10))
35.80328264506534

After the patch:

print(timeit.timeit("test()", setup="from main import test", number=10))
0.9109518649056554

Resolves #243

Pyca rightfully performs consistency checks when importing keys and
these operations are rather expensive. So cache keys once generated so
that repeated uses of the same JWK do not incur undue cost of reloading
the keys from scratch for each subsequent operation.

with a simple test by hand:
$ python
>>> from jwcrypto import jwk
>>> def test():
...     key = jwk.JWK.generate(kty='RSA', size=2048)
...     for i in range(1000):
...             k = key._get_private_key()
...
>>> import timeit

Before the patch:
>>> print(timeit.timeit("test()", setup="from __main__ import test", number=10))
35.80328264506534

After the patch:
>>> print(timeit.timeit("test()", setup="from __main__ import test", number=10))
0.9109518649056554

Resolves latchset#243

Signed-off-by: Simo Sorce <simo@redhat.com>
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.

Loading keys from pyca may be expensive
1 participant