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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion iabgpp-encoder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.12</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -53,4 +58,3 @@
</profile>
</profiles>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.stream.Stream;
import com.iab.gpp.encoder.error.DecodingException;
import com.iab.gpp.encoder.error.EncodingException;
import it.unimi.dsi.fastutil.chars.Char2IntMap;
import it.unimi.dsi.fastutil.chars.Char2IntOpenHashMap;

public abstract class AbstractBase64UrlEncoder {

Expand All @@ -15,64 +17,68 @@ public abstract class AbstractBase64UrlEncoder {
* Base 64 URL character set. Different from standard Base64 char set in that '+' and '/' are
* replaced with '-' and '_'.
*/
private static String DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
private static final String DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
// prettier-ignore
private static Map<Character, Integer> REVERSE_DICT = Stream
private static final Char2IntMap REVERSE_DICT = new Char2IntOpenHashMap(Stream
.of(new Object[][] {{'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7}, {'I', 8},
{'J', 9}, {'K', 10}, {'L', 11}, {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15}, {'Q', 16}, {'R', 17}, {'S', 18},
{'T', 19}, {'U', 20}, {'V', 21}, {'W', 22}, {'X', 23}, {'Y', 24}, {'Z', 25}, {'a', 26}, {'b', 27}, {'c', 28},
{'d', 29}, {'e', 30}, {'f', 31}, {'g', 32}, {'h', 33}, {'i', 34}, {'j', 35}, {'k', 36}, {'l', 37}, {'m', 38},
{'n', 39}, {'o', 40}, {'p', 41}, {'q', 42}, {'r', 43}, {'s', 44}, {'t', 45}, {'u', 46}, {'v', 47}, {'w', 48},
{'x', 49}, {'y', 50}, {'z', 51}, {'0', 52}, {'1', 53}, {'2', 54}, {'3', 55}, {'4', 56}, {'5', 57}, {'6', 58},
{'7', 59}, {'8', 60}, {'9', 61}, {'-', 62}, {'_', 63}})
.collect(Collectors.toMap(data -> (Character) data[0], data -> (Integer) data[1]));

private static Pattern BITSTRING_VERIFICATION_PATTERN = Pattern.compile("^[0-1]*$", Pattern.CASE_INSENSITIVE);
private static Pattern BASE64URL_VERIFICATION_PATTERN =
Pattern.compile("^[A-Za-z0-9\\-_]*$", Pattern.CASE_INSENSITIVE);
.collect(Collectors.toMap(data -> (Character) data[0], data -> (Integer) data[1])));

public String encode(String bitString) throws EncodingException {
// should only be 0 or 1
if (!BITSTRING_VERIFICATION_PATTERN.matcher(bitString).matches()) {
if (isInvalidBitString(bitString)) {
throw new EncodingException("Unencodable Base64Url '" + bitString + "'");
}

bitString = pad(bitString);

String str = "";
StringBuilder sb = new StringBuilder();

int index = 0;
while (index <= bitString.length() - 6) {
String s = bitString.substring(index, index + 6);

try {
int n = FixedIntegerEncoder.decode(s);
Character c = AbstractBase64UrlEncoder.DICT.charAt(n);
str += c;
char c = AbstractBase64UrlEncoder.DICT.charAt(n);
sb.append(c);
index += 6;
} catch (DecodingException e) {
throw new EncodingException("Unencodable Base64Url '" + bitString + "'");
}
}

return str;
return sb.toString();
}

public String decode(String str) throws DecodingException {
// should contain only characters from the base64url set
if (!BASE64URL_VERIFICATION_PATTERN.matcher(str).matches()) {
throw new DecodingException("Undecodable Base64URL string");
}

String bitString = "";
StringBuilder bitString = new StringBuilder();

for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
Integer n = AbstractBase64UrlEncoder.REVERSE_DICT.get(c);
int n = AbstractBase64UrlEncoder.REVERSE_DICT.getOrDefault(c, -1);
if (n == -1) {
throw new DecodingException("Undecodable Base64URL string");
}
String s = FixedIntegerEncoder.encode(n, 6);
bitString += s;
bitString.append(s);
}

return bitString;
return bitString.toString();
}

public static boolean isInvalidBitString(String bitString) {
for(int i = 0; i < bitString.length(); i++) {
char testChar = bitString.charAt(i);
if (testChar != '0' && testChar != '1') {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.iab.gpp.encoder.datatype.encoder;

import static com.iab.gpp.encoder.datatype.encoder.AbstractBase64UrlEncoder.isInvalidBitString;

import java.util.regex.Pattern;
import com.iab.gpp.encoder.error.DecodingException;

public class FixedIntegerEncoder {

private static Pattern BITSTRING_VERIFICATION_PATTERN = Pattern.compile("^[0-1]*$", Pattern.CASE_INSENSITIVE);

public static String encode(int value, int bitStringLength) {
// let bitString = value.toString(2);

String bitString = "";
StringBuilder bitString = new StringBuilder();
while (value > 0) {
if ((value & 1) == 1) {
bitString = "1" + bitString;
bitString.insert(0, "1");
} else {
bitString = "0" + bitString;
bitString.insert(0, "0");
}
value = value >> 1;
}

while (bitString.length() < bitStringLength) {
bitString = "0" + bitString;
bitString.insert(0, "0");
}

return bitString;
return bitString.toString();
}

public static int decode(String bitString) throws DecodingException {
if (!BITSTRING_VERIFICATION_PATTERN.matcher(bitString).matches()) {
if (isInvalidBitString(bitString)) {
throw new DecodingException("Undecodable FixedInteger '" + bitString + "'");
}

Expand Down