Optimize RawBsonDocument encoding

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: Performance
    • None
    • Java Drivers
    • 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:
            Unassigned
            Reporter:
            Slav Babanin
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: