Skip to content

Commit 1d28478

Browse files
oschwaldclaude
andcommitted
Optimize UINT64/UINT128 decoding to avoid BigInteger allocation
For UINT64/UINT128 values less than 8 bytes (which fit in long's positive range), decode directly to long instead of BigInteger when the target is a typed field. This avoids unnecessary BigInteger object allocation for common cases like UINT64 values under 2^56. Maintains backward compatibility by preserving BigInteger return type for Object.class targets (e.g., Map decoding). Performance impact: - UINT64(500) into Long field: decodes as long (no BigInteger allocation) - UINT64(500) into Object/Map: still returns BigInteger (backward compatible) - UINT64 >= 8 bytes: always uses BigInteger (correctness) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent c20e678 commit 1d28478

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ private <T> Object decodeByType(
244244
return coerceFromInt(this.decodeInt32(size), cls);
245245
case UINT64:
246246
case UINT128:
247+
// Optimization: for typed fields, avoid BigInteger allocation when
248+
// value fits in long. Keep Object.class behavior unchanged for
249+
// backward compatibility.
250+
if (size < 8 && !cls.equals(Object.class)) {
251+
return coerceFromLong(this.decodeLong(size), cls);
252+
}
253+
// Size >= 8 bytes or Object.class target: use BigInteger
247254
return coerceFromBigInteger(this.decodeBigInteger(size), cls);
248255
default:
249256
throw new InvalidDatabaseException(

0 commit comments

Comments
 (0)