-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-present.slide
231 lines (129 loc) · 5.2 KB
/
go-present.slide
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
Concurrency & Parallelism in Go
* Agenda :
- Brief introduction to Go
- Understand goroutines, waitgroups, mutexes & channels.
- Understanding concurrency and parallelism with the help of programs and visualisation
- High level overview of the Golang scheduler
- Gotchas of writing concurrent/parallel programs
- Success stories
* A Brief Introduction to Golang
* Golang
Go, also known as Golang, is an open-source, compiled, and statically typed programming language designed by Google. It is built to be simple, high-performing, readable, and efficient.
.play codeSamples/example.go
* Basic Go Code Example
Let's try to hit these websites and print the response in console.
.play codeSamples/websiteWithoutRoutines.go /START OMIT/,/END OMIT/
* GoRoutines
* A goroutine is a lightweight thread managed by the Go runtime.
- Memory consumption (very cheap around few kilobytes at inception)
- Dynamically Sized Stacks
- Low Latency (Run multiple Goroutines on a single OS thread)
- Setup and teardown costs
- Managed by go runtime
.image images/gofunc.jpeg 300 800
* Let's run printResponse function as goroutines
.play codeSamples/goroutines.go /START OMIT/,/END OMIT/
* The main function is a go routine
Even more important to know is that once the main function returns, it closes all other go routines that are currently running.
.image images/maingopher.jpeg 300 800
* Main & goroutines
.image images/main.gif 400 800
* Let's wait for go routines to finish.
.play codeSamples/goroutines-sleep.go /START OMIT/,/END OMIT/
* Wait-Groups
* To wait for multiple goroutines to finish, we can use a wait group.
.play codeSamples/waitGroup.go /START OMIT/,/END OMIT/
* Mutexes
* Mutex
A Mutex ensures ownership of a resource to one goroutine at a time
.play codeSamples/mutex.go /START OMIT/,/END OMIT/
.image images/race.png 200 800
* Channels
* Channels can be thought as pipes through which goroutines communicate.
- Can send and receive values with the channel operator <-
- Blocking in nature
- Helps to synchronizing goroutines(signal mechanism)
- Buffered & Unbuffered channels
- Can be used in Parallelization
.image images/channelsgopher.jpeg 300 500
* Channels
Adding channel to get the responses instead of an array.
.code codeSamples/channels.go /START OMIT/,/END OMIT/
.play codeSamples/channels2.go /START OMIT/,/END OMIT/
* Any questions until now?
.image images/questionsgopher.jpeg 400 600
* Understanding Concurrency & Parallelism
* Recap of Concurrency
- Dealing with a lot of things at the same time
- It is more about the structure of your program
- Can use one or more CPU cores
* Example (Concurrency)
.play codeSamples/pingpong.go /START OMIT/,/END OMIT/
* Visualising ping pong match
.image images/conc_latest.gif
* Recap of Parallelism
- Doing a lot of things at the same time
- Can only be achieved in multi core systems (unless hyper-threading is enabled)
- E.g - Coding and listening to music at the same time
* Example (Parallelism)
.play codeSamples/fib.go /START OMIT/,/END OMIT/
* Visualising fibonnaci calculation
.image images/par_lat.gif
* Concurrency with parallelism
- It is possible to run your concurrent code in parallel
- An example would be running multiple pingpong matches at the same time
* So when should we use what?
- There is no silver bullet, it depends on the problem you're solving
- For CPU heavy tasks use parallelism
- For network/IO heavy tasks use concurrency
* Questions until now?
.image images/questionsgopher.jpeg 400 600
* The Go Scheduler
* Responsibility
- Schedules N number of goroutines on M number of OS threads (M:N scheduling)
- Uses cooperative scheduling instead of preemptive scheduling (until Go 1.13)
- Context switching
- Written in Go
* M:P Threading
.image images/m-n.png 500 800
* Go Routines States
.image images/m-n1.png 500 800
* How do OS threads know where to pick up goroutines from?
.image images/gopher-thinking.png 300 500
* How do OS threads know where to pick up goroutines from?
- Local Run Queue (LRQ)
- Global Run Queue (GRQ)
* Where does these Local Run Queues exist?
.image images/gopher-thinking.png 300 500
* The Processor(P) object
- P : represents the processor, which can be seen as a local scheduler running on a thread
- Created when program starts
- Resource required by OS threads to run Go code
- Stores goroutines that are runnable (LRQ)
.image images/p.png 250 200
* M:P:N threading
.image images/MNP.png 550 1000
* Where does the Go Scheduler fit in?
.image images/exe.png
* What does this scheduler bring to the table?
- Better visibility of goroutine scheduling
- Reuse of OS threads
- Minimal context switching at OS level
* Questions until now?
.image images/questionsgopher.jpeg 400 600
* Gotchas
* Gotchas of Concurrent/Parallel programs
- Spawning many goroutines will *not* always give you better performance
- Goroutine leaks
- Increasing GOMAXPROCS value does *not* mean your code will run faster
- Shared memory for local variables can lead to undesired state
- Cooperative scheduling can starve goroutines
* Sucess Stories
* Uber
.image images/uber.png 500 800
* Twitch
.image images/twitch.png 500 1000
* Others
.image images/twitter.png 500 900
* That's all from our side.
.image images/micdrop.png 600 600