Skip to content

Commit 773b0b2

Browse files
committed
trie: swap out the pools in StackTrie with an arena allocator
Arena allocators are a perfect match for something like StackTrie where allocations happens in stack-like "frames" and can then be deallocation at the end of the "allocation frame" We can construct the "allocation frames" every time we create a new extension/branch node and decontstruct the frame by deallocating everything allocated on the arena during that frame. This saves us from dealing with individual allocations/deallocations
1 parent 0dacfef commit 773b0b2

File tree

4 files changed

+231
-103
lines changed

4 files changed

+231
-103
lines changed

common/arena.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2025 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 common
18+
19+
// Arena is an allocation primitive that allows individual allocations
20+
// out of a page of items and bulk de-allocations of last N allocations.
21+
// The most common way of using an Arena is to pair it up with a sync.Pool
22+
// of pages.
23+
type Arena[T any] struct {
24+
used uint32
25+
pages [][]T
26+
27+
PageSize uint32
28+
NewPage func() any
29+
ReleasePage func(any)
30+
}
31+
32+
// Alloc returns the next free item on the arena
33+
// Allocates a new page if needed
34+
func (a *Arena[T]) Alloc() *T {
35+
pageIndex := a.used / a.PageSize
36+
pageOffset := a.used % a.PageSize
37+
if pageOffset == 0 {
38+
a.pages = append(a.pages, a.NewPage().([]T))
39+
}
40+
a.used++
41+
return &a.pages[pageIndex][pageOffset]
42+
}
43+
44+
// Used returns the number of items that live on this arena
45+
func (a *Arena[T]) Used() uint32 {
46+
return a.used
47+
}
48+
49+
// Reset rollsback the active set of live elements to the given number
50+
func (a *Arena[T]) Reset(to uint32) {
51+
a.used = to
52+
}
53+
54+
// Release releases all the pages that the arena currently owns
55+
func (a *Arena[T]) Release() {
56+
for _, page := range a.pages {
57+
a.ReleasePage(page)
58+
}
59+
a.pages = nil
60+
a.used = 0
61+
}

trie/bytepool.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)