Skip to content

Commit b9fcbfc

Browse files
authored
Merge pull request #196 from cec489/flush-heap
Remove heap allocations when flushing the nodes or cached leaves
2 parents e166f10 + 853aa68 commit b9fcbfc

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

blockchain/internal/utreexobackends/cachedleavesmap.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ func (ms *CachedLeavesMapSlice) DeleteMaps() {
133133
}
134134
}
135135

136+
// ClearMaps clears all maps
137+
//
138+
// This function is safe for concurrent access.
139+
func (ms *CachedLeavesMapSlice) ClearMaps() {
140+
ms.mtx.Lock()
141+
defer ms.mtx.Unlock()
142+
143+
for i := range ms.maps {
144+
for key := range ms.maps[i] {
145+
delete(ms.maps[i], key)
146+
}
147+
}
148+
}
149+
136150
// ForEach loops through all the elements in the cachedleaves map slice and calls fn with the key-value pairs.
137151
//
138152
// This function is safe for concurrent access.

blockchain/internal/utreexobackends/nodesmap.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ func (ms *NodesMapSlice) DeleteMaps() {
170170
}
171171
}
172172

173+
// ClearMaps clears all maps
174+
//
175+
// This function is safe for concurrent access.
176+
func (ms *NodesMapSlice) ClearMaps() {
177+
ms.mtx.Lock()
178+
defer ms.mtx.Unlock()
179+
180+
for i := range ms.maps {
181+
for key := range ms.maps[i] {
182+
delete(ms.maps[i], key)
183+
}
184+
}
185+
}
186+
173187
// ForEach loops through all the elements in the nodes map slice and calls fn with the key-value pairs.
174188
//
175189
// This function is safe for concurrent access.

blockchain/utreexoio.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
// leafLength is the length of a seriailzed leaf.
1717
const leafLength = chainhash.HashSize + 1
1818

19+
// buffer size for VLQ serialization. Double the size needed to serialize 2^64
20+
const vlqBufSize = 22
21+
1922
// serializeLeaf serializes the leaf to [leafLength]byte.
2023
func serializeLeaf(leaf utreexo.Leaf) [leafLength]byte {
2124
var buf [leafLength]byte
@@ -68,12 +71,11 @@ func InitNodesBackEnd(datadir string, maxTotalMemoryUsage int64) (*NodesBackEnd,
6871

6972
// dbPut serializes and puts the key value pair into the database.
7073
func (m *NodesBackEnd) dbPut(k uint64, v utreexo.Leaf) error {
71-
size := serializeSizeVLQ(k)
72-
buf := make([]byte, size)
73-
putVLQ(buf, k)
74+
var buf [vlqBufSize]byte
75+
size := putVLQ(buf[:], k)
7476

7577
serialized := serializeLeaf(v)
76-
return m.db.Put(buf[:], serialized[:], nil)
78+
return m.db.Put(buf[:size], serialized[:], nil)
7779
}
7880

7981
// dbGet fetches the value from the database and deserializes it and returns
@@ -98,10 +100,9 @@ func (m *NodesBackEnd) dbGet(k uint64) (utreexo.Leaf, bool) {
98100

99101
// dbDel removes the key from the database.
100102
func (m *NodesBackEnd) dbDel(k uint64) error {
101-
size := serializeSizeVLQ(k)
102-
buf := make([]byte, size)
103-
putVLQ(buf, k)
104-
return m.db.Delete(buf, nil)
103+
var buf [vlqBufSize]byte
104+
size := putVLQ(buf[:], k)
105+
return m.db.Delete(buf[:size], nil)
105106
}
106107

107108
// Get returns the leaf from the underlying map.
@@ -276,7 +277,7 @@ func (m *NodesBackEnd) flush() {
276277
}
277278
})
278279

279-
m.cache.DeleteMaps()
280+
m.cache.ClearMaps()
280281
}
281282

282283
// Close flushes the cache and closes the underlying database.
@@ -298,10 +299,9 @@ type CachedLeavesBackEnd struct {
298299

299300
// dbPut serializes and puts the key and the value into the database.
300301
func (m *CachedLeavesBackEnd) dbPut(k utreexo.Hash, v uint64) error {
301-
size := serializeSizeVLQ(v)
302-
buf := make([]byte, size)
303-
putVLQ(buf, v)
304-
return m.db.Put(k[:], buf, nil)
302+
var buf [vlqBufSize]byte
303+
size := putVLQ(buf[:], v)
304+
return m.db.Put(k[:], buf[:size], nil)
305305
}
306306

307307
// dbGet fetches and deserializes the value from the database.
@@ -411,7 +411,7 @@ func (m *CachedLeavesBackEnd) flush() {
411411
}
412412
})
413413

414-
m.cache.DeleteMaps()
414+
m.cache.ClearMaps()
415415
}
416416

417417
// Close flushes all the cached entries and then closes the underlying database.

0 commit comments

Comments
 (0)