-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmalloc_test.go
More file actions
69 lines (58 loc) · 1.08 KB
/
malloc_test.go
File metadata and controls
69 lines (58 loc) · 1.08 KB
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
// go test -count=1 -v -bench=. -benchmem
package unsafe
import (
"testing"
"unsafe"
)
type Person struct {
name [50]byte
age int
phone int
}
var clrout = [][2]uintptr{
{unsafe.Offsetof(Person{}.name), unsafe.Sizeof(Person{}.name)},
{unsafe.Offsetof(Person{}.phone), unsafe.Sizeof(Person{}.phone)},
}
func (p *Person) UnsafeClearOutFields() [][2]uintptr {
return clrout
return nil
}
var result *Person
const N = 1000
func BenchmarkMallocNew(b *testing.B) {
var r *Person
for n := 0; n < b.N; n++ {
for n := 0; n < N; n++ {
ptr := Malloc(unsafe.Sizeof(Person{}), false)
r = (*Person)(ptr)
}
}
result = r
}
func BenchmarkMallocNewSelectiveZeroing(b *testing.B) {
var r *Person
for n := 0; n < b.N; n++ {
for n := 0; n < N; n++ {
r = New[Person]()
}
}
result = r
}
func BenchmarkStdNew(b *testing.B) {
var r *Person
for n := 0; n < b.N; n++ {
for n := 0; n < N; n++ {
r = new(Person)
}
}
result = r
}
func BenchmarkNewZero(b *testing.B) {
var r *Person
for n := 0; n < b.N; n++ {
for n := 0; n < N; n++ {
r = NewZero[Person]()
}
}
result = r
}