Skip to content

Optimize BsonArray Index encoding #1673

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

Merged
merged 3 commits into from
Apr 3, 2025
Merged

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin commented Apr 3, 2025

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

@vbabanin vbabanin marked this pull request as ready for review April 3, 2025 01:12
@vbabanin vbabanin requested a review from rozza April 3, 2025 01:12
@vbabanin vbabanin self-assigned this Apr 3, 2025
@vbabanin vbabanin changed the title Optimize Array Index encoding Optimize BsonArray Index encoding Apr 3, 2025
@vbabanin
Copy link
Member Author

vbabanin commented Apr 3, 2025

As @ShaneHarvey noted in the previous PR (#1664 (comment)), Python caches 1000 consecutive array indexes. To align with that behavior across drivers, I’ve adjusted the cache size to 1000 in the Java driver.

private static final int ARRAY_INDEXES_CACHE_SIZE = 256;
private static final String[] ARRAY_INDEXES_CACHE = new String[ARRAY_INDEXES_CACHE_SIZE];
private static final int ARRAY_INDEXES_CACHE_SIZE = 1000;
private static final byte[] ARRAY_INDEXES_BUFFER;
Copy link
Member Author

@vbabanin vbabanin Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In prior benchmarks, the reference-chasing layout (i.e., byte[][]) showed ~25% lower throughput compared to a flat byte[] layout, primarily due to fragmented sequential locality and indirect memory access.

Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -66,7 +66,7 @@ public void shouldThrowWhenMaxDocumentSizeIsExceeded() {
writer.writeEndDocument();
fail();
} catch (BsonMaximumSizeExceededException e) {
assertEquals("Document size of 1037 is larger than maximum of 1024.", e.getMessage());
assertEquals("Document size of 12917 is larger than maximum of 12904.", e.getMessage());
Copy link
Member

@rozza rozza Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this get missed last time? Yes it did - thanks for fixing.

@vbabanin vbabanin merged commit c15e14a into mongodb:main Apr 3, 2025
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants