Skip to content

Commit c01dd2e

Browse files
authored
core/state: replace fastcache code cache with gc-friendly structure (#92)
* core/state, trie: fix memleak from fastcache, core/state: replace fastcache code cache with gc-friendly structure * common/lru: fix race in lru
1 parent b3fc060 commit c01dd2e

File tree

3 files changed

+217
-7
lines changed

3 files changed

+217
-7
lines changed

common/lru/blob_lru.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package lru
18+
19+
import (
20+
"math"
21+
"sync"
22+
23+
"github.com/hashicorp/golang-lru/simplelru"
24+
"github.com/scroll-tech/go-ethereum/common"
25+
)
26+
27+
// SizeConstrainedLRU is a wrapper around simplelru.LRU. The simplelru.LRU is capable
28+
// of item-count constraints, but is not capable of enforcing a byte-size constraint,
29+
// hence this wrapper.
30+
// OBS: This cache assumes that items are content-addressed: keys are unique per content.
31+
// In other words: two Add(..) with the same key K, will always have the same value V.
32+
type SizeConstrainedLRU struct {
33+
size uint64
34+
maxSize uint64
35+
lru *simplelru.LRU
36+
lock sync.Mutex
37+
}
38+
39+
// NewSizeConstrainedLRU creates a new SizeConstrainedLRU.
40+
func NewSizeConstrainedLRU(max uint64) *SizeConstrainedLRU {
41+
lru, err := simplelru.NewLRU(math.MaxInt, nil)
42+
if err != nil {
43+
panic(err)
44+
}
45+
return &SizeConstrainedLRU{
46+
size: 0,
47+
maxSize: max,
48+
lru: lru,
49+
}
50+
}
51+
52+
// Add adds a value to the cache. Returns true if an eviction occurred.
53+
// OBS: This cache assumes that items are content-addressed: keys are unique per content.
54+
// In other words: two Add(..) with the same key K, will always have the same value V.
55+
// OBS: The value is _not_ copied on Add, so the caller must not modify it afterwards.
56+
func (c *SizeConstrainedLRU) Add(key common.Hash, value []byte) (evicted bool) {
57+
c.lock.Lock()
58+
defer c.lock.Unlock()
59+
60+
// Unless it is already present, might need to evict something.
61+
// OBS: If it is present, we still call Add internally to bump the recentness.
62+
if !c.lru.Contains(key) {
63+
targetSize := c.size + uint64(len(value))
64+
for targetSize > c.maxSize {
65+
evicted = true
66+
_, v, ok := c.lru.RemoveOldest()
67+
if !ok {
68+
// list is now empty. Break
69+
break
70+
}
71+
targetSize -= uint64(len(v.([]byte)))
72+
}
73+
c.size = targetSize
74+
}
75+
c.lru.Add(key, value)
76+
return evicted
77+
}
78+
79+
// Get looks up a key's value from the cache.
80+
func (c *SizeConstrainedLRU) Get(key common.Hash) []byte {
81+
c.lock.Lock()
82+
defer c.lock.Unlock()
83+
84+
if v, ok := c.lru.Get(key); ok {
85+
return v.([]byte)
86+
}
87+
return nil
88+
}

common/lru/blob_lru_test.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package lru
18+
19+
import (
20+
"encoding/binary"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/scroll-tech/go-ethereum/common"
25+
)
26+
27+
func mkHash(i int) common.Hash {
28+
h := make([]byte, 32)
29+
binary.LittleEndian.PutUint64(h, uint64(i))
30+
return common.BytesToHash(h)
31+
}
32+
33+
func TestBlobLru(t *testing.T) {
34+
lru := NewSizeConstrainedLRU(100)
35+
var want uint64
36+
// Add 11 items of 10 byte each. First item should be swapped out
37+
for i := 0; i < 11; i++ {
38+
k := mkHash(i)
39+
v := fmt.Sprintf("value-%04d", i)
40+
lru.Add(k, []byte(v))
41+
want += uint64(len(v))
42+
if want > 100 {
43+
want = 100
44+
}
45+
if have := lru.size; have != want {
46+
t.Fatalf("size wrong, have %d want %d", have, want)
47+
}
48+
}
49+
// Zero:th should be evicted
50+
{
51+
k := mkHash(0)
52+
if val := lru.Get(k); val != nil {
53+
t.Fatalf("should be evicted: %v", k)
54+
}
55+
}
56+
// Elems 1-11 should be present
57+
for i := 1; i < 11; i++ {
58+
k := mkHash(i)
59+
want := fmt.Sprintf("value-%04d", i)
60+
have := lru.Get(k)
61+
if have == nil {
62+
t.Fatalf("missing key %v", k)
63+
}
64+
if string(have) != want {
65+
t.Fatalf("wrong value, have %v want %v", have, want)
66+
}
67+
}
68+
}
69+
70+
// TestBlobLruOverflow tests what happens when inserting an element exceeding
71+
// the max size
72+
func TestBlobLruOverflow(t *testing.T) {
73+
lru := NewSizeConstrainedLRU(100)
74+
// Add 10 items of 10 byte each, filling the cache
75+
for i := 0; i < 10; i++ {
76+
k := mkHash(i)
77+
v := fmt.Sprintf("value-%04d", i)
78+
lru.Add(k, []byte(v))
79+
}
80+
// Add one single large elem. We expect it to swap out all entries.
81+
{
82+
k := mkHash(1337)
83+
v := make([]byte, 200)
84+
lru.Add(k, v)
85+
}
86+
// Elems 0-9 should be missing
87+
for i := 1; i < 10; i++ {
88+
k := mkHash(i)
89+
if val := lru.Get(k); val != nil {
90+
t.Fatalf("should be evicted: %v", k)
91+
}
92+
}
93+
// The size should be accurate
94+
if have, want := lru.size, uint64(200); have != want {
95+
t.Fatalf("size wrong, have %d want %d", have, want)
96+
}
97+
// Adding one small item should swap out the large one
98+
{
99+
i := 0
100+
k := mkHash(i)
101+
v := fmt.Sprintf("value-%04d", i)
102+
lru.Add(k, []byte(v))
103+
if have, want := lru.size, uint64(10); have != want {
104+
t.Fatalf("size wrong, have %d want %d", have, want)
105+
}
106+
}
107+
}
108+
109+
// TestBlobLruSameItem tests what happens when inserting the same k/v multiple times.
110+
func TestBlobLruSameItem(t *testing.T) {
111+
lru := NewSizeConstrainedLRU(100)
112+
// Add one 10 byte-item 10 times
113+
k := mkHash(0)
114+
v := fmt.Sprintf("value-%04d", 0)
115+
for i := 0; i < 10; i++ {
116+
lru.Add(k, []byte(v))
117+
}
118+
// The size should be accurate
119+
if have, want := lru.size, uint64(10); have != want {
120+
t.Fatalf("size wrong, have %d want %d", have, want)
121+
}
122+
}

core/state/database.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"errors"
2121
"fmt"
2222

23-
"github.com/VictoriaMetrics/fastcache"
2423
lru "github.com/hashicorp/golang-lru"
24+
lru2 "github.com/scroll-tech/go-ethereum/common/lru"
2525

2626
"github.com/scroll-tech/go-ethereum/common"
2727
"github.com/scroll-tech/go-ethereum/core/rawdb"
@@ -123,14 +123,14 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
123123
zktrie: config != nil && config.Zktrie,
124124
db: trie.NewDatabaseWithConfig(db, config),
125125
codeSizeCache: csc,
126-
codeCache: fastcache.New(codeCacheSize),
126+
codeCache: lru2.NewSizeConstrainedLRU(codeCacheSize),
127127
}
128128
}
129129

