Skip to content

Conversation

@MihaZupan
Copy link
Member

  1. When using AutomaticDecompression, the Accept-Encoding header is practically never touched manually by the user. We can check for this very common case when the header isn't set yet and just add the one value representing all supported methods without more validation.
    • This avoids the allocation of the HttpHeaderValueCollection, the backing List<object>, HeaderStoreItemInfo and enumerators when walking through existing values.
  2. When checking if the response was compressed, we can use TryGetValues to avoid the collection allocation if there is no Content-Encoding header.
  3. If it is compressed, we can move the content header collection instead of allocating a full copy. Closes DecompressionHandler could move the headers more efficiently #63632.

Simple in-memory benchmark (no real IO)

Method Toolchain CompressedResponse Mean Error Ratio Allocated Alloc Ratio
GetAsync \main\corerun.exe False 897.3 ns 17.91 ns 1.00 1.81 KB 1.00
GetAsync \pr\corerun.exe False 674.0 ns 7.99 ns 0.75 1.37 KB 0.75
GetAsync \main\corerun.exe True 1,435.9 ns 13.05 ns 1.00 2.53 KB 1.00
GetAsync \pr\corerun.exe True 1,212.9 ns 11.70 ns 0.84 1.78 KB 0.70

@MihaZupan MihaZupan added this to the 11.0.0 milestone Dec 20, 2025
@MihaZupan MihaZupan marked this pull request as ready for review December 20, 2025 15:18
Copilot AI review requested due to automatic review settings December 20, 2025 15:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce memory allocations when using AutomaticDecompression in SocketsHttpHandler through three main optimizations:

  1. Fast-path optimization for the Accept-Encoding header when it hasn't been set yet
  2. Using TryGetValues to avoid collection allocation when checking for Content-Encoding headers
  3. Moving content headers instead of copying them when creating decompressed content wrappers

Key Changes

  • Optimized Accept-Encoding header handling with a pre-computed header value string
  • Changed Content-Encoding check to use TryGetValues and work directly with the returned string array
  • Modified decompressed content constructors to accept and use the encoding array for header manipulation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
SocketsHttpHandler.cs Added defensive check to filter unsupported decompression methods and added constant for supported methods
DecompressionHandler.cs Implemented fast-path for Accept-Encoding header, optimized Content-Encoding check, modified header handling in decompressed content classes
HttpContent.cs Added SetHeaders method to support moving headers between content objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DecompressionHandler could move the headers more efficiently

1 participant