Skip to content

Commit 856d82f

Browse files
committed
use smaller degree btree to reduce memory allocations
1 parent 0956d8b commit 856d82f

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

btree.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ type BTree[T any] struct {
1212
}
1313

1414
// NewBTree returns a new BTree.
15-
func NewBTree[T any](less func(a, b T) bool) *BTree[T] {
16-
tree := btree.NewBTreeGOptions[T](less, btree.Options{
15+
func NewBTree[T any](less func(a, b T) bool, degree int) *BTree[T] {
16+
tree := btree.NewBTreeGOptions(less, btree.Options{
1717
NoLocks: true,
1818
ReadOnly: true,
19+
Degree: degree,
1920
})
2021
t := &BTree[T]{}
2122
t.Store(tree)

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/cometbft/cometbft v0.38.2
88
github.com/test-go/testify v1.1.4
99
github.com/tidwall/btree v1.7.0
10-
golang.org/x/sync v0.4.0
1110
)
1211

1312
require (
@@ -71,4 +70,4 @@ require (
7170
gopkg.in/yaml.v3 v3.0.1 // indirect
7271
)
7372

74-
replace github.com/tidwall/btree => github.com/yihuang/btree v0.0.0-20240215071918-6726a9b22e40
73+
replace github.com/tidwall/btree => github.com/yihuang/btree v0.0.0-20240318010431-d365682df9a7

go.sum

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d/go.mod h1:RRCYJ
233233
github.com/test-go/testify v1.1.4 h1:Tf9lntrKUMHiXQ07qBScBTSA0dhYQlu83hswqelv1iE=
234234
github.com/test-go/testify v1.1.4/go.mod h1:rH7cfJo/47vWGdi4GPj16x3/t1xGOj2YxzmNQzk2ghU=
235235
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
236-
github.com/yihuang/btree v0.0.0-20240215071918-6726a9b22e40 h1:FPcNbZSQgK2d/Oj5tD4TwJM8ealdG4DLYvETLV9D5EE=
237-
github.com/yihuang/btree v0.0.0-20240215071918-6726a9b22e40/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
236+
github.com/yihuang/btree v0.0.0-20240318010431-d365682df9a7 h1:TgWa3HbVnYmB4cYKvT+bY/6r6yqZZLCd6T1RMLi6qeg=
237+
github.com/yihuang/btree v0.0.0-20240318010431-d365682df9a7/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
238238
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
239239
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
240240
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -338,9 +338,12 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
338338
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
339339
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
340340
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
341+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
341342
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
342343
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
343344
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
344345
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
345346
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
346347
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
348+
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
349+
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

mvdata.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import (
44
"bytes"
55
)
66

7+
const (
8+
// Since we do copy-on-write a lot, smaller degree means smaller allocations
9+
OuterBTreeDegree = 4
10+
InnerBTreeDegree = 4
11+
)
12+
713
type MVData struct {
814
BTree[dataItem]
915
}
1016

1117
func NewMVData() *MVData {
12-
return &MVData{*NewBTree[dataItem](KeyItemLess)}
18+
return &MVData{*NewBTree(KeyItemLess[dataItem], OuterBTreeDegree)}
1319
}
1420

1521
// getTree returns `nil` if not found
@@ -22,7 +28,7 @@ func (d *MVData) getTree(key Key) *BTree[secondaryDataItem] {
2228
func (d *MVData) getTreeOrDefault(key Key) *BTree[secondaryDataItem] {
2329
return d.GetOrDefault(dataItem{Key: key}, func(item *dataItem) {
2430
if item.Tree == nil {
25-
item.Tree = NewBTree(secondaryDataItemLess)
31+
item.Tree = NewBTree(secondaryLesser, InnerBTreeDegree)
2632
}
2733
}).Tree
2834
}
@@ -146,7 +152,7 @@ type secondaryDataItem struct {
146152
Estimate bool
147153
}
148154

149-
func secondaryDataItemLess(a, b secondaryDataItem) bool {
155+
func secondaryLesser(a, b secondaryDataItem) bool {
150156
// reverse the order
151157
return a.Index > b.Index
152158
}

0 commit comments

Comments
 (0)