-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
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);