β

[转]编写高性能的Go代码的最佳实践

鸟窝 101 阅读
Go

原文: go-perfbook/performance

This document outlines best practices for writing high-performance Go code.

At the moment, it's a collection of links to videos, slides, and blog posts
("awesome-go-performance"), but I would like this to evolve into a longer book
format where the content is here instead of external. The links should be sorted into categories.

All the content will be licensed under CC-BY-SA.

Optimization Workflow

The first step is important. It tells you when and where to start optimizing.
More importantly, it also tells you when to stop. Pretty much all
optimizations add code complexity in exchange for speed. And you can always
make code faster. It's a balancing act.

The basic rules of the game are:

  1. minimize CPU usage
    • do less work
    • this generally means "a faster algorithm"
    • but CPU caches and the hidden constants in O() can play tricks on you
  2. minimize allocations (which leads to less CPU stolen by the GC)
  3. make your data quick to access

This book is split into different sections:
1) basic tips for writing not-slow software

 * CS 101-level stuff

2) tips for writing fast software

 * Go-specific sections on how to get the best from Go

3) advanced tips for writing really fast software

 * For when your optimized code isn't fast enough

Basics

  1. choose the best algorithm
  2. pre-compute things you need
  3. add a cache -> reduces work

Introductory Profiling

Techniques applicable to source code in general

  1. introduction to pprof
  2. Writing and running (micro)benchmarks
    • -cpuprofile / -memprofile / -benchmem
  3. How to read it pprof output
  4. What are the different pieces of the runtime that show up
  5. Macro-benchmarks (Profiling in production)
    • net/http/pprof

Tracer

Advanced Techniques

Heap Allocations

Runtime

Common gotchas with the standard library

Unsafe

cgo

Assembly

Alternate implementations

Tooling

Look at some more interesting/advanced tooling

原文: go-perfbook/performance

This document outlines best practices for writing high-performance Go code.

At the moment, it's a collection of links to videos, slides, and blog posts
("awesome-go-performance"), but I would like this to evolve into a longer book
format where the content is here instead of external. The links should be sorted into categories.

All the content will be licensed under CC-BY-SA.

Go
作者:鸟窝
大道至简 衍化至繁
原文地址:[转]编写高性能的Go代码的最佳实践, 感谢原作者分享。

发表评论