Skip to content

Commit

Permalink
Add performance note on local scoped variants slice
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 30, 2022
1 parent fd15b16 commit 374a29b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/trie/node/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/trie/node/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 374a29b

Please sign in to comment.