Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSHARP-5348: Avoid allocations for Bson*Context #1509

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/MongoDB.Driver.Benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# C# Driver Benchmark Suite

This suite implements the benchmarks described in this [spec](https://github.com/mongodb/specifications/blob/master/source/benchmarking/benchmarking.rst).
This suite implements the benchmarks described in this [spec](https://github.com/mongodb/specifications/blob/master/source/benchmarking/benchmarking.md).

## Running the Driver Benchmarks

Expand All @@ -13,7 +13,7 @@ This suite implements the benchmarks described in this [spec](https://github.com
(e.g `dotnet run -c Release -- --driverBenchmarks --envVars MONGODB_URI:"ConnectionString"`)

You can also select the benchmarks to run directly on the command for running the benchmarks as such
`dotnet run -c Release -- --driverBenchmarks --fitler "*BenchmarkClassName*"`. The benchmarks are also grouped into categories namely: BSONBench, WriteBench
`dotnet run -c Release -- --driverBenchmarks --filter "*BenchmarkClassName*"`. The benchmarks are also grouped into categories namely: BSONBench, WriteBench
ReadBench, ParallelBench, SingleBench, MultiBench and DriverBench. So if you wanted to only run the WriteBench benchmarks, you can do so
as follows: `dotnet run -c Release -- --driverBenchmarks --anyCategories "WriteBench"`.

Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB.Bson/IO/BsonBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public override string ReadJavaScriptWithScope()

var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = new BsonBinaryReaderContext(_context, ContextType.JavaScriptWithScope, startPosition, size);
_context = _context.PushContext(ContextType.JavaScriptWithScope, startPosition, size);
var code = _bsonStream.ReadString(Settings.Encoding);

State = BsonReaderState.ScopeDocument;
Expand Down Expand Up @@ -590,7 +590,7 @@ public override void ReadStartArray()

var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = new BsonBinaryReaderContext(_context, ContextType.Array, startPosition, size);
_context = _context.PushContext(ContextType.Array, startPosition, size);
State = BsonReaderState.Type;
}

Expand All @@ -605,7 +605,7 @@ public override void ReadStartDocument()
var contextType = (State == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = new BsonBinaryReaderContext(_context, contextType, startPosition, size);
_context = _context.PushContext(contextType, startPosition, size);
State = BsonReaderState.Type;
}

Expand Down
22 changes: 19 additions & 3 deletions src/MongoDB.Bson/IO/BsonBinaryReaderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ internal class BsonBinaryReaderContext
{
// private fields
private readonly BsonBinaryReaderContext _parentContext;
private readonly ContextType _contextType;
private readonly long _startPosition;
private readonly long _size;
private BsonBinaryReaderContext _cachedPushContext;
private ContextType _contextType;
private long _startPosition;
private long _size;
private string _currentElementName;
private int _currentArrayIndex = -1;

Expand Down Expand Up @@ -83,5 +84,20 @@ public BsonBinaryReaderContext PopContext(long position)
}
return _parentContext;
}

internal BsonBinaryReaderContext PushContext(ContextType contextType, long startPosition, long size)
{
if (_cachedPushContext == null)
_cachedPushContext = new BsonBinaryReaderContext(this, contextType, startPosition, size);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._startPosition = startPosition;
_cachedPushContext._size = size;
_cachedPushContext._currentArrayIndex = -1;
_cachedPushContext._currentElementName = null;
}
return _cachedPushContext;
}
}
}
15 changes: 9 additions & 6 deletions src/MongoDB.Bson/IO/BsonBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public override void WriteEndArray()
_bsonStream.WriteByte(0);
BackpatchSize(); // size of document

_context = _context.ParentContext;
_context = _context.PopContext();
State = GetNextState();
}

Expand All @@ -313,7 +313,7 @@ public override void WriteEndDocument()
_bsonStream.WriteByte(0);
BackpatchSize(); // size of document

_context = _context.ParentContext;
_context = _context.PopContext();
if (_context == null)
{
State = BsonWriterState.Done;
Expand All @@ -323,7 +323,7 @@ public override void WriteEndDocument()
if (_context.ContextType == ContextType.JavaScriptWithScope)
{
BackpatchSize(); // size of the JavaScript with scope value
_context = _context.ParentContext;
_context = _context.PopContext();
}
State = GetNextState();
}
Expand Down Expand Up @@ -400,7 +400,7 @@ public override void WriteJavaScriptWithScope(string code)

