Skip to content

ByteBufBsonDocument and RawBsonDocument simplifications#1902

Open
rozza wants to merge 1 commit intomongodb:ByteBuffrom
rozza:ByteBufBsonDocumentClone
Open

ByteBufBsonDocument and RawBsonDocument simplifications#1902
rozza wants to merge 1 commit intomongodb:ByteBuffrom
rozza:ByteBufBsonDocumentClone

Conversation

@rozza
Copy link
Member

@rozza rozza commented Feb 26, 2026

Rationale

ByteBufBsonDocument#clone used to return a RawBsonDocument. The recent changes returned a normal BsonDocument, which is potentially expensive depending on its usage.

The ByteBufBsonDocument changes also added complex iterator logic, when RawBsonDocument deferred to BsonDocument iterators. As iteration is essentially a hydrating mechanism, there is opportunity for improvements for both implementations. By changing the RawBsonDocument iterators to be more efficient, ByteBufBsonDocument can now utilize these efficiency gains by proxy, relying on the cachedDocument iterators.

This change both reduces the complexity of ByteBufBsonDocument and relies on an improved RawBsonDocument implementation.

Summary of changes

  • ByteBufBsonDocument:

    • Simplify by returning RawBsonDocument from toBsonDocument, avoiding full BSON deserialization. When there are no sequence fields, the body bytes are cloned directly. When sequence fields exist, BsonBinaryWriter.pipe() merges the body with sequence arrays efficiently.
    • Use toBsonDocument for iterators. This eliminates the need for custom iterators (IteratorMode, CombinedIterator, createBodyIterator, and sequence iterators) since entrySet/values/keySet now delegate to the cached RawBsonDocument.
  • RawBsonDocument:

    • Renamed toBaseBsonDocument to override the default toBsonDocument implementation.
    • Implemented the iterators so that they don't need to fully convert the document to a BsonDocument.
  • Tests:

    • Updated ByteBufBsonDocumentTest iteration tests.
    • Updated ByteBufBsonArrayTest#fromValues as entrySet now returns RawBsonDocument instances.

JAVA-6010

### Rationale

`ByteBufBsonDocument#clone` used to return a `RawBsonDocument`.
The recent changes returned a normal `BsonDocument`, which is potentially
expensive depending on its usage.

The `ByteBufBsonDocument` changes also added complex iterator logic, when
`RawBsonDocument` deferred to `BsonDocument` iterators. As iteration is
essentially a hydrating mechanism, there is opportunity for improvements
for both implementations. By changing the `RawBsonDocument` iterators to
be more efficient, `ByteBufBsonDocument` can now utilize these efficiency
gains by proxy, relying on the `cachedDocument` iterators.

This change both reduces the complexity of `ByteBufBsonDocument` and relies
on an improved `RawBsonDocument` implementation.

### Summary of changes

* **`ByteBufBsonDocument`**:
  * Simplify by returning `RawBsonDocument` from `toBsonDocument`, avoiding
    full BSON deserialization. When there are no sequence fields, the body
    bytes are cloned directly. When sequence fields exist,
    `BsonBinaryWriter.pipe()` merges the body with sequence arrays efficiently.
  * Use `toBsonDocument` for iterators. This eliminates the need for custom
    iterators (`IteratorMode`, `CombinedIterator`, `createBodyIterator`, and
    sequence iterators) since `entrySet`/`values`/`keySet` now delegate to the
    cached `RawBsonDocument`.

* **`RawBsonDocument`**:
  * Renamed `toBaseBsonDocument` to override the default `toBsonDocument`
    implementation.
  * Implemented the iterators so that they don't need to fully convert the
    document to a `BsonDocument`.

* **Tests**:
  * Updated `ByteBufBsonDocumentTest` iteration tests.
  * Updated `ByteBufBsonArrayTest#fromValues` as `entrySet` now returns
    `RawBsonDocument` instances.

JAVA-6010

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rozza rozza requested a review from strogiyotec February 26, 2026 13:49
@rozza rozza requested a review from a team as a code owner February 26, 2026 13:49
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