Open
Conversation
Collaborator
|
Write many on UTXO seems to only be used by: rusty-kaspa/consensus/src/consensus/mod.rs Lines 1070 to 1072 in d44e31a Which seems to only be used by consensus tests here: https://github.com/kaspanet/rusty-kaspa/blob/master/testing/integration/src/consensus_integration_tests.rs |
Contributor
|
Can you please add a benchmark that shows the improvement? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Performance Optimization for UTXO Set Database Operations
Changes Made
This PR optimizes the database operations in the UTXO set store by implementing a more efficient approach to writing multiple UTXOs to the database. The changes include:
Added a new
write_many_without_cachemethod toDbUtxoSetStorethat:BatchDbWriterinstead of aDirectDbWriterModified the existing
write_manymethod to use the newwrite_many_without_cachemethod instead of directly using aDirectDbWriterMotivation
The
write_manyfunction in the UTXO set store was identified as a performance bottleneck. The previous implementation used aDirectDbWriterto write UTXOs to the database one by one and updated the cache for each write. This approach was less efficient for bulk operations, especially in high-throughput scenarios.Design Decisions
Batch Writing: By using
WriteBatchandBatchDbWriter, we can accumulate all database operations and execute them as a single atomic transaction. This significantly reduces the overhead of multiple separate database writes.Cache Skipping: The implementation deliberately skips writing to the cache. While this might seem counterintuitive, it's beneficial in scenarios where the UTXOs being written are not likely to be immediately read again, which is often the case in blockchain processing.
Method Separation: By implementing a separate
write_many_without_cachemethod, we maintain a clean API that clearly communicates the behavior while allowing for future optimizations or alternative implementations.Trade-offs
Read vs. Write Performance: Skipping cache updates improves write performance but might slightly degrade read performance for recently written UTXOs that need to be fetched from disk instead of cache. However, in the context of blockchain processing, the write performance gains typically outweigh this potential read performance cost.
Memory Usage: Using a batch operation means all the UTXOs to be written are temporarily held in memory. This increases memory usage compared to writing them one by one, but the performance gain justifies this trade-off for reasonable batch sizes.
Code Complexity: The implementation adds a new method, slightly increasing the codebase size. However, the method is well-documented and follows existing patterns in the codebase, making it easy to understand and maintain.
Testing
The changes have been tested to ensure they maintain the same functionality while improving performance. The existing test suite passes with these changes, confirming that the behavioral contract of the UTXO set store remains intact.