Optimize HexEncoder.DecodeData methods for .NET 8+#1317
Conversation
| using System.Threading.Tasks; | ||
|
|
||
| namespace NBitcoin.Bench | ||
| namespace NBitcoin.Bench; |
There was a problem hiding this comment.
| [Benchmark] | ||
| public void DecodeDataSpan() | ||
| { | ||
| Span<byte> tmp = stackalloc byte[32]; |
There was a problem hiding this comment.
| throw new ArgumentException("output should be bigger", nameof(output)); | ||
|
|
||
| #if NET8_0_OR_GREATER | ||
| var decoded = Convert.FromHexString(encoded); |
There was a problem hiding this comment.
Even though this allocates a byte array, it's still faster to use Convert.FromHexString because it uses SIMD instructions.
There was a problem hiding this comment.
An alternative would be to target NET 10 TFM (along with NET 8.0) by NBitcoin and just use Convert.FromHexString(string source, Span<byte> destination). I'm not sure if it's worth it and how much work it would require (mostly updating a CI script and a csproj file I guess).
HexEncoder.DecodeData methodsHexEncoder.DecodeData methods for .NET 8+
|
@Carti-it hey, the bench is on 32 bytes. Can you try with bigger side? I fear that it will have a big impact on GC. |
fac028a to
49d403e
Compare
I added 49d403e and the results are:
So it does not appear to be worse with longer strings. Moreover, It is true that |
|
@Carti-it 96 bytes is still small, I am thinking about 10 KB for example (a typical large transaction) How come |
|
master
PR
There was a bug in the bench code: 0293327 |
|
Released NBitcoin.Secp256k1/v4.0.1 and NBitcoin.10.0.7 with your latest changes. |
Benchmark
master:
PR (with git 2 commits):
PR (with git 3 commits):
Decoding is about twice as fast with the PR compared to master.