Fix RawBsonDocument encoding performance regression#1888
Draft
vbabanin wants to merge 1 commit intomongodb:mainfrom
Draft
Fix RawBsonDocument encoding performance regression#1888vbabanin wants to merge 1 commit intomongodb:mainfrom
vbabanin wants to merge 1 commit intomongodb:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 nestedRawBsonDocumentinsideBsonDocument / BsonArraybecauseRawBsonDocumentandBsonDocumentboth reportBsonType.DOCUMENT. NestedRawBsonDocumentswere encoded viaBsonDocumentCodec, triggering full deserialize and re-serialize instead of thewriter.pipe(reader)byte-copy path.Changes:
instanceof RawBsonDocumentcheck before the BsonType lookup. This keeps the BsonType fast path for the common case while correctly routing the special case forRawBsonDocument.RawBsonNestedEncodingBenchmarkandRawBsonArrayEncodingBenchmarkto CI to prevent regressions for themongot.Why not revert the optimization?
Reverting to class-based lookup would fix correctness but lose the performance gains from PR #1632 (
BsonTypelookup is an array index vsConcurrentHashMaplookup). The instanceof check is negligible and only affects paths withRawBsonDocument.