summaryrefslogtreecommitdiffstats
path: root/src/runtime/iface_test.go
blob: 06f6eeb95246379a336e39dd2e16856dfff16cf2 (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime_test

import (
	"runtime"
	"testing"
)

type I1 interface {
	Method1()
}

type I2 interface {
	Method1()
	Method2()
}

type TS uint16
type TM uintptr
type TL [2]uintptr

func (TS) Method1() {}
func (TS) Method2() {}
func (TM) Method1() {}
func (TM) Method2() {}
func (TL) Method1() {}
func (TL) Method2() {}

type T8 uint8
type T16 uint16
type T32 uint32
type T64 uint64
type Tstr string
type Tslice []byte

func (T8) Method1()     {}
func (T16) Method1()    {}
func (T32) Method1()    {}
func (T64) Method1()    {}
func (Tstr) Method1()   {}
func (Tslice) Method1() {}

var (
	e  any
	e_ any
	i1 I1
	i2 I2
	ts TS
	tm TM
	tl TL
	ok bool
)

// Issue 9370
func TestCmpIfaceConcreteAlloc(t *testing.T) {
	if runtime.Compiler != "gc" {
		t.Skip("skipping on non-gc compiler")
	}

	n := testing.AllocsPerRun(1, func() {
		_ = e == ts
		_ = i1 == ts
		_ = e == 1
	})

	if n > 0 {
		t.Fatalf("iface cmp allocs=%v; want 0", n)
	}
}

func BenchmarkEqEfaceConcrete(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = e == ts
	}
}

func BenchmarkEqIfaceConcrete(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = i1 == ts
	}
}

func BenchmarkNeEfaceConcrete(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = e != ts
	}
}

func BenchmarkNeIfaceConcrete(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = i1 != ts
	}
}

func BenchmarkConvT2EByteSized(b *testing.B) {
	b.Run("bool", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			e = yes
		}
	})
	b.Run("uint8", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			e = eight8
		}
	})
}

func BenchmarkConvT2ESmall(b *testing.B) {
	for i := 0; i < b.N; i++ {
		e = ts
	}
}

func BenchmarkConvT2EUintptr(b *testing.B) {
	for i := 0; i < b.N; i++ {
		e = tm
	}
}

func BenchmarkConvT2ELarge(b *testing.B) {
	for i := 0; i < b.N; i++ {
		e = tl
	}
}

func BenchmarkConvT2ISmall(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i1 = ts
	}
}

func BenchmarkConvT2IUintptr(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i1 = tm
	}
}

func BenchmarkConvT2ILarge(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i1 = tl
	}
}

func BenchmarkConvI2E(b *testing.B) {
	i2 = tm
	for i := 0; i < b.N; i++ {
		e = i2
	}
}

func BenchmarkConvI2I(b *testing.B) {
	i2 = tm
	for i := 0; i < b.N; i++ {
		i1 = i2
	}
}

func BenchmarkAssertE2T(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		tm = e.(TM)
	}
}

func BenchmarkAssertE2TLarge(b *testing.B) {
	e = tl
	for i := 0; i < b.N; i++ {
		tl = e.(TL)
	}
}

func BenchmarkAssertE2I(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		i1 = e.(I1)
	}
}

func BenchmarkAssertI2T(b *testing.B) {
	i1 = tm
	for i := 0; i < b.N; i++ {
		tm = i1.(TM)
	}
}

func BenchmarkAssertI2I(b *testing.B) {
	i1 = tm
	for i := 0; i < b.N; i++ {
		i2 = i1.(I2)
	}
}

func BenchmarkAssertI2E(b *testing.B) {
	i1 = tm
	for i := 0; i < b.N; i++ {
		e = i1.(any)
	}
}

func BenchmarkAssertE2E(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		e_ = e
	}
}

func BenchmarkAssertE2T2(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		tm, ok = e.(TM)
	}
}

func BenchmarkAssertE2T2Blank(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		_, ok = e.(TM)
	}
}

func BenchmarkAssertI2E2(b *testing.B) {
	i1 = tm
	for i := 0; i < b.N; i++ {
		e, ok = i1.(any)
	}
}

func BenchmarkAssertI2E2Blank(b *testing.B) {
	i1 = tm
	for i := 0; i < b.N; i++ {
		_, ok = i1.(any)
	}
}

func BenchmarkAssertE2E2(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		e_, ok = e.(any)
	}
}

