Browse Source

Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily

csharptest 10 years ago
parent
commit
3faaac1b9a

+ 30 - 0
src/ProtocolBuffers.Test/CodedInputStreamTest.cs

@@ -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);
+            }
+        }
     }
 }

+ 1 - 1
src/ProtocolBuffers/CodedInputStream.cs

@@ -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.