浏览代码

adjust some TODOs in ParsingPrimitives.cs

Jan Tattermusch 5 年之前
父节点
当前提交
eb38a3cdb8
共有 1 个文件被更改,包括 13 次插入14 次删除
  1. 13 14
      csharp/src/Google.Protobuf/ParsingPrimitives.cs

+ 13 - 14
csharp/src/Google.Protobuf/ParsingPrimitives.cs

@@ -558,12 +558,13 @@ namespace Google.Protobuf
             }
             }
             else
             else
             {
             {
-                // TODO: do we need to support skipping in seekable Streams?
-
                 // Skipping more bytes than are in the buffer.  First skip what we have.
                 // Skipping more bytes than are in the buffer.  First skip what we have.
                 int pos = state.bufferSize - state.bufferPos;
                 int pos = state.bufferSize - state.bufferPos;
                 state.bufferPos = state.bufferSize;
                 state.bufferPos = state.bufferSize;
 
 
+                // TODO: If our segmented buffer is backed by a Stream that is seekable, we could skip the bytes more efficiently
+                // by simply updating stream's Position property. This used to be supported in the past, but the support was dropped
+                // because it would make the segmentedBufferHelper more complex. Support can be reintroduced if needed.
                 state.segmentedBufferHelper.RefillBuffer(ref buffer, ref state, true);
                 state.segmentedBufferHelper.RefillBuffer(ref buffer, ref state, true);
 
 
                 while (size - pos > state.bufferSize)
                 while (size - pos > state.bufferSize)
@@ -637,18 +638,16 @@ namespace Google.Protobuf
             }
             }
 #endif
 #endif
 
 
-            // TODO: what if GOOGLE_PROTOBUF_SUPPORT_FAST_STRING is not supported?
-            // -> can we still try to grab an array from the span?
-            // if (length <= state.bufferSize - state.bufferPos && length > 0)
-            // {
-            //      // Fast path:  We already have the bytes in a contiguous buffer, so
-            //      //   just copy directly from it.
-            //      String result = CodedOutputStream.Utf8Encoding.GetString(buffer, state.bufferPos, length);
-            //      state.bufferPos += length;
-            //      return result;
-            // }
-
-            // TODO: creating a char[] and decoding into it and then creating a string from that array might be more efficient
+            var decoder = CodedOutputStream.Utf8Encoding.GetDecoder();
+
+            // TODO: even if GOOGLE_PROTOBUF_SUPPORT_FAST_STRING is not supported,
+            // we could still create a string efficiently by using Utf8Encoding.GetString(byte[] bytes, int index, int count)
+            // whenever the buffer is backed by a byte array (and avoid creating a new byte array), but the problem is
+            // there is no way to get the underlying byte array from a span.
+
+            // TODO: in case the string spans multiple buffer segments, creating a char[] and decoding into it and then
+            // creating a string from that array might be more efficient than creating a string from the copied bytes.
+
             // Slow path: Build a byte array first then copy it.
             // Slow path: Build a byte array first then copy it.
             return CodedOutputStream.Utf8Encoding.GetString(ReadRawBytes(ref buffer, ref state, length), 0, length);
             return CodedOutputStream.Utf8Encoding.GetString(ReadRawBytes(ref buffer, ref state, length), 0, length);
         }
         }