Skip to content

Fix RawBsonDocument encoding performance regression#1888

Merged
vbabanin merged 3 commits intomongodb:mainfrom
vbabanin:JAVA-6101
Feb 24, 2026
Merged

Fix RawBsonDocument encoding performance regression#1888
vbabanin merged 3 commits intomongodb:mainfrom
vbabanin:JAVA-6101

Conversation

@vbabanin
Copy link
Copy Markdown
Member

@vbabanin vbabanin commented Feb 14, 2026

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.

Performance test results

  • Compared 4.11.x baseline vs this change.
  • Used 10 patches per side (20 total); compared mean-to-mean.
  • Result: ~13-15% throughput improvement on the RawBsonDocument nested encoding benchmarks.

Interpretation: the prior regression is eliminated, and we also see an additional ~13-15% improvement relative to 4.11.x.

Test Measurement Mean Z-Score Bas Percent Differ
Full RawBsonDocument Array in BsonDocument BSON Encoding BSON Encoding ops_per_sec 4,941.4442 4.44 13.11%
Full RawBsonDocument in BsonDocument BSON Encoding BSON Encoding ops_per_sec 3,145.7113 8.06 15.54%

JAVA-6101

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
@vbabanin vbabanin marked this pull request as ready for review February 20, 2026 22:50
@vbabanin vbabanin requested a review from a team as a code owner February 20, 2026 22:50
Copy link
Copy Markdown
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

@vbabanin vbabanin merged commit 0ba1687 into mongodb:main Feb 24, 2026
55 checks passed
rozza added a commit that referenced this pull request Mar 24, 2026
* Revert NettyByteBuf.asReadOnly change (#1871)

Originally introduced in 057649f
This change had the unintended side effect of leaking netty ByteBufs when logging.

JAVA-5982

* Update Netty dependency to the latest version. (#1867)

JAVA-5818

* Adjust timeout handling in client-side operations to account for RTT variations (#1793)

JAVA-5375
---------
Co-authored-by: Ross Lawley <ross@mongodb.com>

* Update Snappy version for the latest security fixes. (#1868)

JAVA-6069

* Bson javadoc improvements (#1883)

Fixed no comment warning in BinaryVector
Improved BsonBinary asUuid documentation
Improved BsonBinarySubType isUuid documentation

JAVA-6086

* Make NettyByteBuf share parent reference count. (#1891)

JAVA-6107

* JAVA-5907 (#1893)

* JAVA-5907

* JAVA-5907 use execute within executor service

If we don't use the return value from executor then we should use `execute`  instead of `submit`

* format

* revert error log for netty leak

---------

Co-authored-by: Almas Abdrazak <abdrazak.almas@mongodb.com>

* Fix RawBsonDocument encoding performance regression (#1888)

Add instanceof check in BsonDocumentCodec to route RawBsonDocument 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

* Update specifications to latest (#1884)

JAVA-6092

* Evergreen atlas search fix (#1894)

Update evergreen atlas-deployed-task-group configuration
Assume test secrets and follow the driver-evergreen-tools
atlas recommended usage:

https://github.com/mongodb-labs/drivers-evergreen-tools/tree/master/.evergreen/atlas#usage

JAVA-6103

* [JAVA-6028] Add Micrometer/OpenTelemetry tracing support to the reactive-streams (#1898)

* Add Micrometer/OpenTelemetry tracing support to the reactive-streams driver https://jira.mongodb.org/browse/JAVA-6028

Port the tracing infrastructure from the sync driver to driver-reactive-streams, reusing the existing driver-core, TracingManager, Span, and TraceContext classes.

* Move error handling and span lifecycle (span.error(), span.end()) from Reactor's doOnError/doFinally operators into the async callback, before emitting the result to the subscriber.

* Making sure span is properly closed when an exception occurs

* Clone command event document before storing to prevent use-after-free. (#1901)

* Version: bump 5.7.0-beta1

* Version: bump 5.7.0-SNAPSHOT

* Remove unneeded variable

---------

Co-authored-by: Viacheslav Babanin <slav.babanin@mongodb.com>
Co-authored-by: Almas Abdrazak <almas337519@gmail.com>
Co-authored-by: Almas Abdrazak <abdrazak.almas@mongodb.com>
Co-authored-by: Nabil Hachicha <nabil.hachicha@mongodb.com>
Co-authored-by: Nabil Hachicha <1793238+nhachicha@users.noreply.github.com>
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