diff --git a/csharp/src/Google.Protobuf/CodedInputStream.cs b/csharp/src/Google.Protobuf/CodedInputStream.cs index 46dd6ec9c2..2fda24d91d 100644 --- a/csharp/src/Google.Protobuf/CodedInputStream.cs +++ b/csharp/src/Google.Protobuf/CodedInputStream.cs @@ -421,9 +421,8 @@ namespace Google.Protobuf /// public string ReadString() { - int length = ReadLength(); var span = new ReadOnlySpan(buffer); - return ParsingPrimitives.ReadRawString(ref span, ref state, length); + return ParsingPrimitives.ReadString(ref span, ref state); } /// @@ -470,20 +469,8 @@ namespace Google.Protobuf /// public ByteString ReadBytes() { - int length = ReadLength(); - 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. - ByteString result = ByteString.CopyFrom(buffer, state.bufferPos, length); - state.bufferPos += length; - return result; - } - else - { - // Slow path: Build a byte array and attach it to a new ByteString. - return ByteString.AttachBytes(ReadRawBytes(length)); - } + var span = new ReadOnlySpan(buffer); + return ParsingPrimitives.ReadBytes(ref span, ref state); } /// diff --git a/csharp/src/Google.Protobuf/ParseContext.cs b/csharp/src/Google.Protobuf/ParseContext.cs index f3dcc9bbcf..d1fd38cfd0 100644 --- a/csharp/src/Google.Protobuf/ParseContext.cs +++ b/csharp/src/Google.Protobuf/ParseContext.cs @@ -220,8 +220,7 @@ namespace Google.Protobuf [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString() { - int length = ParsingPrimitives.ParseLength(ref buffer, ref state); - return ParsingPrimitives.ReadRawString(ref buffer, ref state, length); + return ParsingPrimitives.ReadString(ref buffer, ref state); } /// @@ -248,8 +247,7 @@ namespace Google.Protobuf [MethodImpl(MethodImplOptions.AggressiveInlining)] public ByteString ReadBytes() { - int length = ParsingPrimitives.ParseLength(ref buffer, ref state); - return ByteString.AttachBytes(ParsingPrimitives.ReadRawBytes(ref buffer, ref state, length)); + return ParsingPrimitives.ReadBytes(ref buffer, ref state); } /// /// Reads a uint32 field value from the input. diff --git a/csharp/src/Google.Protobuf/ParsingPrimitives.cs b/csharp/src/Google.Protobuf/ParsingPrimitives.cs index ee9ee21b0a..e83b2cfc91 100644 --- a/csharp/src/Google.Protobuf/ParsingPrimitives.cs +++ b/csharp/src/Google.Protobuf/ParsingPrimitives.cs @@ -577,6 +577,10 @@ namespace Google.Protobuf } } + /// + /// Reads a string field value from the input. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string ReadString(ref ReadOnlySpan buffer, ref ParserInternalState state) { int length = ParsingPrimitives.ParseLength(ref buffer, ref state);