Commit 3faaac1b authored by csharptest's avatar csharptest Committed by rogerk

Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily

parent f6a0c116
......@@ -37,6 +37,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
......@@ -605,5 +606,34 @@ namespace Google.ProtocolBuffers
Assert.AreEqual(4, unk.Count);
}
//Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily
[TestMethod]
public void TestSlowPathAvoidance()
{
using (var ms = new MemoryStream())
{
CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
output.WriteField(FieldType.Bytes, 1, "bytes", ByteString.CopyFrom(new byte[100]));
output.WriteField(FieldType.Bytes, 2, "bytes", ByteString.CopyFrom(new byte[100]));
output.Flush();
ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms, new byte[ms.Length / 2]);
uint tag;
string ignore;
ByteString value;
Assert.IsTrue(input.ReadTag(out tag, out ignore));
Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
value = ByteString.Empty;
Assert.IsTrue(input.ReadBytes(ref value) && value.Length == 100);
Assert.IsTrue(input.ReadTag(out tag, out ignore));
Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
value = ByteString.Empty;
Assert.IsTrue(input.ReadBytes(ref value) && value.Length == 100);
}
}
}
}
\ No newline at end of file
......@@ -427,7 +427,7 @@ namespace Google.ProtocolBuffers
public bool ReadBytes(ref ByteString value)
{
int size = (int) ReadRawVarint32();
if (size < bufferSize - bufferPos && size > 0)
if (size <= bufferSize - bufferPos && size > 0)
{
// Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it.
......
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