Reduce allocations in ChecksumUtilities.BytesToString#12359
Merged
ToddGrun merged 3 commits intodotnet:mainfrom Oct 17, 2025
Merged
Conversation
This method was previously allocating a string for each byte being converted and then allocating another string for the stringbuilder.ToString call. Instead: if >NET9 use Convert.ToHexStringLower else create stack alloc'ed char[] and put converted chars in there and allocate a single string from that
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/ChecksumUtilities.cs
Outdated
Show resolved
Hide resolved
| return buffer.ToString(); | ||
|
|
||
| static char GetHexChar(int value) | ||
| => (char)(value < 10 ? '0' + value : 'a' + (value - 10)); |
Member
There was a problem hiding this comment.
Is this faster than indexing into a pre-allocated array of chars?
Contributor
Author
There was a problem hiding this comment.
Probably slightly slower, but I didn't want to duplicate HexConverter.CharToHexLookup, especially as this won't be used OOP once we move to net9
Member
There was a problem hiding this comment.
I wouldn't duplicate all of that either! I was just thinking of something like this:
private static char GetHexChar(int value) => s_hexChars[value];
private static readonly char[] s_hexChars = new[] { '0', '1', '2', '3', '4', '5', '6', '7, '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
Member
There was a problem hiding this comment.
FWIW, that's definitely into the micro-optimization level. I agree that it's not needed for just netstandard2.0. 👍
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/ChecksumUtilities.cs
Outdated
Show resolved
Hide resolved
2) Avoid ImmutableCollectionsMarshal, and just pass the span to Convert.ToHexString
davidwengier
approved these changes
Oct 17, 2025
This was referenced Oct 21, 2025
This was referenced Oct 22, 2025
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.
This method was previously allocating a string for each byte being converted and then allocating another string for the stringbuilder.ToString call. Instead:
This method was accounting for 1.4% of allocations during the typing scenario in the razoreditingtests.ScrollingAndTypingInCohosting speedometer test. The final string allocation was only 0.3%, which is equivalent to what the new implementation should allocate.