From d2eed6c852af334b20c451abf7fc68ec13fbb112 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 27 Jan 2023 10:43:06 -0800 Subject: [PATCH] Avoid allocations in 'Transfer-Encoding: Chunked' check (#81253) --- .../src/System/Net/Http/Headers/HttpGeneralHeaders.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs index 0368349..b1b7176 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs @@ -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; } -- 2.7.4