Skip to content

Commit 4f91e8d

Browse files
committed
CSHARP-5348: Cache Bson*Context.PushContext
1 parent e3923ca commit 4f91e8d

7 files changed

+89
-29
lines changed

benchmarks/MongoDB.Driver.Benchmarks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# C# Driver Benchmark Suite
22

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

55
## Running the Driver Benchmarks
66

src/MongoDB.Bson/IO/BsonBinaryReaderContext.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ internal class BsonBinaryReaderContext
2121
{
2222
// private fields
2323
private readonly BsonBinaryReaderContext _parentContext;
24-
private readonly ContextType _contextType;
25-
private readonly long _startPosition;
26-
private readonly long _size;
24+
private BsonBinaryReaderContext _cachedPushContext;
25+
private ContextType _contextType;
26+
private long _startPosition;
27+
private long _size;
2728
private string _currentElementName;
2829
private int _currentArrayIndex = -1;
2930

@@ -86,7 +87,17 @@ public BsonBinaryReaderContext PopContext(long position)
8687

8788
internal BsonBinaryReaderContext PushContext(ContextType contextType, long startPosition, long size)
8889
{
89-
return new BsonBinaryReaderContext(this, contextType, startPosition, size);
90+
if (_cachedPushContext == null)
91+
_cachedPushContext = new BsonBinaryReaderContext(this, contextType, startPosition, size);
92+
else
93+
{
94+
_cachedPushContext._contextType = contextType;
95+
_cachedPushContext._startPosition = startPosition;
96+
_cachedPushContext._size = size;
97+
_cachedPushContext._currentArrayIndex = -1;
98+
_cachedPushContext._currentElementName = null;
99+
}
100+
return _cachedPushContext;
90101
}
91102
}
92103
}

src/MongoDB.Bson/IO/BsonBinaryWriterContext.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
1818
internal class BsonBinaryWriterContext
1919
{
2020
// private fields
21-
private BsonBinaryWriterContext _parentContext;
21+
private readonly BsonBinaryWriterContext _parentContext;
22+
private BsonBinaryWriterContext _cachedPushContext;
2223
private ContextType _contextType;
2324
private long _startPosition;
2425
private int _index; // used when contextType is Array
@@ -63,7 +64,15 @@ internal BsonBinaryWriterContext PopContext()
6364

6465
internal BsonBinaryWriterContext PushContext(ContextType contextType, long startPosition)
6566
{
66-
return new BsonBinaryWriterContext(this, contextType, startPosition);
67+
if (_cachedPushContext == null)
68+
_cachedPushContext = new BsonBinaryWriterContext(this, contextType, startPosition);
69+
else
70+
{
71+
_cachedPushContext._contextType = contextType;
72+
_cachedPushContext._startPosition = startPosition;
73+
_cachedPushContext._index = 0;
74+
}
75+
return _cachedPushContext;
6776
}
6877
}
6978
}

src/MongoDB.Bson/IO/BsonDocumentReaderContext.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ namespace MongoDB.Bson.IO
1818
internal class BsonDocumentReaderContext
1919
{
2020
// private fields
21-
private BsonDocumentReaderContext _parentContext;
21+
private readonly BsonDocumentReaderContext _parentContext;
22+
private BsonDocumentReaderContext _cachedPushContext;
2223
private ContextType _contextType;
2324
private BsonDocument _document;
2425
private BsonArray _array;
2526
private int _index;
2627

2728
// constructors
28-
internal BsonDocumentReaderContext(
29+
private BsonDocumentReaderContext(
2930
BsonDocumentReaderContext parentContext,
3031
ContextType contextType,
32+
BsonDocument document,
3133
BsonArray array)
3234
{
3335
_parentContext = parentContext;
3436
_contextType = contextType;
37+
_document = document;
3538
_array = array;
3639
}
3740

@@ -127,12 +130,26 @@ public BsonDocumentReaderContext PopContext()
127130

128131
internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonArray array)
129132
{
130-
return new BsonDocumentReaderContext(this, contextType, array);
133+
return PushContext(contextType, null, array);
131134
}
132135

133136
internal BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document)
134137
{
135-
return new BsonDocumentReaderContext(this, contextType, document);
138+
return PushContext(contextType, document, null);
139+
}
140+
141+
private BsonDocumentReaderContext PushContext(ContextType contextType, BsonDocument document, BsonArray array)
142+
{
143+
if (_cachedPushContext == null)
144+
_cachedPushContext = new BsonDocumentReaderContext(this, contextType, document, array);
145+
else
146+
{
147+
_cachedPushContext._contextType = contextType;
148+
_cachedPushContext._document = document;
149+
_cachedPushContext._array = array;
150+
_cachedPushContext._index = 0;
151+
}
152+
return _cachedPushContext;
136153
}
137154
}
138155
}

