[OTLP] Refactor ProtobufSerializer.WriteReservedLength() to improve performance (#6367)
This commit is contained in:
parent
361a1655b9
commit
d8dfaa84a3
|
|
@ -83,7 +83,7 @@
|
||||||
These packages are referenced as "PrivateAssets" or used in tests/examples.
|
These packages are referenced as "PrivateAssets" or used in tests/examples.
|
||||||
-->
|
-->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageVersion Include="BenchmarkDotNet" Version="[0.13.12,0.14)" />
|
<PackageVersion Include="BenchmarkDotNet" Version="0.15.2" />
|
||||||
<PackageVersion Include="CommandLineParser" Version="[2.9.1,3.0)" />
|
<PackageVersion Include="CommandLineParser" Version="[2.9.1,3.0)" />
|
||||||
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
|
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
|
||||||
<PackageVersion Include="Grpc.AspNetCore" Version="[2.59.0,3.0)" />
|
<PackageVersion Include="Grpc.AspNetCore" Version="[2.59.0,3.0)" />
|
||||||
|
|
|
||||||
|
|
@ -201,8 +201,8 @@ internal static class GrpcStatusDeserializer
|
||||||
throw new EndOfStreamException();
|
throw new EndOfStreamException();
|
||||||
}
|
}
|
||||||
|
|
||||||
result |= (long)(b & 0x7F) << shift;
|
result |= (long)(b & 0b_0111_1111) << shift;
|
||||||
if ((b & 0x80) == 0)
|
if ((b & 0b_1000_0000) == 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ internal static class ProtobufSerializer
|
||||||
private const ulong ULong128 = 0x80;
|
private const ulong ULong128 = 0x80;
|
||||||
private const int Fixed32Size = 4;
|
private const int Fixed32Size = 4;
|
||||||
private const int Fixed64Size = 8;
|
private const int Fixed64Size = 8;
|
||||||
|
private const int MaskBitsLow = 0b_0111_1111;
|
||||||
|
private const int MaskBitHigh = 0b_1000_0000;
|
||||||
|
|
||||||
private static readonly Encoding Utf8Encoding = Encoding.UTF8;
|
private static readonly Encoding Utf8Encoding = Encoding.UTF8;
|
||||||
|
|
||||||
|
|
@ -42,63 +44,11 @@ internal static class ProtobufSerializer
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
internal static void WriteReservedLength(byte[] buffer, int writePosition, int length)
|
internal static void WriteReservedLength(byte[] buffer, int writePosition, int length)
|
||||||
{
|
{
|
||||||
int byteLength = 0;
|
var slice = buffer.AsSpan(writePosition, 4);
|
||||||
int? firstByte = null;
|
slice[0] = (byte)((length & MaskBitsLow) | MaskBitHigh);
|
||||||
int? secondByte = null;
|
slice[1] = (byte)(((length >> 7) & MaskBitsLow) | MaskBitHigh);
|
||||||
int? thirdByte = null;
|
slice[2] = (byte)(((length >> 14) & MaskBitsLow) | MaskBitHigh);
|
||||||
int? fourthByte = null;
|
slice[3] = (byte)((length >> 21) & MaskBitsLow);
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
switch (byteLength)
|
|
||||||
{
|
|
||||||
case 0:
|
|
||||||
firstByte = length & 0x7F;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
secondByte = length & 0x7F;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
thirdByte = length & 0x7F;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
fourthByte = length & 0x7F;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
length >>= 7;
|
|
||||||
byteLength++;
|
|
||||||
}
|
|
||||||
while (length > 0);
|
|
||||||
|
|
||||||
if (fourthByte.HasValue)
|
|
||||||
{
|
|
||||||
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)(thirdByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)fourthByte!.Value;
|
|
||||||
}
|
|
||||||
else if (thirdByte.HasValue)
|
|
||||||
{
|
|
||||||
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)(thirdByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = 0;
|
|
||||||
}
|
|
||||||
else if (secondByte.HasValue)
|
|
||||||
{
|
|
||||||
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = 0x80;
|
|
||||||
buffer[writePosition++] = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
|
|
||||||
buffer[writePosition++] = 0x80;
|
|
||||||
buffer[writePosition++] = 0x80;
|
|
||||||
buffer[writePosition++] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|
@ -171,7 +121,7 @@ internal static class ProtobufSerializer
|
||||||
{
|
{
|
||||||
while (value >= UInt128)
|
while (value >= UInt128)
|
||||||
{
|
{
|
||||||
buffer[writePosition++] = (byte)(0x80 | (value & 0x7F));
|
buffer[writePosition++] = (byte)(MaskBitHigh | (value & MaskBitsLow));
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -184,7 +134,7 @@ internal static class ProtobufSerializer
|
||||||
{
|
{
|
||||||
while (value >= ULong128)
|
while (value >= ULong128)
|
||||||
{
|
{
|
||||||
buffer[writePosition++] = (byte)(0x80 | (value & 0x7F));
|
buffer[writePosition++] = (byte)(MaskBitHigh | (value & MaskBitsLow));
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue