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

Loading keys from pyca may be expensive #243

Closed
simo5 opened this issue Oct 6, 2021 · 0 comments · Fixed by #254
Closed

Loading keys from pyca may be expensive #243

simo5 opened this issue Oct 6, 2021 · 0 comments · Fixed by #254

Comments

@simo5
Copy link
Member

simo5 commented Oct 6, 2021

We should optimize by keeping round key objects in private variables.
Reference: pyca/cryptography#6301

simo5 added a commit to simo5/jwcrypto that referenced this issue 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 latchset#243

Signed-off-by: Simo Sorce <simo@redhat.com>
simo5 added a commit that referenced this issue 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

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant