Skip to content

Commit 88a180c

Browse files
better changes to language
1 parent 87167fd commit 88a180c

File tree

4 files changed

+59
-44
lines changed

4 files changed

+59
-44
lines changed

topics/go/language/pointers/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ https://godoc.org/golang.org/x/tools/go/ssa
109109

110110
## Code Review
111111

112-
[Pass by Value](example1/example1.go) ([Go Playground](https://play.golang.org/p/ufunQgPsYA))
113-
[Sharing data I](example2/example2.go) ([Go Playground](https://play.golang.org/p/g0Cs0FVpyi))
112+
[Pass by Value](example1/example1.go) ([Go Playground](https://play.golang.org/p/JJMHWiZ9h9))
113+
[Sharing data I](example2/example2.go) ([Go Playground](https://play.golang.org/p/y_FHIdUbAw))
114114
[Sharing data II](example3/example3.go) ([Go Playground](https://play.golang.org/p/KRKrUCcTYe))
115-
[Stack vs Heap](example4/example4.go) ([Go Playground](https://play.golang.org/p/jYkpV-zQ_o))
115+
[Escape Analysis](example4/example4.go) ([Go Playground](https://play.golang.org/p/ObEFYdDVB2))
116116
[Stack grow](example5/example5.go) ([Go Playground](https://play.golang.org/p/tpDOwBCvqW))
117117

118118
## Exercises

topics/go/language/pointers/example1/example1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
// Display the "value of" and "address of" count.
1313
println("count:\tValue Of[", count, "]\tAddr Of[", &count, "]")
1414

15-
// Pass the "value of" the variable count.
15+
// Pass the "value of" the count.
1616
increment(count)
1717

1818
println("count:\tValue Of[", count, "]\tAddr Of[", &count, "]")
@@ -23,7 +23,7 @@ func main() {
2323
//go:noinline
2424
func increment(inc int) {
2525

26-
// Increment the value.
26+
// Increment the "value of" inc.
2727
inc++
2828
println("inc:\tValue Of[", inc, "]\tAddr Of[", &inc, "]")
2929
}

topics/go/language/pointers/example2/example2.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ func main() {
1111
count := 10
1212

1313
// Display the "value of" and "address of" count.
14-
println("count:\tValue Of[",count, "]\t\tAddr Of[", &count, "]")
14+
println("count:\tValue Of[", count, "]\t\tAddr Of[", &count, "]")
1515

16-
// Pass the "address of" the variable count.
16+
// Pass the "address of" count.
1717
increment(&count)
1818

19-
println("count:\tValue Of[",count, "]\t\tAddr Of[", &count, "]")
19+
println("count:\tValue Of[", count, "]\t\tAddr Of[", &count, "]")
2020
}
2121

2222
// increment declares count as a pointer variable whose value is
2323
// always an address and points to values of type int.
2424
//go:noinline
2525
func increment(inc *int) {
2626

27-
// Increment the value that the "pointer points to". (de-referencing)
27+
// Increment the "value of" count that the "pointer points to".
2828
*inc++
29-
println("inc:\tValue Of[",inc, "]\tAddr Of[", &inc, "]\tValue Points To[", *inc, "]")
30-
}
29+
println("inc:\tValue Of[", inc, "]\tAddr Of[", &inc, "]\tValue Points To[", *inc, "]")
30+
}

topics/go/language/pointers/example4/example4.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Escape Analysis Flaws:
55
// https://docs.google.com/document/d/1CxgUBPlx9iJzkz9JWkb6tIpTe5q32QDmz8l0BouG0Cw/view
66

7-
// Sample program to show variables stay on or escape from the stack.
7+
// Sample program to teach the mechanics of escape analysis.
88
package main
99

1010
// user represents a user in the system.
@@ -15,12 +15,13 @@ type user struct {
1515

1616
// main is the entry point for the application.
1717
func main() {
18-
stayOnStack()
19-
escapeToHeap()
18+
createUserV1()
19+
createUserV2()
2020
}
2121

22-
// stayOnStack shows how the variable does not escape.
23-
func stayOnStack() user {
22+
// createUserV1 creates a user value and passed
23+
// a copy back to the caller.
24+
func createUserV1() user {
2425
u := user{
2526
name: "Bill",
2627
@@ -29,8 +30,9 @@ func stayOnStack() user {
2930
return u
3031
}
3132

32-
// escapeToHeap shows how the variable does escape.
33-
func escapeToHeap() *user {
33+
// createUserV2 creates a user value and shares
34+
// the value with the caller.
35+
func createUserV2() *user {
3436
u := user{
3537
name: "Bill",
3638
@@ -44,49 +46,62 @@ func escapeToHeap() *user {
4446
4547
go build -gcflags "-m -m"
4648
47-
./example4.go:23: can inline stayOnStack as: func() user { u := user literal; return u }
48-
./example4.go:33: can inline escapeToHeap as: func() *user { u := user literal; return &u }
49-
./example4.go:17: can inline main as: func() { stayOnStack(); escapeToHeap() }
50-
./example4.go:18: inlining call to stayOnStack func() user { u := user literal; return u }
51-
./example4.go:19: inlining call to escapeToHeap func() *user { u := user literal; return &u }
49+
./example4.go:24: can inline createUserV1 as: func() user { u := user literal; return u }
50+
./example4.go:35: can inline createUserV2 as: func() *user { u := user literal; return &u }
51+
./example4.go:17: can inline main as: func() { createUserV1(); createUserV2() }
52+
./example4.go:18: inlining call to createUserV1 func() user { u := user literal; return u }
53+
./example4.go:19: inlining call to createUserV2 func() *user { u := user literal; return &u }
5254
./example4.go:19: main &u does not escape
53-
./example4.go:39: &u escapes to heap
54-
./example4.go:39: from ~r0 (return) at ./example4.go:39
55-
./example4.go:37: moved to heap: u
55+
./example4.go:41: &u escapes to heap
56+
./example4.go:41: from ~r0 (return) at ./example4.go:41
57+
./example4.go:39: moved to heap: u
5658
5759
// See the intermediate assembly phase before
5860
// generating the actual arch-specific assembly.
5961
6062
go build -gcflags -S
6163
62-
0x000f 00015 (pointers/example4/example4.go:18) MOVQ $4, DX
63-
0x0016 00022 (pointers/example4/example4.go:18) LEAQ go.string."[email protected]"(SB), CX
64-
0x001d 00029 (pointers/example4/example4.go:18) MOVQ $18, AX
65-
0x0024 00036 (pointers/example4/example4.go:18) NOP
66-
0x0024 00036 (pointers/example4/example4.go:19) MOVQ $0, AX
67-
64+
"".createUserV1 t=1 size=43 args=0x20 locals=0x0
65+
0x0000 00000 example4.go:24 TEXT "".createUserV1(SB), $0-32
66+
0x0000 00000 example4.go:24 FUNCDATA $0, gclocals·ff19ed39bdde8a01a800918ac3ef0ec7(SB)
67+
0x0000 00000 example4.go:24 FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
68+
0x0000 00000 example4.go:18 LEAQ go.string."Bill"(SB), AX
69+
0x0007 00007 example4.go:30 MOVQ AX, "".~r0+8(FP)
70+
0x000c 00012 example4.go:30 MOVQ $4, "".~r0+16(FP)
71+
0x0015 00021 example4.go:18 LEAQ go.string."[email protected]"(SB), AX
72+
0x001c 00028 example4.go:30 MOVQ AX, "".~r0+24(FP)
73+
0x0021 00033 example4.go:30 MOVQ $18, "".~r0+32(FP)
74+
0x002a 00042 example4.go:30 RET
6875
6976
// See the actual machine representation by using
7077
// the disasembler.
7178
7279
go tool objdump -s main.main example4
7380
74-
example4.go:17 0x2040 4883ec28 SUBQ $0x28, SP
75-
example4.go:17 0x2044 48896c2420 MOVQ BP, 0x20(SP)
76-
example4.go:17 0x2049 488d6c2420 LEAQ 0x20(SP), BP
77-
example4.go:19 0x204e 48c7042400000000 MOVQ $0x0, 0(SP)
78-
example4.go:19 0x2056 48c744240800000000 MOVQ $0x0, 0x8(SP)
81+
TEXT main.main(SB) example4.go
82+
example4.go:17 0x104bf10 4883ec28 SUBQ $0x28, SP
83+
example4.go:17 0x104bf14 48896c2420 MOVQ BP, 0x20(SP)
84+
example4.go:17 0x104bf19 488d6c2420 LEAQ 0x20(SP), BP
85+
example4.go:19 0x104bf1e 48c7042400000000 MOVQ $0x0, 0(SP)
86+
example4.go:19 0x104bf26 48c744240800000000 MOVQ $0x0, 0x8(SP)
87+
example4.go:19 0x104bf2f 48c744241800000000 MOVQ $0x0, 0x18(SP)
88+
example4.go:19 0x104bf38 488d05c9a30100 LEAQ 0x1a3c9(IP), AX
89+
example4.go:19 0x104bf3f 48890424 MOVQ AX, 0(SP)
90+
example4.go:19 0x104bf43 48c744240804000000 MOVQ $0x4, 0x8(SP)
91+
example4.go:19 0x104bf4c 488d05acb30100 LEAQ 0x1b3ac(IP), AX
92+
example4.go:19 0x104bf53 4889442410 MOVQ AX, 0x10(SP)
93+
example4.go:19 0x104bf58 48c744241812000000 MOVQ $0x12, 0x18(SP)
94+
example4.go:20 0x104bf61 488b6c2420 MOVQ 0x20(SP), BP
95+
example4.go:20 0x104bf66 4883c428 ADDQ $0x28, SP
96+
example4.go:20 0x104bf6a c3 RET
7997
8098
// See a list of the symbols in an artifact with
8199
// annotations and size.
82100
83101
go tool nm example4
84102
85-
6cea0 R $f64.bfe62e42fefa39ef
86-
96d20 B __cgo_init
87-
96d28 B __cgo_notify_runtime_init_done
88-
96d30 B __cgo_thread_start
89-
4c8a0 T __rt0_amd64_darwin
90-
4af50 T _gosave
91-
4c8c0 T _main
103+
104bf70 T main.init
104+
10b2940 B main.initdone.
105+
104bf10 T main.main
106+
10983d0 B os.executablePath
92107
*/

0 commit comments

Comments
 (0)