Skip to content

Commit

Permalink
CSHARP-5348: Cache Bson*Context.PushContext
Browse files Browse the repository at this point in the history
  • Loading branch information
obligaron committed Oct 21, 2024
1 parent 57b9359 commit d33f424
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 29 deletions.
2 changes: 1 addition & 1 deletion 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 Down
19 changes: 15 additions & 4 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 @@ -86,7 +87,17 @@ public BsonBinaryReaderContext PopContext(long position)

internal BsonBinaryReaderContext PushContext(ContextType contextType, long startPosition, long size)
{
return new BsonBinaryReaderContext(this, contextType, startPosition, 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;
}
}
}
13 changes: 11 additions & 2 deletions 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 @@ -63,7 +64,15 @@ internal BsonBinaryWriterContext PopContext()

internal BsonBinaryWriterContext PushContext(ContextType contextType, long startPosition)
{
return new BsonBinaryWriterContext(this, contextType, startPosition);
if (_cachedPushContext == null)
_cachedPushContext = new BsonBinaryWriterContext(this, contextType, startPosition);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._startPosition = startPosition;
_cachedPushContext._index = 0;
}
return _cachedPushContext;
}
}
}
25 changes: 21 additions & 4 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 @@ -127,12 +130,26 @@ public BsonDocumentReaderContext PopContext()

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

internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document)
{
return new BsonDocumentReaderContext(this, contextType, 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;
}
}
}
38 changes: 24 additions & 14 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 @@ -39,20 +40,14 @@ internal BsonDocumentWriterContext(
private BsonDocumentWriterContext(
BsonDocumentWriterContext parentContext,
ContextType contextType,
BsonArray array)
{
_parentContext = parentContext;
_contextType = contextType;
_array = array;
}

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 @@ -95,17 +90,32 @@ internal BsonDocumentWriterContext PopContext()

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

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

internal BsonDocumentWriterContext PushContext(ContextType contextType, string code)
{
return new BsonDocumentWriterContext(this, contextType, 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;
}
}
}
9 changes: 7 additions & 2 deletions src/MongoDB.Bson/IO/JsonReaderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
internal class JsonReaderContext
{
// private fields
private JsonReaderContext _parentContext;
private readonly JsonReaderContext _parentContext;
private JsonReaderContext _cachedPushContext;
private ContextType _contextType;

// constructors
Expand Down Expand Up @@ -56,7 +57,11 @@ public JsonReaderContext PopContext()

internal JsonReaderContext PushContext(ContextType contextType)
{
return new JsonReaderContext(this, contextType);
if (_cachedPushContext == null)
_cachedPushContext = new JsonReaderContext(this, contextType);
else
_cachedPushContext._contextType = contextType;
return _cachedPushContext;
}
}
}
12 changes: 10 additions & 2 deletions src/MongoDB.Bson/IO/JsonWriterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
internal class JsonWriterContext
{
// private fields
private JsonWriterContext _parentContext;
private readonly JsonWriterContext _parentContext;
private JsonWriterContext _cachedPushContext;
private ContextType _contextType;
private string _indentation;
private bool _hasElements = false;
Expand Down Expand Up @@ -60,7 +61,14 @@ internal JsonWriterContext PopContext()

internal JsonWriterContext PushContext(ContextType contextType, string indentChars)
{
return new JsonWriterContext(this, contextType, indentChars);
if (_cachedPushContext == null)
_cachedPushContext = new JsonWriterContext(this, contextType, indentChars);
else
{
_cachedPushContext._contextType = contextType;
_cachedPushContext._hasElements = false;
}
return _cachedPushContext;
}
}
}

0 comments on commit d33f424

Please sign in to comment.