Optimize RawBsonDocument encoding

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Unknown
    • 5.8.0
    • Affects Version/s: None
    • Component/s: Performance
    • None
    • Java Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Optimize RawBsonDocument encoding by piping already-serialized BSON bytes directly into the writer, eliminating intermediate wrapper allocations in the encode path.

      Today, each RawBsonDocument encode allocates multiple wrapper objects (ByteBuffer, ByteBufNIO, ByteBufferBsonInput, BsonBinaryReader) just to copy bytes that are already serialized. In large batches (up to ~300k documents), this becomes millions of short-lived allocations and adds avoidable CPU/GC overhead.

      Also we could remove the extra full-document byte[] copy in BsonBinaryWriter.pipeDocument(), where the document body is allocated into a temporary byte[], copied from input, then copied again to output. This impacts both RawBsonDocumentCodec.encode() and RawBsonDocumentCodec.decode().

      Proposed changes:

      • Add BsonWriter.pipe(byte[], int, int) to write raw BSON bytes directly (no reader/input wrappers).
      • Implement the pipe fast-path in BsonBinaryWriter to write directly to the output buffer.
      • Expose RawBsonDocument backing bytes via public getters (backing array, offset, length).
      • Update RawBsonDocumentCodec.encode() to use the pipe path instead of creating wrapper objects per encode.
      • Remove the full-document temporary byte[] copy in BsonBinaryWriter.pipeDocument() (affects both encode and decode paths).
      • Add BsonInput.pipe(BsonOutput, int) to enable direct buffer-to-buffer transfer (replacing the double copy).

      Acceptance criteria

      • Encoding/decoding RawBsonDocument avoids constructing intermediate (ByteBuffer, ByteBufNIO, ByteBufferBsonInput, BsonBinaryReader) wrappers solely for copying already-serialized bytes.
      • Remove the full-document temporary byte[] copy in the pipe path and replace with direct transfer.

            Assignee:
            Slav Babanin
            Reporter:
            Slav Babanin
            Almas Abdrazak
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: