Skip to content

Commit 264c06a

Browse files
jsvisarjl493456442
andauthored
triedb/pathdb: use binary.append to eliminate the tmp scratch slice (ethereum#32250)
`binary.AppendUvarint` offers better performance than using append directly, because it avoids unnecessary memory allocation and copying. In our case, it can increase the performance by +35.8% for the `blockWriter.append` function: ``` benchmark old ns/op new ns/op delta BenchmarkBlockWriterAppend-8 5.97 3.83 -35.80% ``` --------- Signed-off-by: jsvisa <[email protected]> Co-authored-by: Gary Rong <[email protected]>
1 parent 83aa643 commit 264c06a

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

triedb/pathdb/history_index_block.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,14 @@ func (br *blockReader) readGreaterThan(id uint64) (uint64, error) {
221221
type blockWriter struct {
222222
desc *indexBlockDesc // Descriptor of the block
223223
restarts []uint16 // Offsets into the data slice, marking the start of each section
224-
scratch []byte // Buffer used for encoding full integers or value differences
225224
data []byte // Aggregated encoded data slice
226225
}
227226

228227
func newBlockWriter(blob []byte, desc *indexBlockDesc) (*blockWriter, error) {
229-
scratch := make([]byte, binary.MaxVarintLen64)
230228
if len(blob) == 0 {
231229
return &blockWriter{
232-
desc: desc,
233-
scratch: scratch,
234-
data: make([]byte, 0, 1024),
230+
desc: desc,
231+
data: make([]byte, 0, 1024),
235232
}, nil
236233
}
237234
restarts, data, err := parseIndexBlock(blob)
@@ -241,7 +238,6 @@ func newBlockWriter(blob []byte, desc *indexBlockDesc) (*blockWriter, error) {
241238
return &blockWriter{
242239
desc: desc,
243240
restarts: restarts,
244-
scratch: scratch,
245241
data: data, // safe to own the slice
246242
}, nil
247243
}
@@ -268,22 +264,14 @@ func (b *blockWriter) append(id uint64) error {
268264
//
269265
// The first element in a restart range is encoded using its
270266
// full value.
271-
n := binary.PutUvarint(b.scratch[0:], id)
272-
b.data = append(b.data, b.scratch[:n]...)
267+
b.data = binary.AppendUvarint(b.data, id)
273268
} else {
274-
// The current section is not full, append the element.
275269
// The element which is not the first one in the section
276270
// is encoded using the value difference from the preceding
277271
// element.
278-
n := binary.PutUvarint(b.scratch[0:], id-b.desc.max)
279-
b.data = append(b.data, b.scratch[:n]...)
272+
b.data = binary.AppendUvarint(b.data, id-b.desc.max)
280273
}
281274
b.desc.entries++
282-
283-
// The state history ID must be greater than 0.
284-
//if b.desc.min == 0 {
285-
// b.desc.min = id
286-
//}
287275
b.desc.max = id
288276
return nil
289277
}
@@ -392,11 +380,10 @@ func (b *blockWriter) full() bool {
392380
//
393381
// This function is safe to be called multiple times.
394382
func (b *blockWriter) finish() []byte {
395-
var buf []byte
396-
for _, number := range b.restarts {
397-
binary.BigEndian.PutUint16(b.scratch[:2], number)
398-
buf = append(buf, b.scratch[:2]...)
383+
buf := make([]byte, len(b.restarts)*2+1)
384+
for i, restart := range b.restarts {
385+
binary.BigEndian.PutUint16(buf[2*i:], restart)
399386
}
400-
buf = append(buf, byte(len(b.restarts)))
387+
buf[len(buf)-1] = byte(len(b.restarts))
401388
return append(b.data, buf...)
402389
}

triedb/pathdb/history_index_block_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,22 @@ func BenchmarkParseIndexBlock(b *testing.B) {
232232
}
233233
}
234234
}
235+
236+
// BenchmarkBlockWriterAppend benchmarks the performance of indexblock.writer
237+
func BenchmarkBlockWriterAppend(b *testing.B) {
238+
b.ReportAllocs()
239+
b.ResetTimer()
240+
241+
desc := newIndexBlockDesc(0)
242+
writer, _ := newBlockWriter(nil, desc)
243+
244+
for i := 0; i < b.N; i++ {
245+
if writer.full() {
246+
desc = newIndexBlockDesc(0)
247+
writer, _ = newBlockWriter(nil, desc)
248+
}
249+
if err := writer.append(writer.desc.max + 1); err != nil {
250+
b.Error(err)
251+
}
252+
}
253+
}

0 commit comments

Comments
 (0)