Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Профилирование и оптимизация программ на Go
Search
Alexey Palazhchenko
April 14, 2017
Programming
3
460
Профилирование и оптимизация программ на Go
Доклад на конференции Стачка 2017
Alexey Palazhchenko
April 14, 2017
Tweet
Share
More Decks by Alexey Palazhchenko
See All by Alexey Palazhchenko
Using PostgreSQL's Background Worker Processes For Fun and Profit
aleksi
0
64
Песнь Хорьков и Гоферов
aleksi
0
330
Fuzzy generics
aleksi
0
130
On Ferrets and Gophers
aleksi
0
230
How to Go Wrong with Concurrency
aleksi
2
750
Adding context to existing code
aleksi
1
120
Зачем и как написать свой database/sql драйвер
aleksi
1
150
Cooking gRPC
aleksi
1
830
Profiling and Optimizing Go Programs
aleksi
1
1.7k
Other Decks in Programming
See All in Programming
The Implementations of Advanced LR Parser Algorithm
junk0612
1
300
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
650
Memory API : Patterns, Performance et Cas d'Utilisation
josepaumard
0
140
Agentic Applications with Symfony
el_stoffel
2
300
リストビュー画面UX改善の振り返り
splcywolf
0
140
Sharing features among Android applications: experience feedback
jbvincey
0
110
Contribute to Comunities | React Tokyo Meetup #4 LT
sasagar
0
130
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
0
820
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
3
3.7k
ComposeでWebアプリを作る技術
tbsten
0
110
海外のアプリで見かけたかっこいいTransitionを真似てみる
shogotakasaki
1
170
Making TCPSocket.new "Happy"!
coe401_
1
1.2k
Featured
See All Featured
Designing for Performance
lara
608
69k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
5
550
Adopting Sorbet at Scale
ufuk
76
9.3k
Designing for humans not robots
tammielis
252
25k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
23
2.6k
For a Future-Friendly Web
brad_frost
176
9.7k
Optimizing for Happiness
mojombo
377
70k
A Modern Web Designer's Workflow
chriscoyier
693
190k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Become a Pro
speakerdeck
PRO
27
5.3k
Transcript
Профилирование и оптимизация программ на Go Алексей Палажченко 14 апреля
2017, Стачка
Профилирование и оптимизация программ на Go Алексей Палажченко 14 апреля
2017, Стачка
None
None
None
None
None
None
type Cache interface { Get(id string) interface{} Set(id string, value
interface{}) Len() int }
func (s *Slice) Get(id string) interface{} { for _, it
:= range s.items { if it.id == id { return it.value } } return nil }
func (s *Slice) Set(id string, value i{}) { for i,
it := range s.items { if it.id == id { s.items[i].value = value return } } s.items = append(s.items, item{id, value}) }
func (s *Slice) Set(id string, value i{}) { for i,
it := range s.items { if it.id == id { s.items[i].value = value return } } s.items = append(s.items, item{id, value}) }
b.ResetTimer() for i := 0; i < b.N; i++ {
for _, id := range ids { Sink = c.Get(id) } }
100 200 300 400 0 1 2 3 4 5
6 7 8 9 10 Slice Map
100 200 300 400 0 1 2 3 4 5
6 7 8 9 10 Slice Map Fancy algorithms are slow when n is small, and n is usually small. - Rob Pike
Sink func popcnt(x uint64) int { var res uint64 for
; x > 0; x >>= 1 { res += x & 1 } return int(res) }
Sink const m1 = 0x5555555555555555 const m2 = 0x3333333333333333 const
m4 = 0x0f0f0f0f0f0f0f0f const h01 = 0x0101010101010101 func popcnt2(x uint64) int { x -= (x >> 1) & m1 x = (x & m2) + ((x >> 2) & m2) x = (x + (x >> 4)) & m4 return int((x * h01) >> 56) }
Sink func BenchmarkPopcnt(b *testing.B) { for i := 0; i
< b.N; i++ { popcnt(uint64(i)) } } func BenchmarkPopcnt2(b *testing.B) { for i := 0; i < b.N; i++ { popcnt2(uint64(i)) } }
Sink go test -v -bench=. BenchmarkPopcnt-4 100000000 15.5 ns/op BenchmarkPopcnt2-4
2000000000 0.34 ns/op
Sink go test -v -bench=. BenchmarkPopcnt-4 100000000 15.5 ns/op BenchmarkPopcnt2-4
2000000000 0.34 ns/op
Sink • go doc compile • go test -bench=. -gcflags
"-S"
Sink popcnt_test.go:14 MOVQ "".b+8(FP), AX popcnt_test.go:14 MOVQ $0, CX popcnt_test.go:14
MOVQ 200(AX), DX popcnt_test.go:14 CMPQ CX, DX popcnt_test.go:14 JGE $0, 34 popcnt_test.go:14 INCQ CX popcnt_test.go:14 MOVQ 200(AX), DX popcnt_test.go:14 CMPQ CX, DX popcnt_test.go:14 JLT $0, 19 popcnt_test.go:17 RET
Sink func BenchmarkPopcnt(b *testing.B) { for i := 0; i
< b.N; i++ { Sink = popcnt(uint64(i)) } } func BenchmarkPopcnt2(b *testing.B) { for i := 0; i < b.N; i++ { Sink = popcnt2(uint64(i)) } }
Sink go test -v -bench=. BenchmarkPopcnt-4 50000000 39.3 ns/op BenchmarkPopcnt2-4
50000000 26.8 ns/op
Sink env GOSSAFUNC=BenchmarkPopcnt2 go test -bench=.
Benchmarks • Не в виртуальной машине • Не трогать во
время работы • Выключить автоматическое управление питанием • rsc.io/benchstat
pprof • runtime/pprof • net/http/pprof • go test • Не
больше одного за раз!
pprof: CPU • setitimer(2), ITIMER_PROF, SIGPROF • До 500 Гц
(100 по умолчанию) • SetCPUProfileRate(hz) • go test -bench=XXX -cpuprofile=XXX.pprof • go tool pprof -svg -output=XXX.svg cache.test XXX.pprof
None
None
pprof: mem/block/mutex • pprof.MemProfileRate = bytes • pprof.SetBlockProfileRate(ns) • pprof.SetMutexProfileFraction(rate)
type Map struct { m sync.Mutex items map[string]interface{} } func
(m *Map) Get(id string) interface{} { m.m.Lock() defer m.m.Unlock() return m.items[id] }
pprof: block • go test -v -bench=XXX -blockprofile=XXX.pprof • go
tool pprof -svg -lines -output=XXX.svg ccache.test XXX.pprof
None
type Map struct { m sync.RWMutex items map[string]interface{} } func
(m *Map) Get(id string) interface{} { m.m.RLock() v := m.items[id] m.m.RUnlock() return v }
pprof: свои профили • Когда нужны stack traces • Интеграция
с go tool pprof • Пример: открытие и закрытие файлов • pprof.NewProfile, pprof.Lookup • Profile.Add, Remove
Execution tracer • Запуск, остановка, переключение горутин • Блокировки на
каналах, select • Блокировки на mutex’ах • Блокировки на сети, syscall’ах
Execution tracer • Все события с полным контекстом • Большие
файлы (со всеми символами) • Замедление ~25% • pprof CPU для throughput, tracer для latency
Execution tracer • import _ "net/http/pprof" • http://127.0.0.1:8080/debug/pprof • redis-benchmark
-r 100000 -e -l -t set,get • go tool trace trace.out
Execution tracer • go tool trace -pprof=TYPE trace.out > TYPE.pprof
• net • sync • syscall • sched
Linux • perf (perf_events) • SystemTap • BPF (eBPF)
Переменные окружения • GOGC (100, off) • GODEBUG • gctrace=1
• allocfreetrace=1 • schedtrace=1000
Оптимизации • struct{} • m[string(b)] • for i, c :=
range []byte(s) • for i := range s { a[i] = <zero value> }
Оптимизации type Key [64]byte type Value struct { Name [32]byte
Balance uint64 Timestamp int64 } m := make(map[Key]Value, 1e8)
• golang-ru @ Google Groups • 4gophers.ru/slack • golangshow.com •
github.com/AlekSi/ stachka-2017