MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.88k stars 848 forks source link

Perf improvments to uint256 #674

Open dangershony opened 5 years ago

dangershony commented 5 years ago

Considering this commit https://github.com/MetacoSA/NBitcoin/pull/671

We have some benchmarks and ideas to improve uint256 here https://github.com/stratisproject/StratisBitcoinFullNode/pull/3379

@nopara73 @maciejzaleski @MithrilMan

NicolasDorier commented 5 years ago

I think my uint256 is already more efficient as I use Span.

MithrilMan commented 5 years ago

@NicolasDorier I'd take a look at ArrayPool, you can save an allocation just renting from it the bytes you need, see https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1.shared?view=netcore-2.1

NicolasDorier commented 5 years ago

I save more by allocating on the stack with spans.

MithrilMan commented 5 years ago

maybe the threshold of the gain is based on the amount of allocated bytes, for little objects like uint256 maybe I agree (benchmark is always the response to doubts, probably I'll do some when I'll start working on Spans)

Even implement object pooling for uint256 could be an improvement, but to be able to use them properly and be seamlessly used with Span+ArrayPool, uint256 should be of ref struct type

Well when I'll ve some result to share I will

NicolasDorier commented 5 years ago

Actually I tried long ago to have structs for Money and uint256. Was not as good as I expected.

It actually consumed more RAM for the ConcurrentChain for example because instead of having pointers on one object, we had duplicated objects.

MithrilMan commented 5 years ago

but did your test were using Span at the time? with ref struct you can just borrow items from the ArrayPool as binary and cast them to Span<uint256>

MithrilMan commented 5 years ago

and internal datastructure of uint256, instead of relying on 8 UInt32, could be composed just of a byte array where you use Span to have logical views like if it were 8 variables

NicolasDorier commented 5 years ago

I was not testing with Span and not with ref struct either.