Commit 6a60ac33 authored by Jon Skeet's avatar Jon Skeet

String optimisations

parent 49fcd4f7
...@@ -219,16 +219,19 @@ namespace Google.ProtocolBuffers { ...@@ -219,16 +219,19 @@ namespace Google.ProtocolBuffers {
/// </summary> /// </summary>
public String ReadString() { public String ReadString() {
int size = (int) ReadRawVarint32(); int size = (int) ReadRawVarint32();
if (size < bufferSize - bufferPos && size > 0) { // No need to read any data for an empty string.
if (size == 0) {
return "";
}
if (size <= bufferSize - bufferPos) {
// Fast path: We already have the bytes in a contiguous buffer, so // Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it. // just copy directly from it.
String result = Encoding.UTF8.GetString(buffer, bufferPos, size); String result = Encoding.UTF8.GetString(buffer, bufferPos, size);
bufferPos += size; bufferPos += size;
return result; return result;
} else {
// Slow path: Build a byte array first then copy it.
return Encoding.UTF8.GetString(ReadRawBytes(size));
} }
// Slow path: Build a byte array first then copy it.
return Encoding.UTF8.GetString(ReadRawBytes(size));
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment