Skip to content

Optimize SHA computations for .NET 8+#1316

Merged
NicolasDorier merged 3 commits into
MetacoSA:masterfrom
Carti-it:optimize-sha-computations
Jun 15, 2026
Merged

Optimize SHA computations for .NET 8+#1316
NicolasDorier merged 3 commits into
MetacoSA:masterfrom
Carti-it:optimize-sha-computations

Conversation

@Carti-it

Copy link
Copy Markdown
Contributor

Stacked on #1315 to avoid conflicts

master:

Method Mean Error StdDev Gen0 Allocated
SHA1 546.5 ns 10.91 ns 25.06 ns 0.0353 224 B
SHA256 578.6 ns 11.50 ns 19.52 ns 0.0381 240 B
SHA512 679.1 ns 13.48 ns 24.30 ns 0.0477 304 B

PR:

Method Mean Error StdDev Median Gen0 Allocated
SHA1 300.7 ns 6.01 ns 10.68 ns 305.7 ns 0.0076 48 B
SHA256 365.5 ns 7.29 ns 10.46 ns 370.5 ns 0.0086 56 B
SHA512 445.5 ns 4.86 ns 3.79 ns 444.7 ns 0.0138 88 B

The CPU time improvements are in the range of 35% - 45% and memory improvements are 70%+.

Comment thread NBitcoin/Crypto/Hashes.cs
public static byte[] SHA256(ReadOnlySpan<byte> data)
{
#if NET8_0_OR_GREATER
return System.Security.Cryptography.SHA256.HashData(data);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is to avoid .ToArray() copy.

@NicolasDorier NicolasDorier merged commit c29ab25 into MetacoSA:master Jun 15, 2026
6 checks passed
@Carti-it Carti-it deleted the optimize-sha-computations branch June 15, 2026 06:05
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.

2 participants