Remove unnecessary string and string[] allocations from MS.Internal.ContentType#6268
Merged
dipeshmsft merged 3 commits intodotnet:mainfrom May 5, 2022
Merged
Conversation
The parsing logic involves a loop that repeatedly Substrings and Trims the remaining space. We can change this to use spans and only create strings when they're actually needed to be stored; the other temporary slices don't need to allocate.
danmoseley
reviewed
Mar 18, 2022
Comment on lines
+528
to
+529
| int startingLength = ++length; | ||
| length = s.Slice(startingLength).IndexOf('"'); |
Member
There was a problem hiding this comment.
Suggested change
| int startingLength = ++length; | |
| length = s.Slice(startingLength).IndexOf('"'); | |
| int startingLength = length + 1; | |
| length = s.Slice(startingLength).IndexOf('"') + 1; |
Member
Author
There was a problem hiding this comment.
That won't work correctly. We can change the ++length to be length + 1, but not the second line.
danmoseley
approved these changes
Mar 18, 2022
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
MS.Internal.ContentType is incurring a multitude of unnecessary allocations. It's parsing input by Substring'ing along it and Trim'ing along it to find the next piece, but only a subset of those strings are actually saved. It's using string.Split just to get two strings, resulting in a string[]. Etc.
Customer Impact
Apps using this indirectly incur unnecessary overhead.
Regression
No
Testing
CI
Risk
Minimal.