Browse Source

Merge pull request #6847 from jtattermusch/optimizations_backport

Backport wrapper optimizations and microbenchmarks (to 3.10.x)
Jan Tattermusch 5 years ago
parent
commit
c18477fab2

+ 8 - 5
Makefile.am

@@ -79,12 +79,15 @@ csharp_EXTRA_DIST=                                                           \
   csharp/src/AddressBook/ListPeople.cs                                       \
   csharp/src/AddressBook/ListPeople.cs                                       \
   csharp/src/AddressBook/Program.cs                                          \
   csharp/src/AddressBook/Program.cs                                          \
   csharp/src/AddressBook/SampleUsage.cs                                      \
   csharp/src/AddressBook/SampleUsage.cs                                      \
-  csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs               \
-  csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs            \
-  csharp/src/Google.Protobuf.Benchmarks/Program.cs                           \
-  csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj    \
-  csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs                        \
   csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs           \
   csharp/src/Google.Protobuf.Benchmarks/BenchmarkMessage1Proto3.cs           \
+  csharp/src/Google.Protobuf.Benchmarks/Benchmarks.cs                        \
+  csharp/src/Google.Protobuf.Benchmarks/Google.Protobuf.Benchmarks.csproj    \
+  csharp/src/Google.Protobuf.Benchmarks/Program.cs                           \
+  csharp/src/Google.Protobuf.Benchmarks/SerializationBenchmark.cs            \
+  csharp/src/Google.Protobuf.Benchmarks/SerializationConfig.cs               \
+  csharp/src/Google.Protobuf.Benchmarks/wrapper_benchmark_messages.proto     \
+  csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmark.cs                  \
+  csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmarkMessages.cs          \
   csharp/src/Google.Protobuf.Conformance/Conformance.cs                      \
   csharp/src/Google.Protobuf.Conformance/Conformance.cs                      \
   csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj  \
   csharp/src/Google.Protobuf.Conformance/Google.Protobuf.Conformance.csproj  \
   csharp/src/Google.Protobuf.Conformance/Program.cs                          \
   csharp/src/Google.Protobuf.Conformance/Program.cs                          \

+ 5 - 0
csharp/generate_protos.sh

