From 97583dfbacd2d07451b2b74d45c6c363dd2914a0 Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 21 Oct 2024 16:12:49 +0200 Subject: [PATCH] Avoid double lookup in dictionary --- .../Internal/GrpcProtocolHelpers.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Grpc.AspNetCore.Server/Internal/GrpcProtocolHelpers.cs b/src/Grpc.AspNetCore.Server/Internal/GrpcProtocolHelpers.cs index ef5a7b191..9e4e1f7a0 100644 --- a/src/Grpc.AspNetCore.Server/Internal/GrpcProtocolHelpers.cs +++ b/src/Grpc.AspNetCore.Server/Internal/GrpcProtocolHelpers.cs @@ -18,6 +18,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using System.Text; using Grpc.Core; @@ -108,7 +109,7 @@ public static byte[] ParseBinaryHeader(string base64) switch (base64.Length % 4) { case 0: - // base64 has the required padding + // base64 has the required padding decodable = base64; break; case 2: @@ -208,11 +209,8 @@ public static AuthContext CreateAuthContext(X509Certificate2 clientCertificate) static void AddProperty(Dictionary> properties, string name, string value) { - if (!properties.TryGetValue(name, out var values)) - { - values = new List(); - properties[name] = values; - } + ref var values = ref CollectionsMarshal.GetValueRefOrAddDefault(properties, name, out _); + values ??= []; values.Add(AuthProperty.Create(name, Encoding.UTF8.GetBytes(value))); }