浏览代码

Optimize WriteRawInt32 for the common case of a value < 128, which is a single byte.
Aside from anything else, this will be used for all tags for fields 1-15.

Jon Skeet 10 年之前
父节点
当前提交
ce0e348ded
共有 1 个文件被更改,包括 7 次插入0 次删除
  1. 7 0
      csharp/src/ProtocolBuffers/CodedOutputStream.cs

+ 7 - 0
csharp/src/ProtocolBuffers/CodedOutputStream.cs

@@ -1033,6 +1033,13 @@ namespace Google.Protobuf
         /// </summary>
         public void WriteRawVarint32(uint value)
         {
+            // Optimize for the common case of a single byte value
+            if (value < 128 && position < limit)
+            {
+                buffer[position++] = (byte)value;
+                return;
+            }
+
             while (value > 127 && position < limit)
             {
                 buffer[position++] = (byte) ((value & 0x7F) | 0x80);