@@ -70,3 +70,8 @@ $PROTOC -Ibenchmarks \
   benchmarks/datasets/google_message1/proto3/*.proto \
   benchmarks/datasets/google_message1/proto3/*.proto \
   benchmarks/benchmarks.proto \
   benchmarks/benchmarks.proto \
   --csharp_out=csharp/src/Google.Protobuf.Benchmarks
   --csharp_out=csharp/src/Google.Protobuf.Benchmarks
+
+# C# only benchmark protos
+$PROTOC -Isrc -Icsharp/src/Google.Protobuf.Benchmarks \
+  csharp/src/Google.Protobuf.Benchmarks/*.proto \
+  --csharp_out=csharp/src/Google.Protobuf.Benchmarks

+ 9 - 6
csharp/src/Google.Protobuf.Benchmarks/Program.cs

@@ -34,13 +34,16 @@ using BenchmarkDotNet.Running;
 
 
 namespace Google.Protobuf.Benchmarks
 namespace Google.Protobuf.Benchmarks
 {
 {
-    /// <summary>
-    /// Entry point, that currently runs the sole benchmark we have.
-    /// Eventually we might want to be able to specify a particular dataset
-    /// from the command line.
-    /// </summary>
     class Program
     class Program
     {
     {
-        static void Main() => BenchmarkRunner.Run<SerializationBenchmark>();
+        // typical usage: dotnet run -c Release -f netcoreapp2.1
+        // (this can profile both .net core and .net framework; for some reason
+        // if you start from "-f net461", it goes horribly wrong)
+        public static void Main(string[] args)
+        {
+            BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
+        }
     }
     }
+
+    
 }
 }

+ 102 - 0
csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmark.cs

@@ -0,0 +1,102 @@
+#region Copyright notice and license
+// Protocol Buffers - Google's data interchange format
+// Copyright 2019 Google Inc.  All rights reserved.
+// https://github.com/protocolbuffers/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#endregion
+
+using BenchmarkDotNet.Attributes;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace Google.Protobuf.Benchmarks
+{
+    /// <summary>
+    /// Benchmark that tests serialization/deserialization of wrapper fields.
+    /// </summary>
+    [MemoryDiagnoser]
+    public class WrapperBenchmark
+    {
+        byte[] manyWrapperFieldsData;
+        byte[] manyPrimitiveFieldsData;
+
+        [GlobalSetup]
+        public void GlobalSetup()
+        {
+            manyWrapperFieldsData = CreateManyWrapperFieldsMessage().ToByteArray();
+            manyPrimitiveFieldsData = CreateManyPrimitiveFieldsMessage().ToByteArray();
+        }
+
+        [Benchmark]
+        public ManyWrapperFieldsMessage ParseWrapperFields()
+        {
+            return ManyWrapperFieldsMessage.Parser.ParseFrom(manyWrapperFieldsData);
+        }
+
+        [Benchmark]
+        public ManyPrimitiveFieldsMessage ParsePrimitiveFields()
+        {
+            return ManyPrimitiveFieldsMessage.Parser.ParseFrom(manyPrimitiveFieldsData);
+        }
+
+        private static ManyWrapperFieldsMessage CreateManyWrapperFieldsMessage()
+        {
+            // Example data match data of an internal benchmarks
+            return new ManyWrapperFieldsMessage()
+            {
+                Int64Field19 = 123,
+                Int64Field37 = 1000032,
+                Int64Field26 = 3453524500,
+                DoubleField79 = 1.2,
+                DoubleField25 = 234,
+                DoubleField9 = 123.3,
+                DoubleField28 = 23,
+                DoubleField7 = 234,
+                DoubleField50 = 2.45
+            };
+        }
+
+        private static ManyPrimitiveFieldsMessage CreateManyPrimitiveFieldsMessage()
+        {
+            // Example data match data of an internal benchmarks
+            return new ManyPrimitiveFieldsMessage()
+            {
+                Int64Field19 = 123,
+                Int64Field37 = 1000032,
+                Int64Field26 = 3453524500,
+                DoubleField79 = 1.2,
+                DoubleField25 = 234,
+                DoubleField9 = 123.3,
+                DoubleField28 = 23,
+                DoubleField7 = 234,
+                DoubleField50 = 2.45
+            };
+        }
+    }
+}

File diff suppressed because it is too large
+ 227 - 0
csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmarkMessages.cs


+ 237 - 0
csharp/src/Google.Protobuf.Benchmarks/wrapper_benchmark_messages.proto

@@ -0,0 +1,237 @@
+syntax = "proto3";
+
+package google.protobuf.benchmarks;
+
+import "google/protobuf/wrappers.proto";
+
+// a message that has a large number of wrapper fields
+// obfuscated version of an internal message
+message ManyWrapperFieldsMessage {
+  google.protobuf.DoubleValue double_field_95 = 95;
+  google.protobuf.DoubleValue double_field_1 = 1;
+  google.protobuf.DoubleValue double_field_79 = 79;
+  google.protobuf.Int64Value int64_field_2 = 2;
+  google.protobuf.DoubleValue double_field_96 = 96;
+  google.protobuf.Int64Value int64_field_3 = 3;
+  google.protobuf.Int64Value int64_field_4 = 4;
+  google.protobuf.DoubleValue double_field_97 = 97;
+  google.protobuf.DoubleValue double_field_65 = 65;
+  google.protobuf.DoubleValue double_field_66 = 66;
+  google.protobuf.DoubleValue double_field_7 = 7;
+  google.protobuf.DoubleValue double_field_62 = 62;
+  google.protobuf.DoubleValue double_field_118 = 118;
+  google.protobuf.DoubleValue double_field_119 = 119;
+  google.protobuf.DoubleValue double_field_67 = 67;
+  google.protobuf.DoubleValue double_field_120 = 120;
+  google.protobuf.DoubleValue double_field_121 = 121;
+  google.protobuf.DoubleValue double_field_122 = 122;
+  google.protobuf.DoubleValue double_field_123 = 123;
+  google.protobuf.DoubleValue double_field_124 = 124;
+  google.protobuf.DoubleValue double_field_8 = 8;
+  google.protobuf.DoubleValue double_field_9 = 9;
+  google.protobuf.DoubleValue double_field_98 = 98;
+  google.protobuf.DoubleValue double_field_10 = 10;
+  google.protobuf.DoubleValue double_field_11 = 11;
+  google.protobuf.DoubleValue double_field_99 = 99;
+  google.protobuf.DoubleValue double_field_84 = 84;
+  google.protobuf.DoubleValue double_field_14 = 14;
+  google.protobuf.DoubleValue double_field_77 = 77;
+  google.protobuf.DoubleValue double_field_15 = 15;
+  google.protobuf.Int64Value int64_field_19 = 19;
+  google.protobuf.Int64Value int64_field_115 = 115;
+  google.protobuf.DoubleValue double_field_116 = 116;
+  google.protobuf.Int64Value int64_field_117 = 117;
+  google.protobuf.DoubleValue double_field_20 = 20;
+  google.protobuf.DoubleValue double_field_21 = 21;
+  google.protobuf.StringValue string_field_73 = 73;
+  google.protobuf.StringValue string_field_74 = 74;
+  google.protobuf.DoubleValue double_field_22 = 22;
+  google.protobuf.DoubleValue double_field_69 = 69;
+  google.protobuf.DoubleValue double_field_70 = 70;
+  google.protobuf.DoubleValue double_field_71 = 71;
+  google.protobuf.DoubleValue double_field_72 = 72;
+  google.protobuf.DoubleValue double_field_25 = 25;
+  google.protobuf.Int64Value int64_field_26 = 26;
+  google.protobuf.DoubleValue double_field_68 = 68;
+  google.protobuf.DoubleValue double_field_28 = 28;
+  google.protobuf.DoubleValue double_field_106 = 106;
+  google.protobuf.DoubleValue double_field_29 = 29;
+  google.protobuf.DoubleValue double_field_30 = 30;
+  google.protobuf.DoubleValue double_field_101 = 101;
+  google.protobuf.DoubleValue double_field_102 = 102;
+  google.protobuf.DoubleValue double_field_103 = 103;
+  google.protobuf.DoubleValue double_field_104 = 104;
+  google.protobuf.DoubleValue double_field_105 = 105;
+  google.protobuf.DoubleValue double_field_31 = 31;
+  google.protobuf.Int64Value int64_field_32 = 32;
+  google.protobuf.DoubleValue double_field_75 = 75;
+  google.protobuf.DoubleValue double_field_129 = 129;
+  int32 enum_field_80 = 80;
+  int32 enum_field_81 = 81;
+  google.protobuf.Int64Value int64_field_82 = 82;
+  int32 enum_field_83 = 83;
+  google.protobuf.Int64Value int64_field_85 = 85;
+  google.protobuf.Int64Value int64_field_86 = 86;
+  google.protobuf.Int64Value int64_field_87 = 87;
+  google.protobuf.Int64Value int64_field_125 = 125;
+  google.protobuf.Int64Value int64_field_37 = 37;
+  google.protobuf.DoubleValue double_field_38 = 38;
+  google.protobuf.Int64Value interactions = 39;
+  repeated int32 repeated_int_field_100 = 100;
+  google.protobuf.DoubleValue double_field_40 = 40;
+  google.protobuf.Int64Value int64_field_41 = 41;
+  google.protobuf.Int64Value int64_field_126 = 126;
+  google.protobuf.Int64Value int64_field_127 = 127;
+  google.protobuf.DoubleValue double_field_128 = 128;
+  google.protobuf.DoubleValue double_field_109 = 109;
+  google.protobuf.Int64Value int64_field_110 = 110;
+  google.protobuf.DoubleValue double_field_111 = 111;
+  google.protobuf.Int64Value int64_field_112 = 112;
+  google.protobuf.DoubleValue double_field_113 = 113;
+  google.protobuf.Int64Value int64_field_114 = 114;
+  google.protobuf.DoubleValue double_field_42 = 42;
+  google.protobuf.Int64Value int64_field_43 = 43;
+  google.protobuf.Int64Value int64_field_44 = 44;
+  google.protobuf.DoubleValue double_field_45 = 45;
+  google.protobuf.DoubleValue double_field_46 = 46;
+  google.protobuf.DoubleValue double_field_78 = 78;
+  google.protobuf.DoubleValue double_field_88 = 88;
+  google.protobuf.DoubleValue double_field_47 = 47;
+  google.protobuf.DoubleValue double_field_89 = 89;
+  google.protobuf.DoubleValue double_field_48 = 48;
+  google.protobuf.DoubleValue double_field_49 = 49;
+  google.protobuf.DoubleValue double_field_50 = 50;
+  google.protobuf.DoubleValue double_field_90 = 90;
+  google.protobuf.DoubleValue double_field_51 = 51;
+  google.protobuf.DoubleValue double_field_91 = 91;
+  google.protobuf.DoubleValue double_field_92 = 92;
+  google.protobuf.Int64Value int64_field_107 = 107;
+  google.protobuf.DoubleValue double_field_93 = 93;
+  google.protobuf.DoubleValue double_field_108 = 108;
+  google.protobuf.DoubleValue double_field_52 = 52;
+  google.protobuf.DoubleValue double_field_53 = 53;
+  google.protobuf.DoubleValue double_field_94 = 94;
+  google.protobuf.DoubleValue double_field_54 = 54;
+  google.protobuf.DoubleValue double_field_55 = 55;
+  google.protobuf.DoubleValue double_field_56 = 56;
+  google.protobuf.DoubleValue double_field_57 = 57;
+  google.protobuf.DoubleValue double_field_58 = 58;
+  google.protobuf.Int64Value int64_field_59 = 59;
+  google.protobuf.Int64Value int64_field_60 = 60;
+}
+
+// same as ManyWrapperFieldsMessages, but with primitive fields
+// for comparison.
+message ManyPrimitiveFieldsMessage {
+  double double_field_95 = 95;
+  double double_field_1 = 1;
+  double double_field_79 = 79;
+  int64 int64_field_2 = 2;
+  double double_field_96 = 96;
+  int64 int64_field_3 = 3;
+  int64 int64_field_4 = 4;
+  double double_field_97 = 97;
+  double double_field_65 = 65;
+  double double_field_66 = 66;
+  double double_field_7 = 7;
+  double double_field_62 = 62;
+  double double_field_118 = 118;
+  double double_field_119 = 119;
+  double double_field_67 = 67;
+  double double_field_120 = 120;
+  double double_field_121 = 121;
+  double double_field_122 = 122;
+  double double_field_123 = 123;
+  double double_field_124 = 124;
+  double double_field_8 = 8;
+  double double_field_9 = 9;
+  double double_field_98 = 98;
+  double double_field_10 = 10;
+  double double_field_11 = 11;
+  double double_field_99 = 99;
+  double double_field_84 = 84;
+  double double_field_14 = 14;
+  double double_field_77 = 77;
+  double double_field_15 = 15;
+  int64 int64_field_19 = 19;
+  int64 int64_field_115 = 115;
+  double double_field_116 = 116;
+  int64 int64_field_117 = 117;
+  double double_field_20 = 20;
+  double double_field_21 = 21;
+  string string_field_73 = 73;
+  string string_field_74 = 74;
+  double double_field_22 = 22;
+  double double_field_69 = 69;
+  double double_field_70 = 70;
+  double double_field_71 = 71;
+  double double_field_72 = 72;
+  double double_field_25 = 25;
+  int64 int64_field_26 = 26;
+  double double_field_68 = 68;
+  double double_field_28 = 28;
+  double double_field_106 = 106;
+  double double_field_29 = 29;
+  double double_field_30 = 30;
+  double double_field_101 = 101;
+  double double_field_102 = 102;
+  double double_field_103 = 103;
+  double double_field_104 = 104;
+  double double_field_105 = 105;
+  double double_field_31 = 31;
+  int64 int64_field_32 = 32;
+  double double_field_75 = 75;
+  double double_field_129 = 129;
+  int32 enum_field_80 = 80;
+  int32 enum_field_81 = 81;
+  int64 int64_field_82 = 82;
+  int32 enum_field_83 = 83;
+  int64 int64_field_85 = 85;
+  int64 int64_field_86 = 86;
+  int64 int64_field_87 = 87;
+  int64 int64_field_125 = 125;
+  int64 int64_field_37 = 37;
+  double double_field_38 = 38;
+  int64 interactions = 39;
+  repeated int32 repeated_int_field_100 = 100;
+  double double_field_40 = 40;
+  int64 int64_field_41 = 41;
+  int64 int64_field_126 = 126;
+  int64 int64_field_127 = 127;
+  double double_field_128 = 128;
+  double double_field_109 = 109;
+  int64 int64_field_110 = 110;
+  double double_field_111 = 111;
+  int64 int64_field_112 = 112;
+  double double_field_113 = 113;
+  int64 int64_field_114 = 114;
+  double double_field_42 = 42;
+  int64 int64_field_43 = 43;
+  int64 int64_field_44 = 44;
+  double double_field_45 = 45;
+  double double_field_46 = 46;
+  double double_field_78 = 78;
+  double double_field_88 = 88;
+  double double_field_47 = 47;
+  double double_field_89 = 89;
+  double double_field_48 = 48;
+  double double_field_49 = 49;
+  double double_field_50 = 50;
+  double double_field_90 = 90;
+  double double_field_51 = 51;
+  double double_field_91 = 91;
+  double double_field_92 = 92;
+  int64 int64_field_107 = 107;
+  double double_field_93 = 93;
+  double double_field_108 = 108;
+  double double_field_52 = 52;
+  double double_field_53 = 53;
+  double double_field_94 = 94;
+  double double_field_54 = 54;
+  double double_field_55 = 55;
+  double double_field_56 = 56;
+  double double_field_57 = 57;
+  double double_field_58 = 58;
+  int64 int64_field_59 = 59;
+  int64 int64_field_60 = 60;
+}

+ 79 - 2
csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs

@@ -386,7 +386,7 @@ namespace Google.Protobuf.WellKnownTypes
         }
         }
 
 
         [Test]
         [Test]
-        public void UnknownFieldInWrapper()
+        public void UnknownFieldInWrapperInt32FastPath()
         {
         {
             var stream = new MemoryStream();
             var stream = new MemoryStream();
             var output = new CodedOutputStream(stream);
             var output = new CodedOutputStream(stream);
@@ -395,19 +395,96 @@ namespace Google.Protobuf.WellKnownTypes
             var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
             var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
 
 
             output.WriteTag(wrapperTag);
             output.WriteTag(wrapperTag);
-            output.WriteLength(4); // unknownTag + value 5 + valueType + value 6, each 1 byte
+            // Wrapper message is just long enough - 6 bytes - to use the wrapper fast-path.
+            output.WriteLength(6); // unknownTag + value 5 + valueType, each 1 byte, + value 65536, 3 bytes
             output.WriteTag(unknownTag);
             output.WriteTag(unknownTag);
             output.WriteInt32((int) valueTag); // Sneakily "pretend" it's a tag when it's really a value
             output.WriteInt32((int) valueTag); // Sneakily "pretend" it's a tag when it's really a value
             output.WriteTag(valueTag);
             output.WriteTag(valueTag);
+            output.WriteInt32(65536);
+            
+            output.Flush();
+            Assert.AreEqual(8, stream.Length); // tag (1 byte) + length (1 byte) + message (6 bytes)
+            stream.Position = 0;
+
+            var message = TestWellKnownTypes.Parser.ParseFrom(stream);
+            Assert.AreEqual(65536, message.Int32Field);
+        }
+
+        [Test]
+        public void UnknownFieldInWrapperInt32SlowPath()
+        {
+            var stream = new MemoryStream();
+            var output = new CodedOutputStream(stream);
+            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
+            var unknownTag = WireFormat.MakeTag(15, WireFormat.WireType.Varint);
+            var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
+
+            output.WriteTag(wrapperTag);
+            // Wrapper message is too short to be used on the wrapper fast-path.
+            output.WriteLength(4); // unknownTag + value 5 + valueType + value 6, each 1 byte
+            output.WriteTag(unknownTag);
+            output.WriteInt32((int)valueTag); // Sneakily "pretend" it's a tag when it's really a value
+            output.WriteTag(valueTag);
             output.WriteInt32(6);
             output.WriteInt32(6);
 
 
             output.Flush();
             output.Flush();
+            Assert.Less(stream.Length, 8); // tag (1 byte) + length (1 byte) + message
             stream.Position = 0;
             stream.Position = 0;
 
 
             var message = TestWellKnownTypes.Parser.ParseFrom(stream);
             var message = TestWellKnownTypes.Parser.ParseFrom(stream);
             Assert.AreEqual(6, message.Int32Field);
             Assert.AreEqual(6, message.Int32Field);
         }
         }
 
 
+        [Test]
+        public void UnknownFieldInWrapperInt64FastPath()
+        {
+            var stream = new MemoryStream();
+            var output = new CodedOutputStream(stream);
+            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int64FieldFieldNumber, WireFormat.WireType.LengthDelimited);
+            var unknownTag = WireFormat.MakeTag(15, WireFormat.WireType.Varint);
+            var valueTag = WireFormat.MakeTag(Int64Value.ValueFieldNumber, WireFormat.WireType.Varint);
+
+            output.WriteTag(wrapperTag);
+            // Wrapper message is just long enough - 10 bytes - to use the wrapper fast-path.
+            output.WriteLength(11); // unknownTag + value 5 + valueType, each 1 byte, + value 0xfffffffffffff, 8 bytes
+            output.WriteTag(unknownTag);
+            output.WriteInt64((int)valueTag); // Sneakily "pretend" it's a tag when it's really a value
+            output.WriteTag(valueTag);
+            output.WriteInt64(0xfffffffffffffL);
+
+            output.Flush();
+            Assert.AreEqual(13, stream.Length); // tag (1 byte) + length (1 byte) + message (11 bytes)
+            stream.Position = 0;
+
+            var message = TestWellKnownTypes.Parser.ParseFrom(stream);
+            Assert.AreEqual(0xfffffffffffffL, message.Int64Field);
+        }
+
+        [Test]
+        public void UnknownFieldInWrapperInt64SlowPath()
+        {
+            var stream = new MemoryStream();
+            var output = new CodedOutputStream(stream);
+            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int64FieldFieldNumber, WireFormat.WireType.LengthDelimited);
+            var unknownTag = WireFormat.MakeTag(15, WireFormat.WireType.Varint);
+            var valueTag = WireFormat.MakeTag(Int64Value.ValueFieldNumber, WireFormat.WireType.Varint);
+
+            output.WriteTag(wrapperTag);
+            // Wrapper message is too short to be used on the wrapper fast-path.
+            output.WriteLength(4); // unknownTag + value 5 + valueType + value 6, each 1 byte
+            output.WriteTag(unknownTag);
+            output.WriteInt64((int)valueTag); // Sneakily "pretend" it's a tag when it's really a value
+            output.WriteTag(valueTag);
+            output.WriteInt64(6);
+
+            output.Flush();
+            Assert.Less(stream.Length, 12); // tag (1 byte) + length (1 byte) + message
+            stream.Position = 0;
+
+            var message = TestWellKnownTypes.Parser.ParseFrom(stream);
+            Assert.AreEqual(6L, message.Int64Field);
+        }
+
         [Test]
         [Test]
         public void ClearWithReflection()
         public void ClearWithReflection()
         {
         {

+ 376 - 25
csharp/src/Google.Protobuf/CodedInputStream.cs

@@ -481,7 +481,33 @@ namespace Google.Protobuf
         /// </summary>
         /// </summary>
         public double ReadDouble()
         public double ReadDouble()
         {
         {
-            return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
+            if (bufferPos + 8 <= bufferSize)
+            {
+                if (BitConverter.IsLittleEndian)
+                {
+                    var result = BitConverter.ToDouble(buffer, bufferPos);
+                    bufferPos += 8;
+                    return result;
+                }
+                else
+                {
+                    var bytes = new byte[8];
+                    bytes[0] = buffer[bufferPos + 7];
+                    bytes[1] = buffer[bufferPos + 6];
+                    bytes[2] = buffer[bufferPos + 5];
+                    bytes[3] = buffer[bufferPos + 4];
+                    bytes[4] = buffer[bufferPos + 3];
+                    bytes[5] = buffer[bufferPos + 2];
+                    bytes[6] = buffer[bufferPos + 1];
+                    bytes[7] = buffer[bufferPos];
+                    bufferPos += 8;
+                    return BitConverter.ToDouble(bytes, 0);
+                }
+            }
+            else
+            {
+                return BitConverter.Int64BitsToDouble((long)ReadRawLittleEndian64());
+            }
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -711,7 +737,260 @@ namespace Google.Protobuf
             return false;
             return false;
         }
         }
 
 
-        #endregion
+        internal static float? ReadFloatWrapperLittleEndian(CodedInputStream input)
+        {
+            // length:1 + tag:1 + value:4 = 6 bytes
+            if (input.bufferPos + 6 <= input.bufferSize)
+            {
+                // The entire wrapper message is already contained in `buffer`.
+                int length = input.buffer[input.bufferPos];
+                if (length == 0)
+                {
+                    input.bufferPos++;
+                    return 0F;
+                }
+                // tag:1 + value:4 = length of 5 bytes
+                // field=1, type=32-bit = tag of 13
+                if (length != 5 || input.buffer[input.bufferPos + 1] != 13)
+                {
+                    return ReadFloatWrapperSlow(input);
+                }
+                var result = BitConverter.ToSingle(input.buffer, input.bufferPos + 2);
+                input.bufferPos += 6;
+                return result;
+            }
+            else
+            {
+                return ReadFloatWrapperSlow(input);
+            }
+        }
+
+        internal static float? ReadFloatWrapperSlow(CodedInputStream input)
+        {
+            int length = input.ReadLength();
+            if (length == 0)
+            {
+                return 0F;
+            }
+            int finalBufferPos = input.totalBytesRetired + input.bufferPos + length;
+            float result = 0F;
+            do
+            {
+                // field=1, type=32-bit = tag of 13
+                if (input.ReadTag() == 13)
+                {
+                    result = input.ReadFloat();
+                }
+                else
+                {
+                    input.SkipLastField();
+                }
+            }
+            while (input.totalBytesRetired + input.bufferPos < finalBufferPos);
+            return result;
+        }
+
+        internal static double? ReadDoubleWrapperLittleEndian(CodedInputStream input)
+        {
+            // length:1 + tag:1 + value:8 = 10 bytes
+            if (input.bufferPos + 10 <= input.bufferSize)
+            {
+                // The entire wrapper message is already contained in `buffer`.
+                int length = input.buffer[input.bufferPos];
+                if (length == 0)
+                {
+                    input.bufferPos++;
+                    return 0D;
+                }
+                // tag:1 + value:8 = length of 9 bytes
+                // field=1, type=64-bit = tag of 9
+                if (length != 9 || input.buffer[input.bufferPos + 1] != 9)
+                {
+                    return ReadDoubleWrapperSlow(input);
+                }
+                var result = BitConverter.ToDouble(input.buffer, input.bufferPos + 2);
+                input.bufferPos += 10;
+                return result;
+            }
+            else
+            {
+                return ReadDoubleWrapperSlow(input);
+            }
+        }
+
+        internal static double? ReadDoubleWrapperSlow(CodedInputStream input)
+        {
+            int length = input.ReadLength();
+            if (length == 0)
+            {
+                return 0D;
+            }
+            int finalBufferPos = input.totalBytesRetired + input.bufferPos + length;
+            double result = 0D;
+            do
+            {
+                // field=1, type=64-bit = tag of 9
+                if (input.ReadTag() == 9)
+                {
+                    result = input.ReadDouble();
+                }
+                else
+                {
+                    input.SkipLastField();
+                }
+            }
+            while (input.totalBytesRetired + input.bufferPos < finalBufferPos);
+            return result;
+        }
+
+        internal static bool? ReadBoolWrapper(CodedInputStream input)
+        {
+            return ReadUInt32Wrapper(input) != 0;
+        }
+
+        internal static uint? ReadUInt32Wrapper(CodedInputStream input)
+        {
+            // length:1 + tag:1 + value:5(varint32-max) = 7 bytes
+            if (input.bufferPos + 7 <= input.bufferSize)
+            {
+                // The entire wrapper message is already contained in `buffer`.
+                int pos0 = input.bufferPos;
+                int length = input.buffer[input.bufferPos++];
+                if (length == 0)
+                {
+                    return 0;
+                }
+                // Length will always fit in a single byte.
+                if (length >= 128)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt32WrapperSlow(input);
+                }
+                int finalBufferPos = input.bufferPos + length;
+                // field=1, type=varint = tag of 8
+                if (input.buffer[input.bufferPos++] != 8)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt32WrapperSlow(input);
+                }
+                var result = input.ReadUInt32();
+                // Verify this message only contained a single field.
+                if (input.bufferPos != finalBufferPos)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt32WrapperSlow(input);
+                }
+                return result;
+            }
+            else
+            {
+                return ReadUInt32WrapperSlow(input);
+            }
+        }
+
+        private static uint? ReadUInt32WrapperSlow(CodedInputStream input)
+        {
+            int length = input.ReadLength();
+            if (length == 0)
+            {
+                return 0;
+            }
+            int finalBufferPos = input.totalBytesRetired + input.bufferPos + length;
+            uint result = 0;
+            do
+            {
+                // field=1, type=varint = tag of 8
+                if (input.ReadTag() == 8)
+                {
+                    result = input.ReadUInt32();
+                }
+                else
+                {
+                    input.SkipLastField();
+                }
+            }
+            while (input.totalBytesRetired + input.bufferPos < finalBufferPos);
+            return result;
+        }
+
+        internal static int? ReadInt32Wrapper(CodedInputStream input)
+        {
+            return (int?)ReadUInt32Wrapper(input);
+        }
+
+        internal static ulong? ReadUInt64Wrapper(CodedInputStream input)
+        {
+            // field=1, type=varint = tag of 8
+            const int expectedTag = 8;
+            // length:1 + tag:1 + value:10(varint64-max) = 12 bytes
+            if (input.bufferPos + 12 <= input.bufferSize)
+            {
+                // The entire wrapper message is already contained in `buffer`.
+                int pos0 = input.bufferPos;
+                int length = input.buffer[input.bufferPos++];
+                if (length == 0)
+                {
+                    return 0L;
+                }
+                // Length will always fit in a single byte.
+                if (length >= 128)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt64WrapperSlow(input);
+                }
+                int finalBufferPos = input.bufferPos + length;
+                if (input.buffer[input.bufferPos++] != expectedTag)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt64WrapperSlow(input);
+                }
+                var result = input.ReadUInt64();
+                // Verify this message only contained a single field.
+                if (input.bufferPos != finalBufferPos)
+                {
+                    input.bufferPos = pos0;
+                    return ReadUInt64WrapperSlow(input);
+                }
+                return result;
+            }
+            else
+            {
+                return ReadUInt64WrapperSlow(input);
+            }
+        }
+
+        internal static ulong? ReadUInt64WrapperSlow(CodedInputStream input)
+        {
+            // field=1, type=varint = tag of 8
+            const int expectedTag = 8;
+            int length = input.ReadLength();
+            if (length == 0)
+            {
+                return 0L;
+            }
+            int finalBufferPos = input.totalBytesRetired + input.bufferPos + length;
+            ulong result = 0L;
+            do
+            {
+                if (input.ReadTag() == expectedTag)
+                {
+                    result = input.ReadUInt64();
+                }
+                else
+                {
+                    input.SkipLastField();
+                }
+            }
+            while (input.totalBytesRetired + input.bufferPos < finalBufferPos);
+            return result;
+        }
+
+        internal static long? ReadInt64Wrapper(CodedInputStream input)
+        {
+            return (long?)ReadUInt64Wrapper(input);
+        }
+
+#endregion
 
 
         #region Underlying reading primitives
         #region Underlying reading primitives
 
 
@@ -876,17 +1155,42 @@ namespace Google.Protobuf
         /// </summary>
         /// </summary>
         internal ulong ReadRawVarint64()
         internal ulong ReadRawVarint64()
         {
         {
-            int shift = 0;
-            ulong result = 0;
-            while (shift < 64)
+            if (bufferPos + 10 <= bufferSize)
             {
             {
-                byte b = ReadRawByte();
-                result |= (ulong) (b & 0x7F) << shift;
-                if ((b & 0x80) == 0)
+                ulong result = buffer[bufferPos++];
+                if (result < 128)
                 {
                 {
                     return result;
                     return result;
                 }
                 }
-                shift += 7;
+                result &= 0x7f;
+                int shift = 7;
+                do
+                {
+                    byte b = buffer[bufferPos++];
+                    result |= (ulong)(b & 0x7F) << shift;
+                    if (b < 0x80)
+                    {
+                        return result;
+                    }
+                    shift += 7;
+                }
+                while (shift < 64);
+            }
+            else
+            {
+                int shift = 0;
+                ulong result = 0;
+                do
+                {
+                    byte b = ReadRawByte();
+                    result |= (ulong)(b & 0x7F) << shift;
+                    if (b < 0x80)
+                    {
+                        return result;
+                    }
+                    shift += 7;
+                }
+                while (shift < 64);
             }
             }
             throw InvalidProtocolBufferException.MalformedVarint();
             throw InvalidProtocolBufferException.MalformedVarint();
         }
         }
@@ -896,11 +1200,32 @@ namespace Google.Protobuf
         /// </summary>
         /// </summary>
         internal uint ReadRawLittleEndian32()
         internal uint ReadRawLittleEndian32()
         {
         {
-            uint b1 = ReadRawByte();
-            uint b2 = ReadRawByte();
-            uint b3 = ReadRawByte();
-            uint b4 = ReadRawByte();
-            return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
+            if (bufferPos + 4 <= bufferSize)
+            {
+                if (BitConverter.IsLittleEndian)
+                {
+                    var result = BitConverter.ToUInt32(buffer, bufferPos);
+                    bufferPos += 4;
+                    return result;
+                }
+                else
+                {
+                    uint b1 = buffer[bufferPos];
+                    uint b2 = buffer[bufferPos + 1];
+                    uint b3 = buffer[bufferPos + 2];
+                    uint b4 = buffer[bufferPos + 3];
+                    bufferPos += 4;
+                    return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
+                }
+            }
+            else
+            {
+                uint b1 = ReadRawByte();
+                uint b2 = ReadRawByte();
+                uint b3 = ReadRawByte();
+                uint b4 = ReadRawByte();
+                return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
+            }
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -908,16 +1233,42 @@ namespace Google.Protobuf
         /// </summary>
         /// </summary>
         internal ulong ReadRawLittleEndian64()
         internal ulong ReadRawLittleEndian64()
         {
         {
-            ulong b1 = ReadRawByte();
-            ulong b2 = ReadRawByte();
-            ulong b3 = ReadRawByte();
-            ulong b4 = ReadRawByte();
-            ulong b5 = ReadRawByte();
-            ulong b6 = ReadRawByte();
-            ulong b7 = ReadRawByte();
-            ulong b8 = ReadRawByte();
-            return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
-                   | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
+            if (bufferPos + 8 <= bufferSize)
+            {
+                if (BitConverter.IsLittleEndian)
+                {
+                    var result = BitConverter.ToUInt64(buffer, bufferPos);
+                    bufferPos += 8;
+                    return result;
+                }
+                else
+                {
+                    ulong b1 = buffer[bufferPos];
+                    ulong b2 = buffer[bufferPos + 1];
+                    ulong b3 = buffer[bufferPos + 2];
+                    ulong b4 = buffer[bufferPos + 3];
+                    ulong b5 = buffer[bufferPos + 4];
+                    ulong b6 = buffer[bufferPos + 5];
+                    ulong b7 = buffer[bufferPos + 6];
+                    ulong b8 = buffer[bufferPos + 7];
+                    bufferPos += 8;
+                    return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
+                           | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
+                }
+            }
+            else
+            {
+                ulong b1 = ReadRawByte();
+                ulong b2 = ReadRawByte();
+                ulong b3 = ReadRawByte();
+                ulong b4 = ReadRawByte();
+                ulong b5 = ReadRawByte();
+                ulong b6 = ReadRawByte();
+                ulong b7 = ReadRawByte();
+                ulong b8 = ReadRawByte();
+                return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
+                       | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
+            }
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -1301,6 +1652,6 @@ namespace Google.Protobuf
                 }
                 }
             }
             }
         }
         }
-        #endregion
+#endregion
     }
     }
 }
 }

+ 37 - 1
csharp/src/Google.Protobuf/FieldCodec.cs

@@ -490,7 +490,7 @@ namespace Google.Protobuf
         {
         {
             var nestedCodec = WrapperCodecs.GetCodec<T>();
             var nestedCodec = WrapperCodecs.GetCodec<T>();
             return new FieldCodec<T?>(
             return new FieldCodec<T?>(
-                input => WrapperCodecs.Read<T>(input, nestedCodec),
+                WrapperCodecs.GetReader<T>(),
                 (output, value) => WrapperCodecs.Write<T>(output, value.Value, nestedCodec),
                 (output, value) => WrapperCodecs.Write<T>(output, value.Value, nestedCodec),
                 (CodedInputStream i, ref T? v) => v = WrapperCodecs.Read<T>(i, nestedCodec),
                 (CodedInputStream i, ref T? v) => v = WrapperCodecs.Read<T>(i, nestedCodec),
                 (ref T? v, T? v2) => { if (v2.HasValue) { v = v2; } return v.HasValue; },
                 (ref T? v, T? v2) => { if (v2.HasValue) { v = v2; } return v.HasValue; },
@@ -522,6 +522,25 @@ namespace Google.Protobuf
                 { typeof(ByteString), ForBytes(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.LengthDelimited)) }
                 { typeof(ByteString), ForBytes(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.LengthDelimited)) }
             };
             };
 
 
+            private static readonly Dictionary<System.Type, object> Readers = new Dictionary<System.Type, object>
+            {
+                // TODO: Provide more optimized readers.
+                { typeof(bool), (Func<CodedInputStream, bool?>)CodedInputStream.ReadBoolWrapper },
+                { typeof(int), (Func<CodedInputStream, int?>)CodedInputStream.ReadInt32Wrapper },
+                { typeof(long), (Func<CodedInputStream, long?>)CodedInputStream.ReadInt64Wrapper },
+                { typeof(uint), (Func<CodedInputStream, uint?>)CodedInputStream.ReadUInt32Wrapper },
+                { typeof(ulong), (Func<CodedInputStream, ulong?>)CodedInputStream.ReadUInt64Wrapper },
+                { typeof(float), BitConverter.IsLittleEndian ?
+                    (Func<CodedInputStream, float?>)CodedInputStream.ReadFloatWrapperLittleEndian :
+                    (Func<CodedInputStream, float?>)CodedInputStream.ReadFloatWrapperSlow },
+                { typeof(double), BitConverter.IsLittleEndian ?
+                    (Func<CodedInputStream, double?>)CodedInputStream.ReadDoubleWrapperLittleEndian :
+                    (Func<CodedInputStream, double?>)CodedInputStream.ReadDoubleWrapperSlow },
+                // `string` and `ByteString` less performance-sensitive. Do not implement for now.
+                { typeof(string), null },
+                { typeof(ByteString), null },
+            };
+
             /// <summary>
             /// <summary>
             /// Returns a field codec which effectively wraps a value of type T in a message.
             /// Returns a field codec which effectively wraps a value of type T in a message.
             ///
             ///
@@ -536,6 +555,23 @@ namespace Google.Protobuf
                 return (FieldCodec<T>) value;
                 return (FieldCodec<T>) value;
             }
             }
 
 
+            internal static Func<CodedInputStream, T?> GetReader<T>() where T : struct
+            {
+                object value;
+                if (!Readers.TryGetValue(typeof(T), out value))
+                {
+                    throw new InvalidOperationException("Invalid type argument requested for wrapper reader: " + typeof(T));
+                }
+                if (value == null)
+                {
+                    // Return default unoptimized reader for the wrapper type.
+                    var nestedCoded = GetCodec<T>();
+                    return input => Read<T>(input, nestedCoded);
+                }
+                // Return optimized read for the wrapper type.
+                return (Func<CodedInputStream, T?>)value;
+            }
+
             internal static T Read<T>(CodedInputStream input, FieldCodec<T> codec)
             internal static T Read<T>(CodedInputStream input, FieldCodec<T> codec)
             {
             {
                 int length = input.ReadLength();
                 int length = input.ReadLength();

Some files were not shown because too many files changed in this diff