_bsonStream.WriteBsonType(BsonType.JavaScriptWithScope);
WriteNameHelper();
_context = new BsonBinaryWriterContext(_context, ContextType.JavaScriptWithScope, _bsonStream.Position);
_context = _context.PushContext(ContextType.JavaScriptWithScope, _bsonStream.Position);
_bsonStream.WriteInt32(0); // reserve space for size of JavaScript with scope value
_bsonStream.WriteString(code, Settings.Encoding);

Expand Down Expand Up @@ -564,7 +564,7 @@ public override void WriteStartArray()
base.WriteStartArray();
_bsonStream.WriteBsonType(BsonType.Array);
WriteNameHelper();
_context = new BsonBinaryWriterContext(_context, ContextType.Array, _bsonStream.Position);
_context = _context.PushContext(ContextType.Array, _bsonStream.Position);
_bsonStream.WriteInt32(0); // reserve space for size

State = BsonWriterState.Value;
Expand All @@ -588,7 +588,10 @@ public override void WriteStartDocument()
WriteNameHelper();
}
var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
_context = new BsonBinaryWriterContext(_context, contextType, _bsonStream.Position);
if (_context == null)
_context = new BsonBinaryWriterContext(null, contextType, _bsonStream.Position);
else
_context = _context.PushContext(contextType, _bsonStream.Position);
_bsonStream.WriteInt32(0); // reserve space for size

State = BsonWriterState.Name;
Expand Down
21 changes: 20 additions & 1 deletion src/MongoDB.Bson/IO/BsonBinaryWriterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
internal class BsonBinaryWriterContext
{
// private fields
private BsonBinaryWriterContext _parentContext;
private readonly BsonBinaryWriterContext _parentContext;
private BsonBinaryWriterContext _cachedPushContext;
private ContextType _contextType;
private long _startPosition;
private int _index; // used when contextType is Array
Expand Down Expand Up @@ -55,5 +56,23 @@ internal int Index
get { return _index; }
set { _index = value; }
}

internal BsonBinaryWriterContext PopContext()
{
return _parentContext;
}

internal BsonBinaryWriterContext PushContext(ContextType contextType, long startPosition)
{
if (_cachedPushContext == null)
_cachedPushContext = new BsonBinaryWriterContext(this, contextType, startPosition);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._startPosition = startPosition;
_cachedPushContext._index = 0;
}
return _cachedPushContext;
}
}
}
4 changes: 2 additions & 2 deletions src/MongoDB.Bson/IO/BsonDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public override void ReadStartArray()
VerifyBsonType("ReadStartArray", BsonType.Array);

var array = _currentValue.AsBsonArray;
_context = new BsonDocumentReaderContext(_context, ContextType.Array, array);
_context = _context.PushContext(ContextType.Array, array);
State = BsonReaderState.Type;
}

Expand All @@ -435,7 +435,7 @@ public override void ReadStartDocument()
{
document = _currentValue.AsBsonDocument;
}
_context = new BsonDocumentReaderContext(_context, ContextType.Document, document);
_context = _context.PushContext(ContextType.Document, document);
State = BsonReaderState.Type;
}

Expand Down
31 changes: 29 additions & 2 deletions src/MongoDB.Bson/IO/BsonDocumentReaderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ namespace MongoDB.Bson.IO
internal class BsonDocumentReaderContext
{
// private fields
private BsonDocumentReaderContext _parentContext;
private readonly BsonDocumentReaderContext _parentContext;
private BsonDocumentReaderContext _cachedPushContext;
private ContextType _contextType;
private BsonDocument _document;
private BsonArray _array;
private int _index;

// constructors
internal BsonDocumentReaderContext(
private BsonDocumentReaderContext(
BsonDocumentReaderContext parentContext,
ContextType contextType,
BsonDocument document,
BsonArray array)
{
_parentContext = parentContext;
_contextType = contextType;
_document = document;
_array = array;
}

Expand Down Expand Up @@ -124,5 +127,29 @@ public BsonDocumentReaderContext PopContext()
{
return _parentContext;
}

internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonArray array)
{
return PushContext(contextType, null, array);
}

internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document)
{
return PushContext(contextType, document, null);
}

private BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document, BsonArray array)
{
if (_cachedPushContext == null)
_cachedPushContext = new BsonDocumentReaderContext(this, contextType, document, array);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._document = document;
_cachedPushContext._array = array;
_cachedPushContext._index = 0;
}
return _cachedPushContext;
}
}
}
16 changes: 8 additions & 8 deletions src/MongoDB.Bson/IO/BsonDocumentWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override void WriteEndArray()

base.WriteEndArray();
var array = _context.Array;
_context = _context.ParentContext;
_context = _context.PopContext();
WriteValue(array);
State = GetNextState();
}
Expand All @@ -219,15 +219,15 @@ public override void WriteEndDocument()
if (_context.ContextType == ContextType.ScopeDocument)
{
var scope = _context.Document;
_context = _context.ParentContext;
_context = _context.PopContext();
var code = _context.Code;
_context = _context.ParentContext;
_context = _context.PopContext();
WriteValue(new BsonJavaScriptWithScope(code, scope));
}
else
{
var document = _context.Document;
_context = _context.ParentContext;
_context = _context.PopContext();
if (_context != null)
{
WriteValue(document);
Expand Down Expand Up @@ -304,7 +304,7 @@ public override void WriteJavaScriptWithScope(string code)
ThrowInvalidState("WriteJavaScriptWithScope", BsonWriterState.Value);
}

_context = new BsonDocumentWriterContext(_context, ContextType.JavaScriptWithScope, code);
_context = _context.PushContext(ContextType.JavaScriptWithScope, code);
State = BsonWriterState.ScopeDocument;
}

Expand Down Expand Up @@ -407,7 +407,7 @@ public override void WriteStartArray()
}

base.WriteStartArray();
_context = new BsonDocumentWriterContext(_context, ContextType.Array, new BsonArray());
_context = _context.PushContext(ContextType.Array, new BsonArray());
State = BsonWriterState.Value;
}

Expand All @@ -430,10 +430,10 @@ public override void WriteStartDocument()
_context = new BsonDocumentWriterContext(null, ContextType.Document, _document);
break;
case BsonWriterState.Value:
_context = new BsonDocumentWriterContext(_context, ContextType.Document, new BsonDocument());
_context = _context.PushContext(ContextType.Document, new BsonDocument());
break;
case BsonWriterState.ScopeDocument:
_context = new BsonDocumentWriterContext(_context, ContextType.ScopeDocument, new BsonDocument());
_context = _context.PushContext(ContextType.ScopeDocument, new BsonDocument());
break;
default:
throw new BsonInternalException("Unexpected state.");
Expand Down
54 changes: 42 additions & 12 deletions src/MongoDB.Bson/IO/BsonDocumentWriterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
internal class BsonDocumentWriterContext
{
// private fields
private BsonDocumentWriterContext _parentContext;
private readonly BsonDocumentWriterContext _parentContext;
private BsonDocumentWriterContext _cachedPushContext;
private ContextType _contextType;
private BsonDocument _document;
private BsonArray _array;
Expand All @@ -36,23 +37,17 @@ internal BsonDocumentWriterContext(
_document = document;
}

internal BsonDocumentWriterContext(
BsonDocumentWriterContext parentContext,
ContextType contextType,
BsonArray array)
{
_parentContext = parentContext;
_contextType = contextType;
_array = array;
}

internal BsonDocumentWriterContext(
private BsonDocumentWriterContext(
BsonDocumentWriterContext parentContext,
ContextType contextType,
BsonDocument document,
BsonArray array,
string code)
{
_parentContext = parentContext;
_contextType = contextType;
_document = document;
_array = array;
_code = code;
}

Expand Down Expand Up @@ -87,5 +82,40 @@ internal string Code
{
get { return _code; }
}

internal BsonDocumentWriterContext PopContext()
{
return _parentContext;
}

internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonDocument document)
{
return PushContext(contextType, document, null, null);
}

internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonArray array)
{
return PushContext(contextType, null, array, null);
}

internal BsonDocumentWriterContext PushContext(ContextType contextType, string code)
{
return PushContext(contextType, null, null, code);
}

private BsonDocumentWriterContext PushContext(ContextType contextType, BsonDocument document, BsonArray array, string code)
{
if (_cachedPushContext == null)
_cachedPushContext = new BsonDocumentWriterContext(this, contextType, document, array, code);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._document = document;
_cachedPushContext._array = array;
_cachedPushContext._code = code;
_cachedPushContext._name = null;
}
return _cachedPushContext;
}
}
}
Loading