From 40ff62b6dd0467ee68f1bf79b078358fd90d7029 Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 21 Oct 2024 13:23:20 +0200 Subject: [PATCH 1/3] Reduce the size of the internal struct ServerGrpcWebContext that is copied around --- src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs b/src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs index 2d7a74938..ce980264d 100644 --- a/src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs +++ b/src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs @@ -20,7 +20,7 @@ namespace Grpc.AspNetCore.Web.Internal; internal readonly record struct ServerGrpcWebContext(ServerGrpcWebMode Request, ServerGrpcWebMode Response); -internal enum ServerGrpcWebMode +internal enum ServerGrpcWebMode : byte { None, GrpcWeb, From 8688d5c1796c397650c6e43442e69aec37983964 Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 21 Oct 2024 13:23:46 +0200 Subject: [PATCH 2/3] Avoid double lookup in dictionary --- src/Shared/Server/MethodOptions.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Shared/Server/MethodOptions.cs b/src/Shared/Server/MethodOptions.cs index 1f0e29c24..f50fc4cd3 100644 --- a/src/Shared/Server/MethodOptions.cs +++ b/src/Shared/Server/MethodOptions.cs @@ -158,10 +158,7 @@ private static void AddCompressionProviders(Dictionary Date: Mon, 21 Oct 2024 16:12:49 +0200 Subject: [PATCH 3/3] 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))); }