diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2016-10-23 12:42:42 +0200 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@google.com> | 2016-10-23 13:00:54 +0200 |
commit | 4f1823da12516f008c5a2206412a2391fc64d1d1 (patch) | |
tree | 3bc02ab963eca72c4f97989cf7a8d09a8de770b4 /src | |
parent | f89a4a189f72ebdeca3e73cf377dc29f3757224a (diff) |
string.EndsWith is really slow on CoreCLR
Diffstat (limited to 'src')
-rw-r--r-- | src/csharp/Grpc.Core/Metadata.cs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs index e686f8e398..6fc715d6ee 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 /// </summary> 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; } + + /// <summary> + /// Returns <c>true</c> if the key has "-bin" binary header suffix. + /// </summary> + 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; + } } } } |