@@ -16,6 +16,9 @@ import (
16
16
// leafLength is the length of a seriailzed leaf.
17
17
const leafLength = chainhash .HashSize + 1
18
18
19
+ // buffer size for VLQ serialization. Double the size needed to serialize 2^64
20
+ const vlqBufSize = 22
21
+
19
22
// serializeLeaf serializes the leaf to [leafLength]byte.
20
23
func serializeLeaf (leaf utreexo.Leaf ) [leafLength ]byte {
21
24
var buf [leafLength ]byte
@@ -68,12 +71,11 @@ func InitNodesBackEnd(datadir string, maxTotalMemoryUsage int64) (*NodesBackEnd,
68
71
69
72
// dbPut serializes and puts the key value pair into the database.
70
73
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 )
74
76
75
77
serialized := serializeLeaf (v )
76
- return m .db .Put (buf [:], serialized [:], nil )
78
+ return m .db .Put (buf [:size ], serialized [:], nil )
77
79
}
78
80
79
81
// 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) {
98
100
99
101
// dbDel removes the key from the database.
100
102
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 )
105
106
}
106
107
107
108
// Get returns the leaf from the underlying map.
@@ -276,7 +277,7 @@ func (m *NodesBackEnd) flush() {
276
277
}
277
278
})
278
279
279
- m .cache .DeleteMaps ()
280
+ m .cache .ClearMaps ()
280
281
}
281
282
282
283
// Close flushes the cache and closes the underlying database.
@@ -298,10 +299,9 @@ type CachedLeavesBackEnd struct {
298
299
299
300
// dbPut serializes and puts the key and the value into the database.
300
301
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 )
305
305
}
306
306
307
307
// dbGet fetches and deserializes the value from the database.
@@ -411,7 +411,7 @@ func (m *CachedLeavesBackEnd) flush() {
411
411
}
412
412
})
413
413
414
- m .cache .DeleteMaps ()
414
+ m .cache .ClearMaps ()
415
415
}
416
416
417
417
// Close flushes all the cached entries and then closes the underlying database.
0 commit comments