Optimize BsonArray Index encoding by vbabanin · Pull Request #1673 · mongodb/mongo-java-driver

Instead of caching precomputed String values, we now cache their corresponding byte[] representations. This avoids repeated ASCII string decoding.

The single contiguous buffer approach provides better cache locality in loops, reduced memory overhead and better performance.

The array index caching implementation consumes approximately 12 KB of memory in total:

ARRAY_INDEXES_BUFFER: ~3,890 bytes
Numbers 0-9: 10 × (1 byte + null terminator) = 20 bytes
Numbers 10-99: 90 × (2 byte + null terminator) = 270 bytes
Numbers 100-999: 900 × (3 bytes + null terminator) = 3,600 bytes
ARRAY_INDEXES_OFFSETS: 4,000 bytes
1,000 integers × 4 bytes per int = 4,000 bytes
ARRAY_INDEXES_LENGTHS: 4,000 bytes
1,000 integers × 4 bytes per int = 4,000 bytes

Array object headers: ~48 bytes

BsonArrayCodecBenchmark results:

Metric Before After Change
ops/s 13583.483 20046.810 +47.5%

JAVA-5836