[CSHARP-161] Preallocating a list when reading a large message Created: 27/Jan/11  Updated: 02/Apr/15  Resolved: 27/Jan/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.0

Type: Improvement Priority: Major - P3
Reporter: Aristarkh Zagorodnikov Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I suggest to improve MongoReplyMessage<TDocument>.ReadFrom(BsonBuffer) by preallocating a list of documents, since its size is known, and doing it after error checking (so we don't allocate anything if cursor died, etc.).

— MongoReplyMessage.cs.orig Thu Jan 27 18:36:48 2011
+++ MongoReplyMessage.cs Thu Jan 27 18:36:55 2011
@@ -74,7 +74,6 @@
cursorId = buffer.ReadInt64();
startingFrom = buffer.ReadInt32();
numberReturned = buffer.ReadInt32();

  • documents = new List<TDocument>();

var settings = new BsonBinaryReaderSettings

{ MaxDocumentSize = server.MaxDocumentSize }

;
BsonReader bsonReader = BsonReader.Create(buffer, settings);
@@ -89,6 +88,7 @@
throw new MongoQueryException(message);
}

+ documents = new List<TDocument>(numberReturned);
while (buffer.Position - messageStartPosition < messageLength) {
var document = BsonSerializer.Deserialize<TDocument>(bsonReader);
documents.Add(document);



 Comments   
Comment by Robert Stam [ 27/Jan/11 ]

Implemented. Set the initial capacity of the documents list to the number of documents returned.

Generated at Wed Feb 07 21:35:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.