Skip to content

Commit 8def26b

Browse files
cody-littleyCody Littley
andauthored
optimimze apply_change_sets_prepare (#4379)
This PR optimizes the apply_change_sets_prepare phase of the execution loop. After this change, peak TPS is ~180k TPS in cryptosim benchmark. --------- Co-authored-by: Cody Littley <cody.littley@seinetwork.io>
1 parent 7b415b7 commit 8def26b

8 files changed

Lines changed: 318 additions & 108 deletions

File tree

‎sei-db/common/keys/evm.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const (
4747
EVMKeyMisc // Full original key preserved (address mappings, codesize, etc.)
4848
)
4949

50+
// EVMKeyKindCount is the number of EVMKeyKind values, for sizing an array indexed by kind. It assumes EVMKeyMisc is
51+
// the last kind.
52+
const EVMKeyKindCount = int(EVMKeyMisc) + 1
53+
5054
// ParseEVMKey parses an EVM key from the x/evm store keyspace.
5155
//
5256
// For optimized keys (nonce, code, codehash, storage, balance), keyBytes is the stripped key.

‎sei-db/state_db/sc/flatkv/import_translator.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flatkv
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/sei-protocol/sei-chain/sei-db/common/keys"
89
"github.com/sei-protocol/sei-chain/sei-db/db_engine/view"
@@ -88,7 +89,7 @@ func (t *ImportTranslator) Translate(cs *proto.NamedChangeSet) ([]PhysicalKVPair
8889
Changeset: proto.ChangeSet{Pairs: filteredPairs},
8990
}
9091

91-
changesByType, err := classifyAndPrefix([]*proto.NamedChangeSet{filteredCS})
92+
changesByType, err := classifyAndPrefix([]*proto.NamedChangeSet{filteredCS}, [keys.EVMKeyKindCount]int{}, nil)
9293
if err != nil {
9394
return nil, err
9495
}
@@ -124,7 +125,10 @@ func (t *ImportTranslator) Translate(cs *proto.NamedChangeSet) ([]PhysicalKVPair
124125
if err != nil {
125126
return nil, fmt.Errorf("failed to merge account changes: %w", err)
126127
}
127-
for addr, batchUpdate := range batchAccts {
128+
for arenaAddr, batchUpdate := range batchAccts {
129+
// arenaAddr is carved from a key arena and this map outlives the call, so storing it as a key
130+
// would pin the arena for the rest of the import. Every map write stores its key.
131+
addr := strings.Clone(arenaAddr)
128132
existing, ok := t.pendingAccts[addr]
129133
if !ok {
130134
t.pendingAccts[addr] = batchUpdate
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package flatkv
2+
3+
import (
4+
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt/util"
5+
)
6+
7+
// keyArena hands out immutable strings carved from one buffer of fixed capacity.
8+
//
9+
// A string from here keeps the whole buffer alive, so anything that retains one past the version that wrote it must
10+
// copy it.
11+
type keyArena struct {
12+
// The bytes handed out so far. Its capacity is the arena's size, and it never grows past it.
13+
buf []byte
14+
}
15+
16+
// newKeyArena returns an arena that holds capacity bytes of keys.
17+
func newKeyArena(capacity int) keyArena {
18+
return keyArena{buf: make([]byte, 0, capacity)}
19+
}
20+
21+
// intern copies key into the arena and returns it as a string. A key that does not fit in the arena's remaining
22+
// capacity gets its own allocation.
23+
func (a *keyArena) intern(key []byte) string {
24+
if len(key) > cap(a.buf)-len(a.buf) {
25+
return string(key)
26+
}
27+
28+
// The append stays within capacity, so it never moves the bytes earlier strings alias.
29+
start := len(a.buf)
30+
a.buf = append(a.buf, key...)
31+
return util.UnsafeBytesToString(a.buf[start:])
32+
}

‎sei-db/state_db/sc/flatkv/lthash_agreement_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ type accountEdit struct {
209209
func (m *stateModel) apply(t *testing.T, blockHeight int64, changeSets []*proto.NamedChangeSet) {
210210
t.Helper()
211211

212-
// Within one call the last write for a physical key wins, matching classifyAndPrefix's per-kind
213-
// maps. Accounts additionally accumulate across the two logical keys that share their row.
212+
// Within one call the last write for a physical key wins. Accounts additionally accumulate across the
213+
// two logical keys that share their row.
214214
accountEdits := make(map[string]*accountEdit)
215215
storage := make(map[string][]byte)
216216
code := make(map[string][]byte)

‎sei-db/state_db/sc/flatkv/store.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ type CommitStore struct {
166166

167167
phaseTimer *metrics.PhaseTimer
168168

169+
// classifyBucketSizes is the length of each kind's bucket in the previous ApplyChangeSets call, used to size
170+
// the next call's buckets. Zero for a kind that call did not touch.
171+
classifyBucketSizes [keys.EVMKeyKindCount]int
172+
169173
// readOnly marks stores opened via LoadVersionReadOnly.
170174
readOnly bool
171175

0 commit comments

Comments
 (0)