Skip to content

Commit d8dfaa8

Browse files
[OTLP] Refactor ProtobufSerializer.WriteReservedLength() to improve performance (#6367)
1 parent 361a165 commit d8dfaa8

File tree

3 files changed

+12
-62
lines changed

3 files changed

+12
-62
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
These packages are referenced as "PrivateAssets" or used in tests/examples.
8484
-->
8585
<ItemGroup>
86-
<PackageVersion Include="BenchmarkDotNet" Version="[0.13.12,0.14)" />
86+
<PackageVersion Include="BenchmarkDotNet" Version="0.15.2" />
8787
<PackageVersion Include="CommandLineParser" Version="[2.9.1,3.0)" />
8888
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
8989
<PackageVersion Include="Grpc.AspNetCore" Version="[2.59.0,3.0)" />

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/Grpc/GrpcStatusDeserializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ private static long DecodeVarint(Stream stream)
201201
throw new EndOfStreamException();
202202
}
203203

204-
result |= (long)(b & 0x7F) << shift;
205-
if ((b & 0x80) == 0)
204+
result |= (long)(b & 0b_0111_1111) << shift;
205+
if ((b & 0b_1000_0000) == 0)
206206
{
207207
return result;
208208
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufSerializer.cs

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal static class ProtobufSerializer
1818
private const ulong ULong128 = 0x80;
1919
private const int Fixed32Size = 4;
2020
private const int Fixed64Size = 8;
21+
private const int MaskBitsLow = 0b_0111_1111;
22+
private const int MaskBitHigh = 0b_1000_0000;
2123

2224
private static readonly Encoding Utf8Encoding = Encoding.UTF8;
2325

@@ -42,63 +44,11 @@ internal static int WriteTagAndLength(byte[] buffer, int writePosition, int cont
4244
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4345
internal static void WriteReservedLength(byte[] buffer, int writePosition, int length)
4446
{
45-
int byteLength = 0;
46-
int? firstByte = null;
47-
int? secondByte = null;
48-
int? thirdByte = null;
49-
int? fourthByte = null;
50-
51-
do
52-
{
53-
switch (byteLength)
54-
{
55-
case 0:
56-
firstByte = length & 0x7F;
57-
break;
58-
case 1:
59-
secondByte = length & 0x7F;
60-
break;
61-
case 2:
62-
thirdByte = length & 0x7F;
63-
break;
64-
case 3:
65-
fourthByte = length & 0x7F;
66-
break;
67-
}
68-
69-
length >>= 7;
70-
byteLength++;
71-
}
72-
while (length > 0);
73-
74-
if (fourthByte.HasValue)
75-
{
76-
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
77-
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
78-
buffer[writePosition++] = (byte)(thirdByte!.Value | 0x80);
79-
buffer[writePosition++] = (byte)fourthByte!.Value;
80-
}
81-
else if (thirdByte.HasValue)
82-
{
83-
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
84-
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
85-
buffer[writePosition++] = (byte)(thirdByte!.Value | 0x80);
86-
buffer[writePosition++] = 0;
87-
}
88-
else if (secondByte.HasValue)
89-
{
90-
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
91-
buffer[writePosition++] = (byte)(secondByte!.Value | 0x80);
92-
buffer[writePosition++] = 0x80;
93-
buffer[writePosition++] = 0;
94-
}
95-
else
96-
{
97-
buffer[writePosition++] = (byte)(firstByte!.Value | 0x80);
98-
buffer[writePosition++] = 0x80;
99-
buffer[writePosition++] = 0x80;
100-
buffer[writePosition++] = 0;
101-
}
47+
var slice = buffer.AsSpan(writePosition, 4);
48+
slice[0] = (byte)((length & MaskBitsLow) | MaskBitHigh);
49+
slice[1] = (byte)(((length >> 7) & MaskBitsLow) | MaskBitHigh);
50+
slice[2] = (byte)(((length >> 14) & MaskBitsLow) | MaskBitHigh);
51+
slice[3] = (byte)((length >> 21) & MaskBitsLow);
10252
}
10353

10454
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -171,7 +121,7 @@ internal static int WriteVarInt32(byte[] buffer, int writePosition, uint value)
171121
{
172122
while (value >= UInt128)
173123
{
174-
buffer[writePosition++] = (byte)(0x80 | (value & 0x7F));
124+
buffer[writePosition++] = (byte)(MaskBitHigh | (value & MaskBitsLow));
175125
value >>= 7;
176126
}
177127

@@ -184,7 +134,7 @@ internal static int WriteVarInt64(byte[] buffer, int writePosition, ulong value)
184134
{
185135
while (value >= ULong128)
186136
{
187-
buffer[writePosition++] = (byte)(0x80 | (value & 0x7F));
137+
buffer[writePosition++] = (byte)(MaskBitHigh | (value & MaskBitsLow));
188138
value >>= 7;
189139
}
190140

0 commit comments

Comments
 (0)