Skip to content

Avoid O(N) string[] allocations for N header values in GetCommaSeparatedValues #54808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2024

Conversation

stephentoub
Copy link
Member

Reduce allocation in HeaderDictionaryExtensions.GetCommaSeparatedValues

Description

The method parses the header value, adding each segment as a string into a StringValues. It does so one at a time, such that each addition requires allocating a new array one element larger than the previous, for O(N) allocations and O(N^2) work. The only consumer of that also doesn't actually use StringValues: it immediately ToArray's it. So this just builds up the array directly, starting with stack memory for the first four elements and growing into array pool buffers if more is needed, then producing a single array of the right size at the end.

@ghost ghost added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Mar 27, 2024
@amcasey
Copy link
Member

amcasey commented Apr 1, 2024

Are you interested in having a look before I merge, @BrennanConroy?

@BrennanConroy
Copy link
Member

I can look after you merge. As you can imagine, I have a lot to catch up on.

@amcasey amcasey merged commit effb34d into dotnet:main Apr 1, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview4 milestone Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Perf
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants