File tree Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change @@ -224,11 +224,9 @@ func loadPruningSnapshotHeights(db dbm.DB) ([]int64, error) {
224224}
225225
226226func int64SliceToBytes (slice ... int64 ) []byte {
227- bz := make ([]byte , 0 , len (slice )* 8 )
228- for _ , ph := range slice {
229- buf := make ([]byte , 8 )
230- binary .BigEndian .PutUint64 (buf , uint64 (ph ))
231- bz = append (bz , buf ... )
227+ bz := make ([]byte , len (slice )* 8 )
228+ for i , ph := range slice {
229+ binary .BigEndian .PutUint64 (bz [i << 3 :], uint64 (ph ))
232230 }
233231 return bz
234232}
Original file line number Diff line number Diff line change 11package pruning
22
33import (
4+ "encoding/binary"
45 "errors"
56 "fmt"
67 "testing"
@@ -454,3 +455,54 @@ func TestLoadSnapshotHeights_PruneNothing(t *testing.T) {
454455
455456 require .Nil (t , manager .LoadSnapshotHeights (db .NewMemDB ()))
456457}
458+
459+ func TestInt64SliceToBytes (t * testing.T ) {
460+ tests := []struct {
461+ name string
462+ input []int64
463+ expect []byte
464+ }{
465+ {
466+ name : "empty slice" ,
467+ input : []int64 {},
468+ expect : []byte {},
469+ },
470+ {
471+ name : "single value" ,
472+ input : []int64 {1 },
473+ expect : func () []byte {
474+ b := make ([]byte , 8 )
475+ binary .BigEndian .PutUint64 (b , 1 )
476+ return b
477+ }(),
478+ },
479+ {
480+ name : "multiple values" ,
481+ input : []int64 {1 , 2 },
482+ expect : func () []byte {
483+ b := make ([]byte , 16 )
484+ binary .BigEndian .PutUint64 (b [0 :], 1 )
485+ binary .BigEndian .PutUint64 (b [8 :], 2 )
486+ return b
487+ }(),
488+ },
489+ }
490+
491+ for _ , tt := range tests {
492+ t .Run (tt .name , func (t * testing.T ) {
493+ got := int64SliceToBytes (tt .input ... )
494+ require .Equal (t , tt .expect , got , "bytes mismatch" )
495+ })
496+ }
497+ }
498+
499+ func BenchmarkInt64SliceToBytes (b * testing.B ) {
500+ data := make ([]int64 , 1024 )
501+ for i := range data {
502+ data [i ] = int64 (i )
503+ }
504+
505+ for b .Loop () {
506+ _ = int64SliceToBytes (data ... )
507+ }
508+ }
You can’t perform that action at this time.
0 commit comments