cosmos / cosmos-sdk

:chains: A Framework for Building High Value Public Blockchains :sparkles:
https://cosmos.network/
Apache License 2.0
6.22k stars 3.59k forks source link

[Feature]: Run cachemulti.store.write in parallel #20787

Open ValarDragon opened 3 months ago

ValarDragon commented 3 months ago

Summary

Cachemulti.store.Write calls every constituent CacheKV store's write method. https://github.com/cosmos/cosmos-sdk/blob/main/store/cachemulti/store.go#L122-L128

Now that in SDK v50 onwards we are using working hashes, where these don't directly write to a shared DB, we should switch this to doing every write in a goroutine and having a sync.WaitGroup to wait for all the writes to complete

Problem Definition

Improves an important speed bottleneck in block execution + syncing.

Proposed Feature

Make the code

// Write calls Write on each underlying store.
func (cms Store) Write() {
    cms.db.Write()
        wg := sync.WaitGroup{}
       wg.Add(len(cms.stores))
    for _, store := range cms.stores {
               go func() {
                store.Write()
                wg.Done()
               }()
    }
        wg.Wait()
}
cool-develope commented 3 months ago

great solution 🔥