@@ -7,9 +7,12 @@ import (
7
7
"errors"
8
8
"testing"
9
9
10
+ "github.com/ChainSafe/gossamer/internal/trie/codec"
10
11
"github.com/ChainSafe/gossamer/internal/trie/node"
12
+ "github.com/ChainSafe/gossamer/lib/trie"
11
13
"github.com/golang/mock/gomock"
12
14
"github.com/stretchr/testify/assert"
15
+ "github.com/stretchr/testify/require"
13
16
)
14
17
15
18
func Test_Generate (t * testing.T ) {
@@ -384,3 +387,39 @@ func Test_lenCommonPrefix(t *testing.T) {
384
387
})
385
388
}
386
389
}
390
+
391
+ // Note on the performance of walk:
392
+ // It was tried to optimise appending to the encodedProofNodes
393
+ // slice by:
394
+ // 1. appending to the same slice *[][]byte passed as argument
395
+ // 2. appending the upper node to the deeper nodes slice
396
+ // In both cases, the performance difference is very small
397
+ // so the code is kept to this inefficient-looking append,
398
+ // which is in the end quite performant still.
399
+ func Benchmark_walk (b * testing.B ) {
400
+ trie := trie .NewEmptyTrie ()
401
+
402
+ // Build a deep trie.
403
+ const trieDepth = 1000
404
+ for i := 0 ; i < trieDepth ; i ++ {
405
+ keySize := 1 + i
406
+ key := make ([]byte , keySize )
407
+ const trieValueSize = 10
408
+ value := make ([]byte , trieValueSize )
409
+
410
+ trie .Put (key , value )
411
+ }
412
+
413
+ longestKeyLE := make ([]byte , trieDepth )
414
+ longestKeyNibbles := codec .KeyLEToNibbles (longestKeyLE )
415
+
416
+ rootNode := trie .RootNode ()
417
+ encodedProofNodes , err := walk (rootNode , longestKeyNibbles )
418
+ require .NoError (b , err )
419
+ require .Equal (b , len (encodedProofNodes ), trieDepth )
420
+
421
+ b .ResetTimer ()
422
+ for i := 0 ; i < b .N ; i ++ {
423
+ _ , _ = walk (rootNode , longestKeyNibbles )
424
+ }
425
+ }
0 commit comments