Skip to content

Commit e55e7b4

Browse files
committed
Cache enum's value array for a speed increase
1 parent 73c021d commit e55e7b4

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/main/java/com/maxmind/maxminddb/Decoder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ final class Decoder {
3333
static enum Type {
3434
EXTENDED, POINTER, UTF8_STRING, DOUBLE, BYTES, UINT16, UINT32, MAP, INT32, UINT64, UINT128, ARRAY, CONTAINER, END_MARKER, BOOLEAN, FLOAT;
3535

36+
// Java clones the array when you call values(). Caching it increased
37+
// the speed by about 5000 requests per second on my machine.
38+
final static Type[] values = Type.values();
39+
3640
public static Type get(int i) {
37-
// XXX - Type.values() might be expensive. Consider caching it.
38-
return Type.values()[i];
41+
return Type.values[i];
3942
}
4043

4144
private static Type get(byte b) {
4245
// bytes are signed, but we want to treat them as unsigned here
43-
// XXX - Type.values() might be expensive. Consider caching it.
4446
return Type.get(b & 0xFF);
4547
}
4648

0 commit comments

Comments
 (0)