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

Mention performance reasons for passing RSAPrivateKey to encode #733

Closed
dmahr1 opened this issue Feb 15, 2022 · 1 comment
Closed

Mention performance reasons for passing RSAPrivateKey to encode #733

dmahr1 opened this issue Feb 15, 2022 · 1 comment

Comments

@dmahr1
Copy link
Contributor

dmahr1 commented Feb 15, 2022

My employer is currently using PyJWT in production for signing URLs. As part of some routine performance monitoring and profiling, I discovered that this signing method was consuming a disproportionate amount of time - even more than the wall clock time of communicating with the database. The icicle plot below shows how most of the time was spent in the load_pem_private_key() method within the cryptography library.
image

I resolved the issue by manually instantiating the RSAPrivateKey object and passing that to jwt.encode(). As a result, RSAAlgorithm.prepare_key() returns immediately rather than instantiating a new RSAPrivateKey. Otherwise, the CPU-intensive RSA_check_key primality test would be needlessly rerun on every call to encode().

It looks like there's some discussion in #602 about changing the type hinting to make this usage more official. In the meantime, a stopgap is to make mention of the performance benefits of passing an RSAPrivateKey object in the Usage Examples page of the documentation.

#734

@dmahr1
Copy link
Contributor Author

dmahr1 commented Feb 15, 2022

@auvipy Thanks for the approval! Closing this issue.

@dmahr1 dmahr1 closed this as completed Feb 15, 2022
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

No branches or pull requests

1 participant