130130
type cachingDB struct {
131131
db *trie.Database
132132
codeSizeCache *lru.Cache
133-
codeCache *fastcache.Cache
133+
codeCache *lru2.SizeConstrainedLRU
134134
zktrie bool
135135
}
136136

@@ -180,12 +180,12 @@ func (db *cachingDB) CopyTrie(t Trie) Trie {
180180

181181
// ContractCode retrieves a particular contract's code.
182182
func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
183-
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
183+
if code := db.codeCache.Get(codeHash); len(code) > 0 {
184184
return code, nil
185185
}
186186
code := rawdb.ReadCode(db.db.DiskDB(), codeHash)
187187
if len(code) > 0 {
188-
db.codeCache.Set(codeHash.Bytes(), code)
188+
db.codeCache.Add(codeHash, code)
189189
db.codeSizeCache.Add(codeHash, len(code))
190190
return code, nil
191191
}
@@ -196,12 +196,12 @@ func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error
196196
// code can't be found in the cache, then check the existence with **new**
197197
// db scheme.
198198
func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
199-
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
199+
if code := db.codeCache.Get(codeHash); len(code) > 0 {
200200
return code, nil
201201
}
202202
code := rawdb.ReadCodeWithPrefix(db.db.DiskDB(), codeHash)
203203
if len(code) > 0 {
204-
db.codeCache.Set(codeHash.Bytes(), code)
204+
db.codeCache.Add(codeHash, code)
205205
db.codeSizeCache.Add(codeHash, len(code))
206206
return code, nil
207207
}

0 commit comments

Comments
 (0)