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

Improve JWT parse / decode performance #620

Merged
merged 27 commits into from
Jan 31, 2023
Merged

Conversation

noetro
Copy link
Contributor

@noetro noetro commented Oct 17, 2022

Changes

This merge improves JWT parse/decode performance by re-using Jackson ObjectFactory and ObjectReader instances that are thread safe and expensive to create.

To do so it also cleans up the deserialiser classes to use the information already available in the Jackson deserializer method parameters - namely the parser codec - instead of passing that information in at constructor time for the deserializer.

This type of pattern is already implemented in the serialize side of the codebase - note the static final ObjectMapper in JWTCreator and that the constructors for PayloadSerializer and HeaderSerializer take no arguments. This pull request brings parity to the deserialize side.

Note that there was some thought to already being able to re-use a parser by instantiating and holding an instance of 'com.auth0.jwt.JWT', but that only works partially since JWT.require is static and it instantiates a new JWTVerifier that always instantiates a new parser.

It's probably just safer to follow the same pattern as JWTCreator and transparently solve this in the library internally for the user.

I added JMH support to the build file with comments and one benchmark for decoding. On my machine throughput:

Master: 221755,008 ±(99.9%) 33951,905 ops/s [Average]
This Pull: 715079,328 ±(99.9%) 47281,796 ops/s [Average]

References

There is a lot of material out there on the internet on re-using ObjectMapper instances, and the JavaDoc in Jackson give guidance.

Testing

I added JMH support to the project so that there is some support for getting a performance impact picture. I didn't add new test cases as there seemed to be good coverage on all the deserialisation variants.

  • [ X] This change adds test coverage
  • [X ] This change has been tested on the latest version of Java or why not

Checklist

@noetro noetro requested a review from a team as a code owner October 17, 2022 07:35
@Widcket
Copy link
Contributor

Widcket commented Nov 7, 2022

cc @poovamraj

@noetro
Copy link
Contributor Author

noetro commented Dec 29, 2022

Any reviewers out there....?

@poovamraj
Copy link
Contributor

Hey, @noetro sorry for the delayed response. Me and @jimmyjames will check this out this week and get back to you. Thanks for understanding

@noetro
Copy link
Contributor Author

noetro commented Jan 3, 2023

No worries, I understand everyone is busy. I was just checking in.

Copy link
Contributor

@jimmyjames jimmyjames left a comment

Choose a reason for hiding this comment

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

🎉 Thanks @noetro for this change, a great improvement! Appreciate it, and apologies for the delay in reviewing 🙇

@jimmyjames jimmyjames merged commit 9024318 into auth0:master Jan 31, 2023
@jimmyjames jimmyjames mentioned this pull request Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants