Skip to content

Commit 1a0fd52

Browse files
committed
serialize-deserialize capacity
1 parent c89239b commit 1a0fd52

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

crypto/statetrie/nibbles/nibbles.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2019-2023 Algorand, Inc.
1+
// Copyright (C) 2018-2023 Algorand, Inc.
22
// This file is part of go-algorand
33
//
44
// go-algorand is free software: you can redistribute it and/or modify
@@ -72,7 +72,7 @@ func MakeNibbles(data []byte, oddLength bool) Nibbles {
7272
// [] -> [], false
7373
func Pack(nyb Nibbles) ([]byte, bool) {
7474
length := len(nyb)
75-
data := make([]byte, length/2+length%2)
75+
data := make([]byte, length/2+length%2, length/2+length%2+1)
7676
for i := 0; i < length; i++ {
7777
if i%2 == 0 {
7878
data[i/2] = nyb[i] << 4
@@ -131,19 +131,13 @@ func SharedPrefix(nyb1 Nibbles, nyb2 Nibbles) Nibbles {
131131
// [0x1, 0x2, 0x3, 0x4] -> [0x12, 0x34, 0x03]
132132
// [] -> [0x03]
133133
func Serialize(nyb Nibbles) (data []byte) {
134-
p, h := Pack(nyb)
135-
length := len(p)
136-
output := make([]byte, length+1)
137-
copy(output, p)
138-
if h {
134+
if p, h := Pack(nyb); h {
139135
// 0x01 is the odd length indicator
140-
output[length] = oddIndicator
136+
return append(p, oddIndicator)
141137
} else {
142138
// 0x03 is the even length indicator
143-
output[length] = evenIndicator
139+
return append(p, evenIndicator)
144140
}
145-
146-
return output
147141
}
148142

149143
// Deserialize returns a nibble array from the byte array.

0 commit comments

Comments
 (0)