-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Consider using SortedList instead of Dictionary in HttpHeaders #62846
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsThe Dictionary we allocate in HttpHeaders for the headers table seems to use a significant amount of memory. Since the headers table itself is typically not that large, it seems reasonable to use SortedList instead and trade off reduced memory usage for a small amount of added CPU cost on update and retrieval -- I doubt this added CPU cost is significant, whereas the memory savings may be significant.
|
This may require a custom |
Well that's unfortunate. Aside from boxing the enumerator, are there other known issues? |
An insertion has |
I'm not sure it matters. We limit the size of the response headers that we will accept, so there's a bound on the total number of headers we will add. While O(n^2) isn't great, it's not necessarily a DOS as long as n is smallish. |
Another alternative here might be to simply have an unsorted list of key value pairs. This has cheap insertion but makes both lookup and removal expensive. That might not be a bad tradeoff though. And if we did it this way, we could actually preserve the original header ordering, which seems like a nice benefit. For stuff like YARP we would want to just enumerate the raw headers and not worry about grouping by header name, which means that "raw enumeration" for these sorts of scenarios would be cheap as well. Unfortunately we defined the NonValidated collection to group by header name as well... |
Right, but as of right now |
.... do we insert headers singly, or as a range? |
Are you referring to the non generic |
The generic as well. The enumerator is a struct, but the type is not publicly exposed so the runtime/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs Line 555 in af726fc
|
Triage:
Further design discussion:
|
Similarly, I noticed this in the same trace. It's a small impact in this case because we only enumerate the concurrent dictionary when scavenging connections. I'm not sure why these collections were implemented so that their enumerators must be boxed. Is this simply an oversight? Can we fix it? It's a breaking change, but a very small one... |
For a dictionary approach maybe dotnet/aspnetcore#31360 is also of interest (the |
It's a binary breaking change. I don't think it's that small. Anyone enumerating one of these today would fail at run time.
|
Makes sense, thanks for clarifying. |
The Dictionary we allocate in HttpHeaders for the headers table seems to use a significant amount of memory. Since the headers table itself is typically not that large, it seems reasonable to use SortedList instead and trade off reduced memory usage for a small amount of added CPU cost on update and retrieval -- I doubt this added CPU cost is significant, whereas the memory savings may be significant.
The text was updated successfully, but these errors were encountered: