Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-161

Preallocating a list when reading a large message

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.0
    • 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);

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            onyxmaster Aristarkh Zagorodnikov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: