From 6a60ac33d0e90f25f7e9ac84917dd92b2e11619d Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 27 Jan 2009 14:47:35 +0000 Subject: [PATCH] String optimisations --- src/ProtocolBuffers/CodedInputStream.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs index b7fe9c3394..78d9251946 100644 --- a/src/ProtocolBuffers/CodedInputStream.cs +++ b/src/ProtocolBuffers/CodedInputStream.cs @@ -219,16 +219,19 @@ namespace Google.ProtocolBuffers { /// public String ReadString() { 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 // just copy directly from it. String result = Encoding.UTF8.GetString(buffer, bufferPos, size); bufferPos += size; 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)); } ///