Skip to content

optimize utxo write function#850

Open
Psalmuel01 wants to merge 4 commits intokaspanet:masterfrom
Psalmuel01:utxo-write
Open

optimize utxo write function#850
Psalmuel01 wants to merge 4 commits intokaspanet:masterfrom
Psalmuel01:utxo-write

Conversation

@Psalmuel01
Copy link

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:

  1. Added a new write_many_without_cache method to DbUtxoSetStore that:

    • Uses a BatchDbWriter instead of a DirectDbWriter
    • Writes all UTXOs in a single batch operation
    • Skips cache updates for better performance
  2. Modified the existing write_many method to use the new write_many_without_cache method instead of directly using a DirectDbWriter

Motivation

The write_many function in the UTXO set store was identified as a performance bottleneck. The previous implementation used a DirectDbWriter to 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

  1. Batch Writing: By using WriteBatch and BatchDbWriter, we can accumulate all database operations and execute them as a single atomic transaction. This significantly reduces the overhead of multiple separate database writes.

  2. 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.

  3. Method Separation: By implementing a separate write_many_without_cache method, we maintain a clean API that clearly communicates the behavior while allowing for future optimizations or alternative implementations.

Trade-offs

  1. 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.

  2. 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.

  3. 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.

@IzioDev
Copy link
Collaborator

IzioDev commented Jan 28, 2026

Write many on UTXO seems to only be used by:

fn append_imported_pruning_point_utxos(&self, utxoset_chunk: &[(TransactionOutpoint, UtxoEntry)], current_multiset: &mut MuHash) {
let mut pruning_meta_write = self.pruning_meta_stores.write();
pruning_meta_write.utxo_set.write_many(utxoset_chunk).unwrap();

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

@someone235
Copy link
Contributor

Can you please add a benchmark that shows the improvement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants