|
1 | 1 | # Tips and Tricks
|
2 | 2 |
|
3 |
| -This final section contains a number of tips to optimise Go code. |
| 3 | +This section contains a number of tips to optimise Go code. |
4 | 4 |
|
5 | 5 | ## Reduce allocations
|
6 | 6 |
|
@@ -141,6 +141,10 @@ In your design, some goroutines may run until the program exits. These goroutine
|
141 | 141 |
|
142 | 142 | **Never start a goroutine without knowing how it will stop**
|
143 | 143 |
|
| 144 | +A good way to achieve this is to use something like [run.Group][4], [workgroup.Group][5], or similar. |
| 145 | + |
| 146 | +Peter Bourgon has a great presentation on the design behing run.Group from GopherCon EU |
| 147 | + |
144 | 148 | ### Further reading
|
145 | 149 |
|
146 | 150 | - [Concurrency Made Easy][0] (video)
|
@@ -206,13 +210,13 @@ defer func() {
|
206 | 210 | mu.Unlock()
|
207 | 211 | }()
|
208 | 212 | ```
|
209 |
| -`defer` is expensive if the work being done is small, the classic example is `defer` ing a mutex unlock around a struct variable or map lookup. You may choose to avoid `defer` in those situations. |
| 213 | +`defer` can be expensive if the work being done is small, the classic example is `defer` ing a mutex unlock around a struct variable or map lookup. You may choose to avoid `defer` in those situations. |
210 | 214 |
|
211 | 215 | This is a case where readability and maintenance is sacrificed for a performance win.
|
212 | 216 |
|
213 | 217 | #### Always revisit these decisions.
|
214 | 218 |
|
215 |
| -.link https://github.com/golang/go/issues/9704#issuecomment-251003577 |
| 219 | +However, in revising this presentation, I have not been able to write a program that demonstrates a measurable cost from using `defer` -- the compiler is getting very good at eliminating the cost of using defer. |
216 | 220 |
|
217 | 221 | ## Avoid Finalisers
|
218 | 222 |
|
@@ -260,17 +264,17 @@ Old versions of Go will never get better. They will never get bug fixes or optim
|
260 | 264 | - Go 1.5 and 1.6 had a slower compiler, but it produces faster code, and has a faster GC.
|
261 | 265 | - Go 1.7 delivered roughly a 30% improvement in compilation speed over 1.6, a 2x improvement in linking speed (better than any previous version of Go).
|
262 | 266 | - Go 1.8 will deliver a smaller improvement in compilation speed (at this point), but a significant improvement in code quality for non Intel architectures.
|
| 267 | +- Go 1.9, 1.10, 1.11 continue to drive down the GC pause time and improve the quality of generated code. |
263 | 268 |
|
264 | 269 | Old version of Go receive no updates. Do not use them. Use the latest and you will get the best performance.
|
265 | 270 |
|
266 |
| -.link http://dave.cheney.net/2016/04/02/go-1-7-toolchain-improvements Go 1.7 toolchain improvements |
267 |
| -.link http://dave.cheney.net/2016/09/18/go-1-8-performance-improvements-one-month-in Go 1.8 performance improvements |
268 |
| - |
269 | 271 | ## Discussion
|
270 | 272 |
|
271 | 273 | Any questions?
|
272 | 274 |
|
273 | 275 | [0]: https://www.youtube.com/watch?v=yKQOunhhf4A&index=16&list=PLq2Nv-Sh8EbZEjZdPLaQt1qh_ohZFMDj8
|
274 | 276 | [1]: https://dave.cheney.net/paste/concurrency-made-easy.pdf
|
275 | 277 | [2]: https://golang.org/pkg/bytes
|
276 |
| -[3]: https://golang.org/pkg/strings |
| 278 | +[3]: https://golang.org/pkg/strings |
| 279 | +[4]: https://github.com/oklog/run |
| 280 | +[5]: https://github.com/heptio/workgroup |
0 commit comments