From 4f1823da12516f008c5a2206412a2391fc64d1d1 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Sun, 23 Oct 2016 12:42:42 +0200 Subject: [PATCH] string.EndsWith is really slow on CoreCLR --- src/csharp/Grpc.Core/Metadata.cs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs index e686f8e3980..6fc715d6ee1 100644 --- a/src/csharp/Grpc.Core/Metadata.cs +++ b/src/csharp/Grpc.Core/Metadata.cs @@ -264,7 +264,7 @@ namespace Grpc.Core public Entry(string key, byte[] valueBytes) { this.key = NormalizeKey(key); - GrpcPreconditions.CheckArgument(this.key.EndsWith(BinaryHeaderSuffix), + GrpcPreconditions.CheckArgument(HasBinaryHeaderSuffix(this.key), "Key for binary valued metadata entry needs to have suffix indicating binary value."); this.value = null; GrpcPreconditions.CheckNotNull(valueBytes, "valueBytes"); @@ -280,7 +280,7 @@ namespace Grpc.Core public Entry(string key, string value) { this.key = NormalizeKey(key); - GrpcPreconditions.CheckArgument(!this.key.EndsWith(BinaryHeaderSuffix), + GrpcPreconditions.CheckArgument(!HasBinaryHeaderSuffix(this.key), "Key for ASCII valued metadata entry cannot have suffix indicating binary value."); this.value = GrpcPreconditions.CheckNotNull(value, "value"); this.valueBytes = null; @@ -367,7 +367,7 @@ namespace Grpc.Core /// internal static Entry CreateUnsafe(string key, byte[] valueBytes) { - if (key.EndsWith(BinaryHeaderSuffix)) + if (HasBinaryHeaderSuffix(key)) { return new Entry(key, null, valueBytes); } @@ -381,6 +381,27 @@ namespace Grpc.Core "Metadata entry key not valid. Keys can only contain lowercase alphanumeric characters, underscores and hyphens."); return normalized; } + + /// + /// Returns true if the key has "-bin" binary header suffix. + /// + private static bool HasBinaryHeaderSuffix(string key) + { + // We don't use just string.EndsWith because its implementation is extremely slow + // on CoreCLR and we've seen significant differences in gRPC benchmarks caused by it. + // See https://github.com/dotnet/coreclr/issues/5612 + + int len = key.Length; + if (len >= 4 && + key[len - 4] == '-' && + key[len - 3] == 'b' && + key[len - 2] == 'i' && + key[len - 1] == 'n') + { + return true; + } + return false; + } } } }