IABTechLab / iabgpp-java

Apache License 2.0
11 stars 12 forks source link

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

Open ChristopherWirt opened 5 months ago

ChristopherWirt commented 5 months ago

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.