src/MongoDB.Bson/IO/BsonDocumentWriterContext.cs

+24-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
1818
internal class BsonDocumentWriterContext
1919
{
2020
// private fields
21-
private BsonDocumentWriterContext _parentContext;
21+
private readonly BsonDocumentWriterContext _parentContext;
22+
private BsonDocumentWriterContext _cachedPushContext;
2223
private ContextType _contextType;
2324
private BsonDocument _document;
2425
private BsonArray _array;
@@ -39,20 +40,14 @@ internal BsonDocumentWriterContext(
3940
private BsonDocumentWriterContext(
4041
BsonDocumentWriterContext parentContext,
4142
ContextType contextType,
42-
BsonArray array)
43-
{
44-
_parentContext = parentContext;
45-
_contextType = contextType;
46-
_array = array;
47-
}
48-
49-
private BsonDocumentWriterContext(
50-
BsonDocumentWriterContext parentContext,
51-
ContextType contextType,
43+
BsonDocument document,
44+
BsonArray array,
5245
string code)
5346
{
5447
_parentContext = parentContext;
5548
_contextType = contextType;
49+
_document = document;
50+
_array = array;
5651
_code = code;
5752
}
5853

@@ -95,17 +90,32 @@ internal BsonDocumentWriterContext PopContext()
9590

9691
internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonDocument document)
9792
{
98-
return new BsonDocumentWriterContext(this, contextType, document);
93+
return PushContext(contextType, document, null, null);
9994
}
10095

10196
internal BsonDocumentWriterContext PushContext(ContextType contextType, BsonArray array)
10297
{
103-
return new BsonDocumentWriterContext(this, contextType, array);
98+
return PushContext(contextType, null, array, null);
10499
}
105100

106101
internal BsonDocumentWriterContext PushContext(ContextType contextType, string code)
107102
{
108-
return new BsonDocumentWriterContext(this, contextType, code);
103+
return PushContext(contextType, null, null, code);
104+
}
105+
106+
private BsonDocumentWriterContext PushContext(ContextType contextType, BsonDocument document, BsonArray array, string code)
107+
{
108+
if (_cachedPushContext == null)
109+
_cachedPushContext = new BsonDocumentWriterContext(this, contextType, document, array, code);
110+
else
111+
{
112+
_cachedPushContext._contextType = contextType;
113+
_cachedPushContext._document = document;
114+
_cachedPushContext._array = array;
115+
_cachedPushContext._code = code;
116+
_cachedPushContext._name = null;
117+
}
118+
return _cachedPushContext;
109119
}
110120
}
111121
}

src/MongoDB.Bson/IO/JsonReaderContext.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
1818
internal class JsonReaderContext
1919
{
2020
// private fields
21-
private JsonReaderContext _parentContext;
21+
private readonly JsonReaderContext _parentContext;
22+
private JsonReaderContext _cachedPushContext;
2223
private ContextType _contextType;
2324

2425
// constructors
@@ -56,7 +57,11 @@ public JsonReaderContext PopContext()
5657

5758
internal JsonReaderContext PushContext(ContextType contextType)
5859
{
59-
return new JsonReaderContext(this, contextType);
60+
if (_cachedPushContext == null)
61+
_cachedPushContext = new JsonReaderContext(this, contextType);
62+
else
63+
_cachedPushContext._contextType = contextType;
64+
return _cachedPushContext;
6065
}
6166
}
6267
}

src/MongoDB.Bson/IO/JsonWriterContext.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace MongoDB.Bson.IO
1818
internal class JsonWriterContext
1919
{
2020
// private fields
21-
private JsonWriterContext _parentContext;
21+
private readonly JsonWriterContext _parentContext;
22+
private JsonWriterContext _cachedPushContext;
2223
private ContextType _contextType;
2324
private string _indentation;
2425
private bool _hasElements = false;
@@ -60,7 +61,14 @@ internal JsonWriterContext PopContext()
6061

6162
internal JsonWriterContext PushContext(ContextType contextType, string indentChars)
6263
{
63-
return new JsonWriterContext(this, contextType, indentChars);
64+
if (_cachedPushContext == null)
65+
_cachedPushContext = new JsonWriterContext(this, contextType, indentChars);
66+
else
67+
{
68+
_cachedPushContext._contextType = contextType;
69+
_cachedPushContext._hasElements = false;
70+
}
71+
return _cachedPushContext;
6472
}
6573
}
6674
}

0 commit comments

Comments
 (0)