-
Notifications
You must be signed in to change notification settings - Fork 135
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
Reduce heap allocations in AspNetCoreDiagnosticObserver #779
Conversation
{ | ||
if (_headers.TryGetValue(name, out var values)) | ||
{ | ||
return values.ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ToArray()
allocates another string array and copy the content (https://source.dot.net/#System.Linq/EnumerableHelpers.Linq.cs,105) maybe we can avoid it by using:
return values.ToArray(); | |
return values as string[] ?? values.ToArray(); |
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is StringValues.ToArray()
, not Enumerable.ToArray()
(confusing, I know)
https://source.dot.net/#Microsoft.Extensions.Primitives/StringValues.cs,280
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, yeah didn't realize that, this struct is only used by the HttpRequest.Headers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this much better, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
- Use a wrapper on top of IHeaderDictionary instead of duplicating the headers - Make all methods that expect a IHeadersCollection generic, to avoid boxing the wrapper - Cache the SamplingPriority values in a dictionary to avoid the costly enum conversion - Remove the ToList when accessing the header values
16814ae
to
bb5b68e
Compare
Benchmark: