Repository: dotnet/runtime
File: src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/DecompressionHandler.cs (C#)

Code before:
        private readonly HttpMessageHandlerStage _innerHandler;
        private readonly DecompressionMethods _decompressionMethods;

        private const string Gzip = "gzip";
        private const string Deflate = "deflate";
        private const string Brotli = "br";
        private const string Zstd = "zstd";
        private static readonly StringWithQualityHeaderValue s_gzipHeaderValue = new(Gzip);
        private static readonly StringWithQualityHeaderValue s_deflateHeaderValue = new(Deflate);
        private static readonly StringWithQualityHeaderValue s_brotliHeaderValue = new(Brotli);
        private static readonly StringWithQualityHeaderValue s_zstdHeaderValue = new(Zstd);

        /// <summary>Header value for all enabled decompression methods, e.g. "gzip, deflate".</summary>
        private readonly string _acceptEncodingHeaderValue;

        public DecompressionHandler(DecompressionMethods decompressionMethods, HttpMessageHandlerStage innerHandler)
        {
            Debug.Assert(decompressionMethods != DecompressionMethods.None);
            Debug.Assert(innerHandler != null);

            _decompressionMethods = decompressionMethods;
            _innerHandler = innerHandler;

            List<string?> methods = [GZipEnabled ? Gzip : null, DeflateEnabled ? Deflate : null, BrotliEnabled ? Brotli : null, ZstandardEnabled ? Zstd : null];
            methods.RemoveAll(item => item is null);
            _acceptEncodingHeaderValue = string.Join(", ", methods);
        }

        internal bool GZipEnabled => (_decompressionMethods & DecompressionMethods.GZip) != 0;
        internal bool DeflateEnabled => (_decompressionMethods & DecompressionMethods.Deflate) != 0;
        internal bool BrotliEnabled => (_decompressionMethods & DecompressionMethods.Brotli) != 0;
        internal bool ZstandardEnabled => (_decompressionMethods & DecompressionMethods.Zstandard) != 0;

        private static bool EncodingExists(HttpHeaderValueCollection<StringWithQualityHeaderValue> acceptEncodingHeader, string encoding)
        {
            foreach (StringWithQualityHeaderValue existingEncoding in acceptEncodingHeader)
            {
                if (string.Equals(existingEncoding.Value, encoding, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }

            return false;
        }

        internal override async ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
        {
            if (!request.Headers.Contains(KnownHeaders.AcceptEncoding.Descriptor))
            {
                // Very common case: no Accept-Encoding header yet, so just add one with all supported encodings.

Review comment (suggestion):
```csharp
Span<string?> methods = ["", "", "", ""];
int count = 0;
if (GZipEnabled) methods[count++] = Gzip;
if (DeflateEnabled) methods[count++] = Deflate;
if (BrotliEnabled) methods[count++] = Brotli;
if (ZstandardEnabled) methods[count++] = Zstandard;
_acceptEncodingHeaderValue = string.Join(", ", methods.Slice(0, count);
```

Code after:
        private readonly HttpMessageHandlerStage _innerHandler;
        private readonly DecompressionMethods _decompressionMethods;

        private const string Gzip = "gzip";
        private const string Deflate = "deflate";
        private const string Brotli = "br";
        private const string Zstd = "zstd";
        private static readonly StringWithQualityHeaderValue s_gzipHeaderValue = new(Gzip);
        private static readonly StringWithQualityHeaderValue s_deflateHeaderValue = new(Deflate);
        private static readonly StringWithQualityHeaderValue s_brotliHeaderValue = new(Brotli);
        private static readonly StringWithQualityHeaderValue s_zstdHeaderValue = new(Zstd);

        /// <summary>Header value for all enabled decompression methods, e.g. "gzip, deflate".</summary>
        private readonly string _acceptEncodingHeaderValue;

        public DecompressionHandler(DecompressionMethods decompressionMethods, HttpMessageHandlerStage innerHandler)
        {
            Debug.Assert(decompressionMethods != DecompressionMethods.None);
            Debug.Assert(innerHandler != null);

            _decompressionMethods = decompressionMethods;
            _innerHandler = innerHandler;

            Span<string?> methods = [null, null, null, null];
            int count = 0;
            if (GZipEnabled) methods[count++] = Gzip;
            if (DeflateEnabled) methods[count++] = Deflate;
            if (BrotliEnabled) methods[count++] = Brotli;
            if (ZstandardEnabled) methods[count++] = Zstd;
            _acceptEncodingHeaderValue = string.Join(", ", methods.Slice(0, count));
        }

        internal bool GZipEnabled => (_decompressionMethods & DecompressionMethods.GZip) != 0;
        internal bool DeflateEnabled => (_decompressionMethods & DecompressionMethods.Deflate) != 0;
        internal bool BrotliEnabled => (_decompressionMethods & DecompressionMethods.Brotli) != 0;
        internal bool ZstandardEnabled => (_decompressionMethods & DecompressionMethods.Zstandard) != 0;

        private static bool EncodingExists(HttpHeaderValueCollection<StringWithQualityHeaderValue> acceptEncodingHeader, string encoding)
        {
            foreach (StringWithQualityHeaderValue existingEncoding in acceptEncodingHeader)
            {
                if (string.Equals(existingEncoding.Value, encoding, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }

            return false;
        }

        internal override async ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)