Skip to content

Conversation

@BorisDog
Copy link
Contributor

This PR introduces ReadOnlyMemoryBsonReader, an alternative to BinaryBsonReader. ReadOnlyMemoryBsonReader provides a more lightweight BSON deserialization from a continuous single chunk buffers.
Preliminary testing shows an improvement of ~50% in our deserialization benchmarks.

@BorisDog BorisDog requested a review from a team as a code owner November 21, 2025 23:29
@BorisDog BorisDog added the improvement Optimizations or refactoring (no new features or fixes). label Nov 21, 2025
Copilot finished reviewing on behalf of BorisDog November 21, 2025 23:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces ReadOnlyMemoryBsonReader, a new, optimized BSON reader implementation designed for continuous single-chunk buffers. The reader provides approximately 50% improvement in deserialization benchmarks compared to BinaryBsonReader.

Key changes:

  • New ReadOnlyMemoryBsonReader class that directly works with ReadOnlyMemory<byte> instead of streams
  • New ReadOnlyMemoryBuffer implementation of IByteBuffer for read-only memory
  • Updated existing BSON deserialization code to use BsonBinaryReaderUtils.CreateBinaryReader() which automatically selects the optimized reader when possible
  • Enhanced name decoders (Utf8NameDecoder, TrieNameDecoder) to support span-based decoding
  • Various refactorings to reduce nesting and improve code readability

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
ReadOnlyMemoryBsonReader.cs New optimized BSON reader implementation for contiguous memory
ReadOnlyMemoryBuffer.cs New read-only byte buffer wrapper for ReadOnlyMemory<byte>
ReadOnlyMemoryReaderSettings.cs Settings class for the new reader
BsonBinaryReaderUtils.cs Factory utilities for creating appropriate reader implementations
Utf8Helper.cs, Utf8NameDecoder.cs, TrieNameDecoder.cs Span-based decoding support
RawBsonDocument.cs, RawBsonArray.cs, LazyBsonDocument.cs, LazyBsonArray.cs Refactored to use factory method for reader creation
ObjectId.cs Refactored to use ReadOnlySpan<byte> for parsing
BsonBinaryReader.cs Moved common utilities to BsonBinaryReaderUtils
Various operation files Simplified using statements and refactored to use factory method
Comments suppressed due to low confidence (1)

src/MongoDB.Bson/IO/ReadOnlyMemoryBsonReader.cs:1

  • Double forward slashes in XML documentation comment. Should be /// <param name="requiredBsonType">The required BSON type.</param> with three slashes, not four.
/* Copyright 2010-present MongoDB Inc.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@BorisDog BorisDog requested review from papafe and rstam and removed request for ajcvickers November 21, 2025 23:45
@codeowners-service-app
Copy link

codeowners-service-app bot commented Nov 21, 2025

Assigned mongoKart for team dbx-csharp-dotnet because rstam is out of office.
Assigned jordan-smith721 for team dbx-csharp-dotnet because papafe is out of office.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Optimizations or refactoring (no new features or fixes).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant