-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface_test.go
253 lines (199 loc) · 4.72 KB
/
interface_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package benchmarks
import (
"fmt"
"reflect"
"testing"
"unsafe"
)
type WithInterface struct {
something interface{}
}
func TestWithInterface(t *testing.T) {
var x WithInterface
x.something = "hat"
fmt.Printf("x is %#v\n", x)
x.something = 1337
fmt.Printf("x is %#v\n", x)
typ := reflect.TypeOf(x)
structField := typ.Field(0)
typ = structField.Type
fmt.Printf("Align=%d\n", typ.Align())
fmt.Printf("FieldAlign=%d\n", typ.FieldAlign())
fmt.Printf("Size=%d\n", typ.Size())
}
func TestLookAtPointer(t *testing.T) {
var val int = 1337
valptr := &val
// Store a the pointer to our int in an interface type
var store interface{} = valptr
// Print out where the int is stored in memory,
// and confirm the contents is as we expect.
fmt.Printf("int is at %p\n", valptr)
pointer := uintptr(unsafe.Pointer(valptr))
contents := *(*[8]byte)(unsafe.Pointer(pointer))
fmt.Printf("contents is %x (1337 in hex is %x)\n", contents, 1337)
// Print out where the interface{} is stored in memory,
// and look at the content
pointer = uintptr(unsafe.Pointer(&store))
fmt.Printf("interface{} is at %p\n", unsafe.Pointer(pointer))
ifcontents := *(*[16]byte)(unsafe.Pointer(pointer))
fmt.Printf("contents is %x\n", ifcontents)
// The interface{} looks like two pointers
pointers := *(*[2]uintptr)(unsafe.Pointer(&store))
fmt.Printf("1st pointer is %x\n", pointers[0])
fmt.Printf("2nd pointer is %x\n", pointers[1])
// The second pointer points to our integer
// We guess the initial pointer points to type information
typ := reflect.TypeOf(valptr)
pointers = *(*[2]uintptr)(unsafe.Pointer(&typ))
fmt.Printf("Type 1st pointer is %x\n", pointers[0])
fmt.Printf("Type 2nd pointer is %x\n", pointers[1])
}
func TestLookAtImmediate(t *testing.T) {
var val int = 1337
var store interface{} = val
pointer := uintptr(unsafe.Pointer(&store))
fmt.Printf("interface{} is at %p\n", unsafe.Pointer(pointer))
// The interface{} looks like two pointers
pointers := *(*[2]uintptr)(unsafe.Pointer(&store))
fmt.Printf("1st pointer is %x\n", pointers[0])
fmt.Printf("2nd pointer is %x\n", pointers[1])
valcontents := *(*[8]byte)(unsafe.Pointer(pointers[1]))
fmt.Printf("value contents is %x\n", valcontents)
// The second pointer points to our integer
// We guess the initial pointer points to type information
typ := reflect.TypeOf(val)
pointers = *(*[2]uintptr)(unsafe.Pointer(&typ))
fmt.Printf("Type 1st pointer is %x\n", pointers[0])
fmt.Printf("Type 2nd pointer is %x\n", pointers[1])
}
type benchType interface {
get() int
}
type typeA struct {
a int
}
func (a *typeA) get() int {
return a.a
}
type typeB struct {
b int
}
func (b *typeB) get() int {
return b.b
}
func BenchmarkInterfaceTypeAssert(b *testing.B) {
var x [2]interface{}
x[0] = &typeA{}
x[1] = &typeB{}
b.ReportAllocs()
b.ResetTimer()
count := 0
for i := 0; i < b.N; i++ {
y := x[i&1]
if _, ok := y.(*typeA); ok {
count++
}
}
b.Logf("count is %d", count)
}
func BenchmarkInterfaceTypeAssertInterface(b *testing.B) {
var x [2]interface{}
x[0] = &typeA{}
x[1] = &typeB{}
b.ReportAllocs()
b.ResetTimer()
count := 0
for i := 0; i < b.N; i++ {
y := x[i&1]
if _, ok := y.(benchType); ok {
count++
}
}
b.Logf("count is %d", count)
}
func BenchmarkInterfaceTypeSwitch(b *testing.B) {
var x [2]interface{}
x[0] = &typeA{}
x[1] = &typeB{}
b.ReportAllocs()
b.ResetTimer()
count := 0
for i := 0; i < b.N; i++ {
y := x[i&1]
switch y := y.(type) {
case *typeA:
count += y.get()
case *typeB:
count += y.get()
case benchType:
count += y.get()
}
}
b.Logf("count is %d", count)
}
func BenchmarkInterfaceCall(b *testing.B) {
var x [2]benchType
x[0] = &typeA{}
x[1] = &typeB{}
b.ReportAllocs()
b.ResetTimer()
total := 0
for i := 0; i < b.N; i++ {
total += x[i&1].get()
}
if total > 0 {
b.Logf("total is %d", total)
}
}
func BenchmarkInterfaceCallComparison(b *testing.B) {
x := &typeA{}
b.ReportAllocs()
b.ResetTimer()
total := 0
for i := 0; i < b.N; i++ {
total += x.get()
}
if total > 0 {
b.Logf("total is %d", total)
}
}
func BenchmarkInterfaceCallTypeAssertion(b *testing.B) {
var x [2]benchType
x[0] = &typeA{}
x[1] = &typeB{}
b.ReportAllocs()
b.ResetTimer()
total := 0
for i := 0; i < b.N; i++ {
switch x := x[i&1].(type) {
case *typeA:
total += x.get()
case *typeB:
total += x.get()
}
}
if total > 0 {
b.Logf("total is %d", total)
}
}
func BenchmarkInterfaceStore(b *testing.B) {
var store interface{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
store = 1337
}
_ = store
}
func BenchmarkInterfaceStorePointer(b *testing.B) {
var store interface{}
val := 7
valptr := &val
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
store = valptr
}
_ = store
}