Skip to content

Fix RawBsonDocument encoding performance regression#1888

Draft
vbabanin wants to merge 1 commit intomongodb:mainfrom
vbabanin:JAVA-6101
Draft

Fix RawBsonDocument encoding performance regression#1888
vbabanin wants to merge 1 commit intomongodb:mainfrom
vbabanin:JAVA-6101

Conversation

@vbabanin
Copy link
Member

PR #1632 optimized codec lookup from class-based (codecRegistry.get(value.getClass())) to BsonType-based (bsonTypeCodecMap.get(value.getBsonType())). This is faster, but it regressed encoding of nested RawBsonDocument inside BsonDocument / BsonArray because RawBsonDocument and BsonDocument both report BsonType.DOCUMENT. Nested RawBsonDocuments were encoded via BsonDocumentCodec, triggering full deserialize and re-serialize instead of the writer.pipe(reader) byte-copy path.

Changes:

  • Add a targeted special-case instanceof RawBsonDocument check before the BsonType lookup. This keeps the BsonType fast path for the common case while correctly routing the special case for RawBsonDocument.
  • Added RawBsonNestedEncodingBenchmark and RawBsonArrayEncodingBenchmark to CI to prevent regressions for the mongot.

Why not revert the optimization?

Reverting to class-based lookup would fix correctness but lose the performance gains from PR #1632 (BsonType lookup is an array index vs ConcurrentHashMap lookup). The instanceof check is negligible and only affects paths with RawBsonDocument.

Add instanceof check in BsonDocumentCodec and BsonArrayCodec to route
RawBsonDocument values to RawBsonDocumentCodec, restoring efficient
byte-copy encoding. Previous BsonType-based lookup led to sub-optimal
performance as it could not distinguish RawBsonDocument from BsonDocument.

JAVA-6101
@vbabanin vbabanin self-assigned this Feb 14, 2026
@vbabanin vbabanin requested a review from rozza February 14, 2026 02:04
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.

1 participant

Comments