func BenchmarkAssertE2E2Blank(b *testing.B) {
	e = tm
	for i := 0; i < b.N; i++ {
		_, ok = e.(any)
	}
}

func TestNonEscapingConvT2E(t *testing.T) {
	m := make(map[any]bool)
	m[42] = true
	if !m[42] {
		t.Fatalf("42 is not present in the map")
	}
	if m[0] {
		t.Fatalf("0 is present in the map")
	}

	n := testing.AllocsPerRun(1000, func() {
		if m[0] {
			t.Fatalf("0 is present in the map")
		}
	})
	if n != 0 {
		t.Fatalf("want 0 allocs, got %v", n)
	}
}

func TestNonEscapingConvT2I(t *testing.T) {
	m := make(map[I1]bool)
	m[TM(42)] = true
	if !m[TM(42)] {
		t.Fatalf("42 is not present in the map")
	}
	if m[TM(0)] {
		t.Fatalf("0 is present in the map")
	}

	n := testing.AllocsPerRun(1000, func() {
		if m[TM(0)] {
			t.Fatalf("0 is present in the map")
		}
	})
	if n != 0 {
		t.Fatalf("want 0 allocs, got %v", n)
	}
}

func TestZeroConvT2x(t *testing.T) {
	tests := []struct {
		name string
		fn   func()
	}{
		{name: "E8", fn: func() { e = eight8 }},  // any byte-sized value does not allocate
		{name: "E16", fn: func() { e = zero16 }}, // zero values do not allocate
		{name: "E32", fn: func() { e = zero32 }},
		{name: "E64", fn: func() { e = zero64 }},
		{name: "Estr", fn: func() { e = zerostr }},
		{name: "Eslice", fn: func() { e = zeroslice }},
		{name: "Econstflt", fn: func() { e = 99.0 }}, // constants do not allocate
		{name: "Econststr", fn: func() { e = "change" }},
		{name: "I8", fn: func() { i1 = eight8I }},
		{name: "I16", fn: func() { i1 = zero16I }},
		{name: "I32", fn: func() { i1 = zero32I }},
		{name: "I64", fn: func() { i1 = zero64I }},
		{name: "Istr", fn: func() { i1 = zerostrI }},
		{name: "Islice", fn: func() { i1 = zerosliceI }},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			n := testing.AllocsPerRun(1000, test.fn)
			if n != 0 {
				t.Errorf("want zero allocs, got %v", n)
			}
		})
	}
}

var (
	eight8  uint8 = 8
	eight8I T8    = 8
	yes     bool  = true

	zero16     uint16 = 0
	zero16I    T16    = 0
	one16      uint16 = 1
	thousand16 uint16 = 1000

	zero32     uint32 = 0
	zero32I    T32    = 0
	one32      uint32 = 1
	thousand32 uint32 = 1000

	zero64     uint64 = 0
	zero64I    T64    = 0
	one64      uint64 = 1
	thousand64 uint64 = 1000

	zerostr  string = ""
	zerostrI Tstr   = ""
	nzstr    string = "abc"

	zeroslice  []byte = nil
	zerosliceI Tslice = nil
	nzslice    []byte = []byte("abc")

	zerobig [512]byte
	nzbig   [512]byte = [512]byte{511: 1}
)

func BenchmarkConvT2Ezero(b *testing.B) {
	b.Run("zero", func(b *testing.B) {
		b.Run("16", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zero16
			}
		})
		b.Run("32", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zero32
			}
		})
		b.Run("64", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zero64
			}
		})
		b.Run("str", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zerostr
			}
		})
		b.Run("slice", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zeroslice
			}
		})
		b.Run("big", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = zerobig
			}
		})
	})
	b.Run("nonzero", func(b *testing.B) {
		b.Run("str", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = nzstr
			}
		})
		b.Run("slice", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = nzslice
			}
		})
		b.Run("big", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = nzbig
			}
		})
	})
	b.Run("smallint", func(b *testing.B) {
		b.Run("16", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = one16
			}
		})
		b.Run("32", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = one32
			}
		})
		b.Run("64", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = one64
			}
		})
	})
	b.Run("largeint", func(b *testing.B) {
		b.Run("16", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = thousand16
			}
		})
		b.Run("32", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = thousand32
			}
		})
		b.Run("64", func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				e = thousand64
			}
		})
	})
}