MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.87k stars 846 forks source link

HAS_SPAN / NONATIVEHASH insights #666

Open MithrilMan opened 5 years ago

MithrilMan commented 5 years ago

Hi @NicolasDorier, I see that your code is decorated with #if HAS_SPAN / NONATIVEHASH

I understand your reason (you support different TargetFrameworks and some of them doesn't have support for the stuff you use inside these #if blocks)

What I'd like to know, is if you computed someohow the outcome of using Span/ReadonlySpan, ArrayPool, etc...), if you have some kind of benchmark or some consideration about those changes.

I'd want to start implement Span/ReadonlySpan and ArrayPool in Stratis code but before that I'd like to know your experience and considerations on using these classes.

The reason behind this is that during IBD on our node, I see lot of memory spike reported by Task Manager, but almost half of the reported memory is flagged as free memory, and we have high level of fragmentation in the heap. I think the cause is that current code instantiate too many stuff during serialization and consensus and this causes the memory spike (manually invoking GC.Collect with memory compaction recover lot of memory but this isn't a solution of course)

As a side question: have you thought about some of the crypto classes dotnetcore exposes now, instead of bouncycastle?

NicolasDorier commented 5 years ago

I did not do benchmark, but I think the move to span and array pool saved at least 50% of memory allocation. The best is to try NBitcoin.Benchmark to compare.

I am not aware of any dotnetcore class which exist which could replace some of my BouncyCastle stuff.