Skip to content

Commit 6bf2a73

Browse files
authored
GetBytes(string).Length -> GetByteCount(string) + stackalloc in ReadString (#181)
1 parent 6911edd commit 6bf2a73

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingRead.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,16 @@ internal static int ReadString(ref SequenceReader<byte> reader, out string value
177177
reader.TryCopyTo(tempSpan);
178178
reader.Advance(lenC);
179179
value = Encoding.UTF8.GetString(tempSpan);
180-
return offset + s_encoding.GetBytes(value).Length;
180+
return offset + s_encoding.GetByteCount(value);
181181

182182
case FormatCode.Sym32:
183183
case FormatCode.Str32:
184184
offset += WireFormatting.ReadInt32(ref reader, out var len);
185-
Span<byte> tempSpan32 = new byte[len];
185+
Span<byte> tempSpan32 = len <= 64 ? stackalloc byte[len] : new byte[len];
186186
reader.TryCopyTo(tempSpan32);
187187
reader.Advance(len);
188188
value = Encoding.UTF8.GetString(tempSpan32);
189-
return offset + s_encoding.GetBytes(value).Length;
189+
return offset + s_encoding.GetByteCount(value);
190190
}
191191

192192
throw new AMQP.AmqpParseException($"ReadString invalid type {type}");

RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingWrite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static int WriteAny(Span<byte> seq, object value)
3636

3737
private static int WriteString(Span<byte> seq, string value)
3838
{
39-
var len = s_encoding.GetBytes(value).Length;
39+
var len = s_encoding.GetByteCount(value);
4040
var offset = 0;
4141
// Str8
4242
if (len <= byte.MaxValue)

0 commit comments

Comments
 (0)