From 7abf43cf55c186ee499f97094678c257a6126fd3 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet Date: Mon, 17 Feb 2020 18:07:16 +0100 Subject: [PATCH] Add the benchmarking code that was left out. --- core/state/snapshot/hextrie_generator.go | 10 +++++++++ core/state/snapshot/trie_generator_test.go | 26 ++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/state/snapshot/hextrie_generator.go b/core/state/snapshot/hextrie_generator.go index 51200668b660..e2f50033ef5a 100644 --- a/core/state/snapshot/hextrie_generator.go +++ b/core/state/snapshot/hextrie_generator.go @@ -29,6 +29,16 @@ type leaf struct { type trieGeneratorFn func(in chan (leaf), out chan (common.Hash)) +// StackGenerate is a hexary trie builder which is built from the bottom-up as +// keys are added. +func StackGenerate(in chan (leaf), out chan (common.Hash)) { + t := trie.NewStackTrie() + for leaf := range in { + t.TryUpdate(leaf.key[:], leaf.value) + } + out <- t.Hash() +} + // PruneGenerate is a hexary trie builder which collapses old nodes, but is still // based on more or less the ordinary trie builder func PruneGenerate(in chan (leaf), out chan (common.Hash)) { diff --git a/core/state/snapshot/trie_generator_test.go b/core/state/snapshot/trie_generator_test.go index c43b63eeec69..28727f90f96e 100644 --- a/core/state/snapshot/trie_generator_test.go +++ b/core/state/snapshot/trie_generator_test.go @@ -101,10 +101,12 @@ func TestTrieGenerationAppendonly(t *testing.T) { } } -// BenchmarkTrieGeneration/4K/standard-6 98 14141790 ns/op 6164989 B/op 57929 allocs/op -// BenchmarkTrieGeneration/4K/pruning-6 72 14015967 ns/op 6604020 B/op 54962 allocs/op -// BenchmarkTrieGeneration/10K/standard-6 42 30085495 ns/op 17280084 B/op 151006 allocs/op -// BenchmarkTrieGeneration/10K/pruning-6 32 34536586 ns/op 16510731 B/op 137402 allocs/op +// BenchmarkTrieGeneration/4K/standard-8 127 9429425 ns/op 6188077 B/op 58026 allocs/op +// BenchmarkTrieGeneration/4K/pruning-8 72 16544534 ns/op 6617322 B/op 55016 allocs/op +// BenchmarkTrieGeneration/4K/stack-8 159 6452936 ns/op 6308393 B/op 12022 allocs/op +// BenchmarkTrieGeneration/10K/standard-8 50 25025175 ns/op 17283703 B/op 151023 allocs/op +// BenchmarkTrieGeneration/10K/pruning-8 28 38141602 ns/op 16540254 B/op 137520 allocs/op +// BenchmarkTrieGeneration/10K/stack-8 60 18888649 ns/op 17557314 B/op 30067 allocs/op func BenchmarkTrieGeneration(b *testing.B) { // Get a fairly large trie // Create a custom account factory to recreate the same addresses @@ -150,6 +152,14 @@ func BenchmarkTrieGeneration(b *testing.B) { generateTrie(it, PruneGenerate) } }) + b.Run("stack", func(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00")) + generateTrie(it, StackGenerate) + } + }) }) b.Run("10K", func(b *testing.B) { // 4K accounts @@ -173,5 +183,13 @@ func BenchmarkTrieGeneration(b *testing.B) { generateTrie(it, PruneGenerate) } }) + b.Run("stack", func(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + it := head.(*diffLayer).AccountIterator(common.HexToHash("0x00")) + generateTrie(it, StackGenerate) + } + }) }) }