Skip to content

Commit 97583df

Browse files
committed
Avoid double lookup in dictionary
1 parent 8688d5c commit 97583df

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/Grpc.AspNetCore.Server/Internal/GrpcProtocolHelpers.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System.Diagnostics.CodeAnalysis;
2020
using System.Globalization;
21+
using System.Runtime.InteropServices;
2122
using System.Security.Cryptography.X509Certificates;
2223
using System.Text;
2324
using Grpc.Core;
@@ -108,7 +109,7 @@ public static byte[] ParseBinaryHeader(string base64)
108109
switch (base64.Length % 4)
109110
{
110111
case 0:
111-
// base64 has the required padding
112+
// base64 has the required padding
112113
decodable = base64;
113114
break;
114115
case 2:
@@ -208,11 +209,8 @@ public static AuthContext CreateAuthContext(X509Certificate2 clientCertificate)
208209

209210
static void AddProperty(Dictionary<string, List<AuthProperty>> properties, string name, string value)
210211
{
211-
if (!properties.TryGetValue(name, out var values))
212-
{
213-
values = new List<AuthProperty>();
214-
properties[name] = values;
215-
}
212+
ref var values = ref CollectionsMarshal.GetValueRefOrAddDefault(properties, name, out _);
213+
values ??= [];
216214

217215
values.Add(AuthProperty.Create(name, Encoding.UTF8.GetBytes(value)));
218216
}

0 commit comments

Comments
 (0)