Reduce heap allocations in AspNetCoreDiagnosticObserver#779
Merged
Conversation
tonyredondo
reviewed
Jul 3, 2020
| { | ||
| if (_headers.TryGetValue(name, out var values)) | ||
| { | ||
| return values.ToArray(); |
Member
There was a problem hiding this comment.
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:
Suggested change
| return values.ToArray(); | |
| return values as string[] ?? values.ToArray(); |
what do you think?
Contributor
Author
There was a problem hiding this comment.
This is StringValues.ToArray(), not Enumerable.ToArray() (confusing, I know)
https://source.dot.net/#Microsoft.Extensions.Primitives/StringValues.cs,280
Member
There was a problem hiding this comment.
ok, yeah didn't realize that, this struct is only used by the HttpRequest.Headers
lucaspimentel
approved these changes
Jul 7, 2020
lucaspimentel
left a comment
Member
There was a problem hiding this comment.
I like this much better, thanks!
- 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
kevingosse
force-pushed
the
kevin/aspnetcorediagnosticobserver
branch
from
July 8, 2020 11:20
16814ae to
bb5b68e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmark: