-
Notifications
You must be signed in to change notification settings - Fork 14
Performance Tips
When using the BitcoinBlockchain library you will process blocks returned as an enumeration by a call to BlockchainParser.ParseBlockchain():
foreach (BitcoinBlockchain.Data.Block block in blockchainParser.ParseBlockchain())
{
// Do something with block.
}
An instance of type BitcoinBlockchain.Data.Block holds information about all the transactions, inputs and outputs in one block and can use a lot of memory. To improve both the memory footprint of your application as well as its speed consider these ideas:
-
After you are done processing a block do not keep it around in memory. For example do not simply collect all instances of type BitcoinBlockchain.Data.Block in a list. That would consume huge amounts of memory.
-
If during processing you need to store so much data that you expect to exceed 2 GB of memory, build your application for the x64 configuration.
-
To improve the performance of your application you may want to dispatch the processing of a block on a background thread. If you do that however you need to account for the fact that multiple blocks will be processed concurrently. You have to be prepared to deal with various multi-threading aspects. For example a transaction input may end up being processed before its source output. You may want to consider a hybrid approach where for one given block some of the processing is done on the main thread and some of the processing is dispatched on a background thread. Do not use too many background threads. If your processing is CPU-bound, the rule of thumb is not to use more threads than logical CPU cores. The actual way to determine the optimal number can be a little bit more complicated especially when your processing is not CPU-bound.