-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
54 lines (43 loc) · 1.06 KB
/
slice.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
package main
// go build -gcflags '-m -l'
// -gcflags to view the results of the escape analysis mentioned above in output from the compiler.
// -m to print optimization decisions
// -l to omit inlining decisions
const numberOfItems = 20_000_000
type bigStruct struct {
A, B, C int
D, E, F string
G, H, I bool
}
// moved to heap: element
// make([]*bigStruct, 0, numberOfItems) escapes to heap
//
//go:noinline
func fillSliceWithAppend() []*bigStruct {
data := make([]*bigStruct, 0, numberOfItems)
element := bigStruct{
A: 123, B: 456, C: 789,
D: "ABC", E: "DEF", F: "HIJ",
G: true, H: true, I: true,
}
for i := 0; i < numberOfItems; i++ {
data = append(data, &element)
}
return data
}
// moved to heap: element
// make([]*bigStruct, numberOfItems) escapes to heap
//
//go:noinline
func fillSliceWithIndex() []*bigStruct {
data := make([]*bigStruct, numberOfItems)
element := bigStruct{
A: 123, B: 456, C: 789,
D: "ABC", E: "DEF", F: "HIJ",
G: true, H: true, I: true,
}
for i := 0; i < numberOfItems; i++ {
data[i] = &element
}
return data
}