Debian Codegraph @

asm/metadata_test.go

raw symbols SCIP
1 package asm
2
3 import (
4 "testing"
5 "unsafe"
6
7 "github.com/go-quicktest/qt"
8 )
9
10 func TestMetadata(t *testing.T) {
11 var m Metadata
12
13 // Metadata should be the size of a pointer.
14 qt.Assert(t, qt.Equals(unsafe.Sizeof(m), unsafe.Sizeof(uintptr(0))))
15
16 // A lookup in a nil meta should return nil.
17 qt.Assert(t, qt.IsNil(m.Get(bool(false))))
18
19 // We can look up anything we inserted.
20 m.Set(bool(false), int(0))
21 m.Set(int(1), int(1))
22 qt.Assert(t, qt.Equals(m.Get(bool(false)), 0))
23 qt.Assert(t, qt.Equals(m.Get(1), 1))
24
25 // We have copy on write semantics
26 old := m
27 m.Set(bool(false), int(1))
28 qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
29 qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
30 qt.Assert(t, qt.Equals(old.Get(bool(false)), 0))
31 qt.Assert(t, qt.Equals(old.Get(int(1)), 1))
32
33 // Newtypes are handled distinctly.
34 type b bool
35 m.Set(b(false), int(42))
36 qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
37 qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
38 qt.Assert(t, qt.Equals(m.Get(b(false)), 42))
39
40 // Setting nil removes a key.
41 m.Set(bool(false), nil)
42 qt.Assert(t, qt.IsNil(m.Get(bool(false))))
43 qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
44 qt.Assert(t, qt.Equals(m.Get(b(false)), 42))
45 }
46
47 func BenchmarkMetadata(b *testing.B) {
48 // Assume that three bits of metadata on a single instruction is
49 // our worst case.
50 const worstCaseItems = 3
51
52 type t struct{}
53
54 b.Run("add first", func(b *testing.B) {
55 b.ReportAllocs()
56
57 for b.Loop() {
58 var v Metadata
59 v.Set(t{}, 0)
60 }
61 })
62
63 b.Run("add last", func(b *testing.B) {
64 var m Metadata
65 for i := range worstCaseItems - 1 {
66 m.Set(i, i)
67 }
68
69 b.ReportAllocs()
70
71 for b.Loop() {
72 v := m
73 v.Set(t{}, 0)
74 }
75 })
76
77 b.Run("add existing", func(b *testing.B) {
78 var m Metadata
79 for i := range worstCaseItems - 1 {
80 m.Set(i, i)
81 }
82 m.Set(t{}, 0)
83
84 b.ReportAllocs()
85
86 for b.Loop() {
87 v := m
88 v.Set(t{}, 0)
89 }
90 })
91
92 b.Run("get miss", func(b *testing.B) {
93 var m Metadata
94 for i := range worstCaseItems {
95 m.Set(i, i)
96 }
97
98 b.ReportAllocs()
99
100 for b.Loop() {
101 if m.Get(t{}) != nil {
102 b.Fatal("got result from miss")
103 }
104 }
105 })
106 }