diff --git a/csharp/src/Google.Protobuf/WritingPrimitives.cs b/csharp/src/Google.Protobuf/WritingPrimitives.cs index c317ee2bee..f85e3d8ea0 100644 --- a/csharp/src/Google.Protobuf/WritingPrimitives.cs +++ b/csharp/src/Google.Protobuf/WritingPrimitives.cs @@ -179,12 +179,23 @@ namespace Google.Protobuf } else { - // TODO: optimize this part!!!! +#if NETSTANDARD1_1 + // slowpath when Encoding.GetBytes(Char*, Int32, Byte*, Int32) is not available byte[] bytes = Utf8Encoding.GetBytes(value); WriteRawBytes(ref buffer, ref state, bytes); - // TODO: we need to write to a span... - //Utf8Encoding.GetBytes(value, 0, value.Length, buffer, state.position); - //state.position += length; +#else + ReadOnlySpan source = value.AsSpan(); + int bytesUsed; + unsafe + { + fixed (char* sourceChars = &MemoryMarshal.GetReference(source)) + fixed (byte* destinationBytes = &MemoryMarshal.GetReference(buffer.Slice(state.position))) + { + bytesUsed = Utf8Encoding.GetBytes(sourceChars, source.Length, destinationBytes, buffer.Length); + } + } + state.position += bytesUsed; +#endif } } else