Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize memdb #1258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/store/ledgerstore/ledger_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,10 @@ func (this *LedgerStoreImp) executeBlock(block *types.Block) (result store.Execu
})

cache := storage.NewCacheDB(overlay)
for _, tx := range block.Transactions {
cache.Reset()
for i, tx := range block.Transactions {
if i > 0 {
cache.Reset()
}
notify, crossStateHashes, e := this.handleTransaction(overlay, cache, gasTable, block, tx)
if e != nil {
err = e
Expand Down
28 changes: 21 additions & 7 deletions core/store/overlaydb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
package overlaydb

import (
"math/rand"
"math"
_ "unsafe" // required by go:linkname

"github.com/syndtr/goleveldb/leveldb/comparer"
"github.com/syndtr/goleveldb/leveldb/errors"
Expand Down Expand Up @@ -184,26 +185,41 @@ const (
// MemDB is an in-memdb key/value database.
type MemDB struct {
cmp comparer.BasicComparer
rnd *rand.Rand

kvData []byte
// Node data:
// [0] : KV offset
// [1] : Key length
// [2] : Value length
// [3] : Height
// [3..height] : Next nodes
// [0..height] : Next nodes
nodeData []int
prevNode [tMaxHeight]int
maxHeight int
n int
kvSize int
}

//go:linkname fastRand runtime.fastrand
func fastRand() uint32

var probabilityTable []uint32

const probability float64 = 1.0 / 4

func init() {
for i := 1; i < tMaxHeight; i++ {
prob := math.Pow(probability, float64(i))
probabilityTable = append(probabilityTable, uint32(prob*math.MaxUint32))
}
}

func (p *MemDB) randHeight() (h int) {
const branching = 4

r := fastRand()

h = 1
for h < tMaxHeight && p.rnd.Int()%branching == 0 {
for h < tMaxHeight && r < probabilityTable[h-1] {
h++
}
return
Expand Down Expand Up @@ -415,7 +431,6 @@ func (p *MemDB) Len() int {

// Reset resets the MemDB to initial empty state. Allows reuse the buffer.
func (p *MemDB) Reset() {
p.rnd = rand.New(rand.NewSource(0xdeadbeef))
p.maxHeight = 1
p.n = 0
p.kvSize = 0
Expand All @@ -442,7 +457,6 @@ func (p *MemDB) Reset() {
func NewMemDB(capacity int, kvNum int) *MemDB {
p := &MemDB{
cmp: comparer.DefaultComparer,
rnd: rand.New(rand.NewSource(0xdeadbeef)),
maxHeight: 1,
kvData: make([]byte, 0, capacity),
nodeData: make([]int, 4+tMaxHeight, (4+tMaxHeight)*(1+kvNum)),
Expand Down