Avoid allocations in 'Transfer-Encoding: Chunked' check (#81253)
authorMiha Zupan <mihazupan.zupan1@gmail.com>
Fri, 27 Jan 2023 18:43:06 +0000 (10:43 -0800)
committerGitHub <noreply@github.com>
Fri, 27 Jan 2023 18:43:06 +0000 (10:43 -0800)
src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs

index 0368349..b1b7176 100644 (file)
@@ -81,14 +81,25 @@ namespace System.Net.Http.Headers
 
         internal static bool? GetTransferEncodingChunked(HttpHeaders parent, HttpGeneralHeaders? headers)
         {
-            if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked))
+            if (parent.TryGetHeaderValue(KnownHeaders.TransferEncoding.Descriptor, out object? value))
             {
-                return true;
+                // Fast-path for the very common case where "chunked" is the only value.
+                if (value is string stringValue && stringValue.Equals("chunked", StringComparison.OrdinalIgnoreCase))
+                {
+                    return true;
+                }
+
+                if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked))
+                {
+                    return true;
+                }
             }
+
             if (headers != null && headers._transferEncodingChunkedSet)
             {
                 return false;
             }
+
             return null;
         }