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

Reduce the number of object allocations on the decode/encode hotpath #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChristopherWirt
Copy link

When profiling I noticed a large number of strings being generated from AbstractBase64UrlEncoder.decode(). I think this is mostly attributable to the string concatenation inside the various loops. To fix this I've switched to using a StringBuilder. This avoids new strings being allocated on each iteration.

I've also removed the use of regular expressions to validate inputs these are also relatively expensive to be happening at such a low level of the code. For bitString validation I've used a simple char inspecting method. For base64 I just used the absence of it appearing in the REVERSE_DICT to indicate this is an invalid base64 string.

Finally to reduce auto-boxing I switched to using a Char2IntOpenHashMap for holding the REVERSE_DICT. To do this I've had to import a 3rd party fast utils lib - which is a lightweight collection of optimised primitive collection types, useful in cases like this.

When profiling I noticed a large number of strings being generated from
AbstractBase64UrlEncoder.decode(). I think this is mostly attributable
to the string concatenation inside the various loops. To fix this I've
switched to using a StringBuilder. This avoids new strings being
allocated on each iteration.

I've also removed the use of regular expressions to validate inputs
these are also relatively expensive to be happening at such a low level
of the code. For bitString validation I've used a simple char inspecting
method. For base64 I just used the absence of it appearing in the
REVERSE_DICT to indicate this is an invalid base64 string.

Finally to reduce autoboxing I switched to using a Char2IntOpenHashMap
for holding the REVERSE_DICT. To do this I've had to import a 3rd party
fast utils lib.
@yuzawa-san
Copy link

I expanded upon the ideas in this pr in my own pr #56 please take a look

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.

3 participants