Просмотр исходного кода

修改了Google.Protobuf代码,让CodedInputStream可以重用,这样反序列化不再需要new CodedInputStream,减少GC

tanghai 7 лет назад
Родитель
Сommit
b39d1c6899

+ 23 - 4
Unity/Assets/ThirdParty/Google.Protobuf/CodedInputStream.cs

@@ -57,12 +57,12 @@ namespace Google.Protobuf
         /// Whether to leave the underlying stream open when disposing of this stream.
         /// This is always true when there's no stream.
         /// </summary>
-        private readonly bool leaveOpen;
+        private bool leaveOpen;
 
         /// <summary>
         /// Buffer of data read from the stream or provided at construction time.
         /// </summary>
-        private readonly byte[] buffer;
+        private byte[] buffer;
 
         /// <summary>
         /// The index of the buffer at which we need to refill from the stream (if there is one).
@@ -111,8 +111,8 @@ namespace Google.Protobuf
 
         private int recursionDepth = 0;
 
-        private readonly int recursionLimit;
-        private readonly int sizeLimit;
+        private int recursionLimit;
+        private int sizeLimit;
 
         #region Construction
         // Note that the checks are performed such that we don't end up checking obviously-valid things
@@ -176,6 +176,25 @@ namespace Google.Protobuf
             this.recursionLimit = DefaultRecursionLimit;
             this.leaveOpen = leaveOpen;
         }
+        
+        public void Reset(byte[] buf, int offset, int length)
+        {
+            this.buffer = buf;
+            this.bufferPos = offset;
+            this.bufferSize = length;
+            this.sizeLimit = DefaultSizeLimit;
+            this.recursionLimit = DefaultRecursionLimit;
+            this.leaveOpen = true;
+
+            bufferSizeAfterLimit = 0;
+            lastTag = 0;
+            nextTag = 0;
+            hasNextTag = false;
+            totalBytesRetired = 0;
+            currentLimit = int.MaxValue;
+            sizeLimit = 0;
+            recursionDepth = 0;
+        }
 
         /// <summary>
         /// Creates a new CodedInputStream reading data from the given

+ 7 - 2
Unity/Assets/ThirdParty/Google.Protobuf/MessageExtensions.cs

@@ -39,6 +39,8 @@ namespace Google.Protobuf
     /// </summary>
     public static class MessageExtensions
     {
+        public static CodedInputStream inputStream = new CodedInputStream(new byte[0]);
+        
         /// <summary>
         /// Merges data from the given byte array into an existing message.
         /// </summary>
@@ -48,7 +50,8 @@ namespace Google.Protobuf
         {
             ProtoPreconditions.CheckNotNull(message, "message");
             ProtoPreconditions.CheckNotNull(data, "data");
-            CodedInputStream input = new CodedInputStream(data);
+            inputStream.Reset(data, 0, data.Length);
+            CodedInputStream input = inputStream;
             message.MergeFrom(input);
             input.CheckReadEndOfStreamTag();
         }
@@ -62,7 +65,9 @@ namespace Google.Protobuf
         {
             ProtoPreconditions.CheckNotNull(message, "message");
             ProtoPreconditions.CheckNotNull(data, "data");
-            CodedInputStream input = new CodedInputStream(data, offset, length);
+            inputStream.Reset(data, offset, length);
+            CodedInputStream input = inputStream;
+            //CodedInputStream input = new CodedInputStream(data, offset, length);
             message.MergeFrom(input);
             input.CheckReadEndOfStreamTag();
         }