Optimize Netty writeCharacters path

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: 3.7.1
    • Component/s: Performance
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Add an optimized ASCII write path in ByteBufferBsonOutput.writeCharacters for Netty buffers. Today, Netty (non-array-backed) buffers fall back to writeOnBuffers, which performs character-by-character relative put(byte) with inline UTF-8 encoding and buffer boundary checks in one complex loop.

      This optimization adds a dedicated ASCII fast-path for Netty buffers (tight loop, minimal branching, JIT-friendly).

      Evidence

      Netty transport insert benchmarks vs main show improvements with no regressions:

      • Large doc bulk insert: +14.89%
      • Large doc insertOne: +13.99%
      • Large doc Client BulkWrite insert: +10.59%
      • Small doc Collection BulkWrite insert: +5.83%
      • Small doc bulk insert: +5.66%
      • Small doc Client BulkWrite insert: +4.74%

      5/8 insert benchmarks show statistically significant improvement (z > 2). No regressions.

      Proposed Changes

      • Detect Netty-backed buffers in writeCharacters.
      • For ASCII, write bytes via ByteBuf.setByte(bufferPos + i, c) in a tight loop.
      • Fall back to existing path for non-ASCII.

      Acceptance Criteria

      • ASCII path for Netty ByteBuff - similarly to what has been done for JDK's ByteBuffer.
      • Non-ASCII behavior unchanged and correct.
      • Existing tests pass.
      • Netty insert benchmarks show the expected improvement trend with no regressions.

            Assignee:
            Unassigned
            Reporter:
            Slav Babanin
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: