Skip to content

Commit

Permalink
Improved staging shard performance
Browse files Browse the repository at this point in the history
using map instead of growing array as shard id can be 1000+ in some cases
  • Loading branch information
biospb committed May 4, 2022
1 parent beb038c commit 2c1e234
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions domain/consensus/model/staging_area.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import "github.com/pkg/errors"
import (
"github.com/pkg/errors"
)

// StagingShard is an interface that enables every store to have it's own Commit logic
// See StagingArea for more details
Expand All @@ -19,29 +21,27 @@ type StagingShardID uint64
// When the StagingArea is being Committed, it goes over all it's shards, and commits those one-by-one.
// Since Commit happens in a DatabaseTransaction, a StagingArea is atomic.
type StagingArea struct {
shards []StagingShard
shards map[StagingShardID]StagingShard
isCommitted bool
}

// NewStagingArea creates a new, empty staging area.
func NewStagingArea() *StagingArea {
return &StagingArea{
shards: []StagingShard{},
shards: make(map[StagingShardID]StagingShard),
isCommitted: false,
}
}

// GetOrCreateShard attempts to retrieve a shard with the given name.
// If it does not exist - a new shard is created using `createFunc`.
func (sa *StagingArea) GetOrCreateShard(shardID StagingShardID, createFunc func() StagingShard) StagingShard {
for uint64(len(sa.shards)) <= uint64(shardID) {
sa.shards = append(sa.shards, nil)
shard, ok := sa.shards[shardID]
if !ok {
shard = createFunc()
sa.shards[shardID] = shard
}
if sa.shards[shardID] == nil {
sa.shards[shardID] = createFunc()
}

return sa.shards[shardID]
return shard
}

// Commit goes over all the Shards in the StagingArea and commits them, inside the provided database transaction.
Expand All @@ -52,9 +52,6 @@ func (sa *StagingArea) Commit(dbTx DBTransaction) error {
}

for _, shard := range sa.shards {
if shard == nil { // since sa.shards is an array and not a map, some shard slots might be empty.
continue
}
err := shard.Commit(dbTx)
if err != nil {
return err
Expand Down

0 comments on commit 2c1e234

Please sign in to comment.