@@ -32,7 +32,7 @@ public class BsonBinaryReader : BsonReader
3232#pragma warning restore CA2213 // Disposable never disposed
3333 private readonly BsonStream _bsonStream ;
3434 private BsonBinaryReaderContext _context ;
35- private readonly Lazy < Stack < BsonBinaryReaderContext > > _contexts = new ( ( ) => new ( ) ) ;
35+ private readonly Stack < BsonBinaryReaderContext > _contexts = new ( 4 ) ;
3636
3737 // constructors
3838 /// <summary>
@@ -113,7 +113,7 @@ public override void Close()
113113 /// </summary>
114114 /// <returns>A bookmark.</returns>
115115 public override BsonReaderBookmark GetBookmark ( ) =>
116- new BsonBinaryReaderBookmark ( State , CurrentBsonType , CurrentName , _context , _contexts . Value . ToArray ( ) , _bsonStream . Position ) ;
116+ new BsonBinaryReaderBookmark ( State , CurrentBsonType , CurrentName , _context , _contexts , _bsonStream . Position ) ;
117117
118118 /// <summary>
119119 /// Determines whether this reader is at end of file.
@@ -342,7 +342,7 @@ public override void ReadEndArray()
342342 ThrowInvalidState ( "ReadEndArray" , BsonReaderState . EndOfArray ) ;
343343 }
344344
345- _context = PopContext ( ) ;
345+ PopContext ( ) ;
346346
347347 switch ( _context . ContextType )
348348 {
@@ -372,10 +372,10 @@ public override void ReadEndDocument()
372372 ThrowInvalidState ( "ReadEndDocument" , BsonReaderState . EndOfDocument ) ;
373373 }
374374
375- _context = PopContext ( ) ;
375+ PopContext ( ) ;
376376 if ( _context . ContextType == ContextType . JavaScriptWithScope )
377377 {
378- _context = PopContext ( ) ; // JavaScriptWithScope
378+ PopContext ( ) ; // JavaScriptWithScope
379379 }
380380 switch ( _context . ContextType )
381381 {
@@ -434,8 +434,7 @@ public override string ReadJavaScriptWithScope()
434434 var startPosition = _bsonStream . Position ; // position of size field
435435 var size = ReadSize ( ) ;
436436
437- PushContext ( _context ) ;
438- _context = new BsonBinaryReaderContext ( ContextType . JavaScriptWithScope , startPosition , size ) ;
437+ PushContext ( new ( ContextType . JavaScriptWithScope , startPosition , size ) ) ;
439438
440439 var code = _bsonStream . ReadString ( Settings . Encoding ) ;
441440
@@ -557,7 +556,7 @@ public override IByteBuffer ReadRawBsonDocument()
557556
558557 if ( _context . ContextType == ContextType . JavaScriptWithScope )
559558 {
560- _context = PopContext ( ) ; // JavaScriptWithScope
559+ PopContext ( ) ; // JavaScriptWithScope
561560 }
562561 switch ( _context . ContextType )
563562 {
@@ -595,8 +594,7 @@ public override void ReadStartArray()
595594 var startPosition = _bsonStream . Position ; // position of size field
596595 var size = ReadSize ( ) ;
597596
598- PushContext ( _context ) ;
599- _context = new ( ContextType . Array , startPosition , size ) ;
597+ PushContext ( new ( ContextType . Array , startPosition , size ) ) ;
600598
601599 State = BsonReaderState . Type ;
602600 }
@@ -612,8 +610,9 @@ public override void ReadStartDocument()
612610 var contextType = ( State == BsonReaderState . ScopeDocument ) ? ContextType . ScopeDocument : ContextType . Document ;
613611 var startPosition = _bsonStream . Position ; // position of size field
614612 var size = ReadSize ( ) ;
615- PushContext ( _context ) ;
616- _context = new ( contextType , startPosition , size ) ;
613+
614+ PushContext ( new ( contextType , startPosition , size ) ) ;
615+
617616 State = BsonReaderState . Type ;
618617 }
619618
@@ -670,18 +669,11 @@ public override void ReadUndefined()
670669 public override void ReturnToBookmark ( BsonReaderBookmark bookmark )
671670 {
672671 var binaryReaderBookmark = ( BsonBinaryReaderBookmark ) bookmark ;
672+
673673 State = binaryReaderBookmark . State ;
674674 CurrentBsonType = binaryReaderBookmark . CurrentBsonType ;
675675 CurrentName = binaryReaderBookmark . CurrentName ;
676-
677- _context = binaryReaderBookmark . CurrentContext ;
678-
679- _contexts . Value . Clear ( ) ;
680- foreach ( var context in binaryReaderBookmark . ContextsStack . Reverse ( ) )
681- {
682- _contexts . Value . Push ( context ) ;
683- }
684-
676+ _context = binaryReaderBookmark . RestoreContext ( _contexts ) ;
685677 _bsonStream . Position = binaryReaderBookmark . Position ;
686678 }
687679
@@ -761,7 +753,7 @@ protected override void Dispose(bool disposing)
761753 {
762754 Close ( ) ;
763755 }
764- catch { } // ignore exceptions
756+ catch { /* ignore exceptions */ }
765757 }
766758 base . Dispose ( disposing ) ;
767759 }
@@ -790,7 +782,7 @@ private string GenerateDottedElementName()
790782 elementName = "?" ;
791783 }
792784
793- return GenerateDottedElementName ( _contexts . Value . ToArray ( ) , 0 , elementName ) ;
785+ return GenerateDottedElementName ( _contexts . ToArray ( ) , 0 , elementName ) ;
794786 }
795787
796788 private string GenerateDottedElementName ( BsonBinaryReaderContext [ ] contexts , int currentContextIndex , string elementName )
@@ -851,20 +843,21 @@ private int ReadSize()
851843 return size ;
852844 }
853845
854- private BsonBinaryReaderContext PopContext ( )
846+ private void PopContext ( )
855847 {
856848 var actualSize = _bsonStream . Position - _context . StartPosition ;
857849 if ( actualSize != _context . Size )
858850 {
859851 throw new FormatException ( $ "Expected size to be { _context . Size } , not { actualSize } .") ;
860852 }
861853
862- return _contexts . Value . Pop ( ) ;
854+ _context = _contexts . Pop ( ) ;
863855 }
864856
865- private void PushContext ( BsonBinaryReaderContext context )
857+ private void PushContext ( BsonBinaryReaderContext newContext )
866858 {
867- _contexts . Value . Push ( context ) ;
859+ _contexts . Push ( _context ) ;
860+ _context = newContext ;
868861 }
869862 }
870863}
0 commit comments