From 374a29b6759981bcf91d56ae84bfda12cf158a65 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 30 Jun 2022 17:56:03 +0000 Subject: [PATCH] Add performance note on local scoped variants slice --- internal/trie/node/header.go | 3 +++ internal/trie/node/header_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/internal/trie/node/header.go b/internal/trie/node/header.go index d3773f4868..033c5e84e7 100644 --- a/internal/trie/node/header.go +++ b/internal/trie/node/header.go @@ -129,6 +129,9 @@ func decodeHeaderByte(header byte) (variantBits, // order by the number of bits each variant mask occupy // in the header byte. // See https://spec.polkadot.network/#defn-node-header + // Performance note: see `Benchmark_decodeHeaderByte`; + // running with a locally scoped slice is as fast as having + // it at global scope. variants := []variant{ leafVariant, // mask 1100_0000 branchVariant, // mask 1100_0000 diff --git a/internal/trie/node/header_test.go b/internal/trie/node/header_test.go index fd535ac39a..8c572bbaf2 100644 --- a/internal/trie/node/header_test.go +++ b/internal/trie/node/header_test.go @@ -418,3 +418,15 @@ func Test_decodeHeaderByte(t *testing.T) { }) } } + +func Benchmark_decodeHeaderByte(b *testing.B) { + // With global scoped variants slice: + // 3.453 ns/op 0 B/op 0 allocs/op + // With locally scoped variants slice: + // 3.441 ns/op 0 B/op 0 allocs/op + header := leafVariant.bits | 0b0000_0001 + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _, _, _ = decodeHeaderByte(header) + } +}