Răsfoiți Sursa

address a few TODOs

Jan Tattermusch 5 ani în urmă
părinte
comite
a171f6d670

+ 6 - 0
csharp/src/Google.Protobuf/CodedInputStream.cs

@@ -431,6 +431,12 @@ namespace Google.Protobuf
         /// </summary>
         public void ReadMessage(IMessage builder)
         {
+            // TODO(jtattermusch): if the message doesn't implement IBufferMessage (and thus does not provide the InternalMergeFrom method),
+            // what we're doing here works fine, but could be more efficient.
+            // What happends is that we first initialize a ParseContext from the current coded input stream only to parse the length of the message, at which point
+            // we will need to switch back again to CodedInputStream-based parsing (which involves copying and storing the state) to be able to
+            // invoke the legacy MergeFrom(CodedInputStream) method.
+            // For now, this inefficiency is fine, considering this is only a backward-compatibility scenario (and regenerating the code fixes it).
             var span = new ReadOnlySpan<byte>(buffer);
             ParseContext.Initialize(ref span, ref state, out ParseContext ctx);
             try

+ 4 - 3
csharp/src/Google.Protobuf/ParseContext.cs

@@ -74,8 +74,10 @@ namespace Google.Protobuf
         internal static void Initialize(CodedInputStream input, out ParseContext ctx)
         {
             ctx.buffer = new ReadOnlySpan<byte>(input.InternalBuffer);
-            // TODO: ideally we would use a reference to the original state, but that doesn't seem possible
-            ctx.state = input.InternalState;  // creates copy of the state
+            // ideally we would use a reference to the original state, but that doesn't seem possible
+            // so we just copy the struct that holds the state. We will need to later store the state back
+            // into CodedInputStream if we want to keep it usable.
+            ctx.state = input.InternalState;
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -228,7 +230,6 @@ namespace Google.Protobuf
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public void ReadMessage(IMessage message)
         {
-            // TODO: add a fallback if IMessage does not implement IBufferMessage 
             ParsingPrimitivesMessages.ReadMessage(ref this, message);
         }