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

[JAVA-4816] perf: improving performance of ObjectId parseHexString() #1034

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion bson/src/main/org/bson/types/ObjectId.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,24 @@ private static byte[] parseHexString(final String s) {

byte[] b = new byte[OBJECT_ID_LENGTH];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
char c1 = s.charAt(i * 2);
char c2 = s.charAt(i * 2 + 1);
b[i] = hexCharToByte(c1) * 16 + hexCharToByte(c2)
}
return b;
}

private static byte hexCharToByte(char c) {
if (c >= '0' && c <= '9') {
return c - 48;
} else if (c >= 'a' && c <= 'f') {
return c - 87;
} else if (c >= 'A' && c <= 'F') {
return c - 55;
}
throw new IllegalArgumentException("invalid hexadecimal character: [" + c + "]");
}

private static int dateToTimestampSeconds(final Date time) {
return (int) (time.getTime() / 1000);
}
Expand Down