Skip to content

Conversation

@ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Oct 17, 2025

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 string using string.Create and place converted chars directly into the span used to crate it

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.

image

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
@ToddGrun ToddGrun requested a review from a team as a code owner October 17, 2025 18:20
Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice find!

return buffer.ToString();

static char GetHexChar(int value)
=> (char)(value < 10 ? '0' + value : 'a' + (value - 10));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this faster than indexing into a pre-allocated array of chars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, that's definitely into the micro-optimization level. I agree that it's not needed for just netstandard2.0. 👍

2) Avoid ImmutableCollectionsMarshal, and just pass the span to Convert.ToHexString
Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀🚀🚀🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants