mongodb/mongo-csharp-driver — pull request #105 by optimiz3 (2012-05-01 22:05:20+00:00)
Title: Improve deserialization performance by an additional ~8% (Common case string decoding)

ReadCString is one of the top CPU consumers during deserialization because 2 O(N) passes are performed to calculate string length plus an additional O(N) pass to copy memory. Performance can be significantly improved by special casing empty, single character, and the "_id" strings during deserialization. Doing so eliminates transient string memory allocations, and is done using a single O(1) lookup. This improves Dictionaries encoded as ArrayOfDocuments ({ k, v }) and general purpose users that are already following the MongoDB performance best practice of using single character field names.

This following scenario is special cased in ReadCString:
1. The "_id" string

This following scenarios are special cased in both ReadCString and ReadString:
1. Empty strings
2. Single character strings (by definition ASCII when dealing with UTF8)

Other notes:
1. Fixed an off-by-one bug in ReadString in a call to EnsureDataAvailable.
2. Switched to a UTF8Encoding that will throw on invalid characters rather than corrupting the returned data.