Skip to content

Conversation

@ToddGrun
Copy link
Contributor

  1. The ArrayBuilder that was being used commonly ends up exceeding the re-use size threshold, thus negating it's pooling benefit
  2. The ArrayBuilder.ToArray call can be replaced by an upfront array allocation as the size of the array is easy to determine
  3. The Encoding.GetBytes calls can be changed to a non-allocating version
  4. In NET, can avoid creating the SHA256 object
  5. In NET, can use the HashData call to avoid allocating a return array
  6. In NET, can create a guid without needing to create a new array

Found this while looking at a profile for the razor speedometer test. This isn't a huge allocator, but it's a couple MB of allocations that should be mostly removed.

1) The ArrayBuilder that was being used commonly ends up exceeding the re-use size threshold, thus negating it's pooling benefit
2) The ArrayBuilder.ToArray call can be replaced by an upfront array allocation as the size of the array is easy to determine
3) The Encoding.GetBytes calls can be changed to a non-allocating version
4) In NET, can avoid creating the SHA256 object
5) In NET, can use the HashData call to avoid allocating a return array
6) In NET, can create a guid without needing to create a new array
@ToddGrun ToddGrun requested a review from a team as a code owner September 15, 2025 23:42
+ 2
+ Encoding.Unicode.GetByteCount(hintName);

var hashInput = new byte[hashInputLength];
Copy link
Member

Choose a reason for hiding this comment

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

Is there no streaming version that can avoid this alloc in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I could find (at least on the Encoding class)

Copy link
Member

Choose a reason for hiding this comment

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

What about IncrementalHash.Create(Sha256), and then just append the data, instead of needing the initial array to copy into?

Copy link
Member

Choose a reason for hiding this comment

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

we also have the Checksum type. Which, TBH, feels pretty appropriate for this use case :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, reworked with using Checksums instead. It does seem easier this way.

Array.Resize(ref hash, 16);
var guid = new Guid(hash);
#if NET
Span<byte> bytesToChecksum = stackalloc byte[16];
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a constant we can use for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there is anything in the Guid class accessible to us. A regexp search for "guid.*16" in Roslyn has several hits, but nothing is sticking out as a good candidate to use.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants