@@ -219,16 +219,18 @@ func (h *trienodeHistory) encode() ([]byte, []byte, []byte, error) {
219219 binary .Write (& headerSection , binary .BigEndian , h .meta .block ) // 8 byte
220220
221221 for _ , owner := range h .owners {
222+ paths := h .nodeList [owner ]
223+ restartCount := (len (paths ) + trienodeDataBlockRestartLen - 1 ) / trienodeDataBlockRestartLen
222224 // Fill the key section with node index
223225 var (
224226 prevKey []byte
225- restarts []uint32
227+ restarts = make ( []uint32 , 0 , restartCount * 2 )
226228 prefixLen int
227229
228230 internalKeyOffset uint32 // key offset within the trie data internally
229231 internalValOffset uint32 // value offset within the trie data internally
230232 )
231- for i , path := range h . nodeList [ owner ] {
233+ for i , path := range paths {
232234 key := []byte (path )
233235
234236 // Track the internal key and value offsets at the beginning of the
@@ -269,11 +271,13 @@ func (h *trienodeHistory) encode() ([]byte, []byte, []byte, error) {
269271
270272 // Encode trailer, the number of restart sections is len(restarts))/2,
271273 // as we track the offsets of both key and value sections.
272- var trailer []byte
273- for _ , number := range append ( restarts , uint32 ( len ( restarts )) / 2 ) {
274+ trailer := make ( []byte , 0 , 4 * ( len ( restarts ) + 1 ))
275+ for _ , number := range restarts {
274276 binary .BigEndian .PutUint32 (buf [:4 ], number )
275277 trailer = append (trailer , buf [:4 ]... )
276278 }
279+ binary .BigEndian .PutUint32 (buf [:4 ], uint32 (len (restarts ))/ 2 )
280+ trailer = append (trailer , buf [:4 ]... )
277281 if _ , err := keySection .Write (trailer ); err != nil {
278282 return nil , nil , nil , err
279283 }
0 commit comments