Skip to content

Commit 1cf1ea8

Browse files
rpc: reuse storage composite key buffers (erigontech#21205)
## Summary - Reuse a storage composite-key buffer in the cached history state reader. - Keep the address+slot key layout unchanged while avoiding per-read heap allocation. - Match the existing `HistoryReaderV3` composite-key reuse pattern. ## Benchmark <img width="1948" height="236" alt="519afec69b23bed9cfe7096cb5106034" src="https://github.com/user-attachments/assets/d6fdaf68-6417-44eb-bf1e-7ab11d3ea62c" /> ## Test plan - `gofmt -l rpc/rpchelper/helper.go` - `git diff --check -- rpc/rpchelper/helper.go` - `go test -timeout=20m ./rpc/rpchelper` - `make lint` Co-authored-by: lupin012 <58134934+lupin012@users.noreply.github.com>
1 parent 9e01d17 commit 1cf1ea8

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

rpc/rpchelper/helper.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,17 @@ func CreateHistoryCachedStateReader(ctx context.Context, cache kvcache.CacheView
236236
if minHistoryTxNum := state.StateHistoryStartTxNum(tx); txNum < minHistoryTxNum {
237237
return nil, fmt.Errorf("%w: block tx: %d, min tx: %d", state.PrunedError, txNum, minHistoryTxNum)
238238
}
239-
return &cachedHistoryReaderV3{asOfView, state.NewHistoryReaderV3(tx, txNum)}, nil
239+
return &cachedHistoryReaderV3{
240+
cache: asOfView,
241+
reader: state.NewHistoryReaderV3(tx, txNum),
242+
composite: make([]byte, 0, len(common.Address{})+len(common.Hash{})),
243+
}, nil
240244
}
241245

242246
type cachedHistoryReaderV3 struct {
243-
cache asOfView
244-
reader *state.HistoryReaderV3
247+
cache asOfView
248+
reader *state.HistoryReaderV3
249+
composite []byte
245250
}
246251

247252
func (hr *cachedHistoryReaderV3) SetTrace(trace bool, tracePrefix string) {
@@ -295,7 +300,8 @@ func (hr *cachedHistoryReaderV3) ReadAccountDataForDebug(address accounts.Addres
295300
func (hr *cachedHistoryReaderV3) ReadAccountStorage(address accounts.Address, key accounts.StorageKey) (uint256.Int, bool, error) {
296301
addressValue := address.Value()
297302
keyValue := key.Value()
298-
enc, ok, err := hr.cache.GetAsOf(append(addressValue[:], keyValue[:]...), hr.reader.GetTxNum())
303+
hr.composite = append(append(hr.composite[:0], addressValue[:]...), keyValue[:]...)
304+
enc, ok, err := hr.cache.GetAsOf(hr.composite, hr.reader.GetTxNum())
299305
if err != nil {
300306
return uint256.Int{}, false, err
301307
}

0 commit comments

Comments
 (0)