Slide 1

Slide 1 text

go-perftuner WARSAW, APR 25 2019 Oleg Kovalov Allegro Twitter: oleg_kovalov Github: cristaloleg

Slide 2

Slide 2 text

Me

Slide 3

Slide 3 text

What is a performance optimization?

Slide 4

Slide 4 text

What is a performance optimization?

Slide 5

Slide 5 text

What is a performance optimization?

Slide 6

Slide 6 text

What is a performance optimization?

Slide 7

Slide 7 text

What is a performance optimization?

Slide 8

Slide 8 text

What is a performance optimization?

Slide 9

Slide 9 text

When performance optimization is needed?

Slide 10

Slide 10 text

When performance optimization is needed?

Slide 11

Slide 11 text

When performance optimization is needed?

Slide 12

Slide 12 text

When performance optimization is needed?

Slide 13

Slide 13 text

When performance optimization is needed?

Slide 14

Slide 14 text

When performance optimization is needed?

Slide 15

Slide 15 text

When performance optimization is needed?

Slide 16

Slide 16 text

When performance optimization is needed?

Slide 17

Slide 17 text

When performance optimization isn’t needed?

Slide 18

Slide 18 text

When performance optimization isn’t needed?

Slide 19

Slide 19 text

When performance optimization isn’t needed?

Slide 20

Slide 20 text

When performance optimization isn’t needed?

Slide 21

Slide 21 text

When performance optimization isn’t needed?

Slide 22

Slide 22 text

When performance optimization isn’t needed?

Slide 23

Slide 23 text

When performance optimization isn’t needed?

Slide 24

Slide 24 text

Types of performance tuning

Slide 25

Slide 25 text

Types of performance tuning

Slide 26

Slide 26 text

Types of performance tuning

Slide 27

Slide 27 text

Types of performance tuning

Slide 28

Slide 28 text

Types of performance tuning

Slide 29

Slide 29 text

Types of performance tuning

Slide 30

Slide 30 text

Types of performance tuning

Slide 31

Slide 31 text

Types of performance tuning

Slide 32

Slide 32 text

Types of performance tuning

Slide 33

Slide 33 text

What Go compiler can do?

Slide 34

Slide 34 text

What Go compiler can do?

Slide 35

Slide 35 text

What Go compiler can do?

Slide 36

Slide 36 text

What Go compiler can do?

Slide 37

Slide 37 text

So, welcome go-perftuner

Slide 38

Slide 38 text

So, welcome go-perftuner

Slide 39

Slide 39 text

So, welcome go-perftuner

Slide 40

Slide 40 text

So, welcome go-perftuner

Slide 41

Slide 41 text

So, welcome go-perftuner

Slide 42

Slide 42 text

So, welcome go-perftuner

Slide 43

Slide 43 text

Code inlining

Slide 44

Slide 44 text

Code inlining

Slide 45

Slide 45 text

Code inlining

Slide 46

Slide 46 text

Code inlining

Slide 47

Slide 47 text

Code inlining

Slide 48

Slide 48 text

func ReadRuneSimple(s string) (ch rune, size int) { if len(s) == 0 { return } ch, size = utf8.DecodeRuneInString(s) return } almostInlined example (1)

Slide 49

Slide 49 text

func ReadRuneSimple(s string) (ch rune, size int) { if len(s) == 0 { return } ch, size = utf8.DecodeRuneInString(s) return } $ go-perftuner inl -threshold=100 old.go almostInlined example (1)

Slide 50

Slide 50 text

func ReadRuneSimple(s string) (ch rune, size int) { if len(s) == 0 { return } ch, size = utf8.DecodeRuneInString(s) return } $ go-perftuner inl -threshold=100 old.go $ almostInlined example (1)

Slide 51

Slide 51 text

func ReadRuneLogged(s string) (ch rune, size int) { if len(s) == 0 { return } log.Printf("we're working") ch, size = utf8.DecodeRuneInString(s) return } $ go-perftuner inl -threshold=100 new.go almostInlined example (2)

Slide 52

Slide 52 text

func ReadRuneLogged(s string) (ch rune, size int) { if len(s) == 0 { return } log.Printf("we're working") ch, size = utf8.DecodeRuneInString(s) return } $ go-perftuner inl -threshold=100 new.go inl: ./new.go:34:6: ReadRuneLogged: budget exceeded by 50 almostInlined example (2)

Slide 53

Slide 53 text

Bounds-checking elimination

Slide 54

Slide 54 text

Bounds-checking elimination

Slide 55

Slide 55 text

Bounds-checking elimination

Slide 56

Slide 56 text

Bounds-checking elimination

Slide 57

Slide 57 text

boundChecks example (1) func PutUint32(b []byte, v uint32) { b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) }

Slide 58

Slide 58 text

boundChecks example (2) func PutUint32(b []byte, v uint32) { b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) } $ go-perftuner bce old.go bce: ./old.go:4:7: slice/array has bound checks bce: ./old.go:5:7: slice/array has bound checks bce: ./old.go:6:7: slice/array has bound checks bce: ./old.go:7:7: slice/array has bound checks $

Slide 59

Slide 59 text

func PutUint32(b []byte, v uint32) { _ = b[3] // early check to guarantee safety b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) } boundChecks example (3)

Slide 60

Slide 60 text

boundChecks example (4) func PutUint32(b []byte, v uint32) { _ = b[3] // early check to guarantee safety b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) } $ go-perftuner bce new.go bce: ./new.go:4:7: slice/array has bound checks $

Slide 61

Slide 61 text

boundChecks example (4) func PutUint32(b []byte, v uint32) { if len(b) < 4 { return } b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) b[3] = byte(v >> 24) } $ go-perftuner bce new.go $

Slide 62

Slide 62 text

Escape analysis

Slide 63

Slide 63 text

Escape analysis

Slide 64

Slide 64 text

Escape analysis

Slide 65

Slide 65 text

Escape analysis

Slide 66

Slide 66 text

func getRands(size int) []int { nums := make([]int, size) for i := range nums { nums[i] = rand.Int() } return nums } escapedVariable example (1)

Slide 67

Slide 67 text

func getRands(size int) []int { nums := make([]int, size) for i := range nums { nums[i] = rand.Int() } return nums } $ go-perftuner esc old.go esc: ./old.go:16:14: make([]int, size) $ escapedVariable example (1)

Slide 68

Slide 68 text

func sumRand() (total int) { nums := make([]int, 8191) for i := range nums { nums[i] = rand.Int() } for _, x := range nums { total += x } return total } escapedVariable example (2)

Slide 69

Slide 69 text

func sumRand() (total int) { nums := make([]int, 8191) for i := range nums { nums[i] = rand.Int() } for _, x := range nums { total += x } return total } $ go-perftuner esc 1.go $ escapedVariable example (3)

Slide 70

Slide 70 text

func sumRand() (total int) { nums := make([]int, 8191 + 1) for i := range nums { nums[i] = rand.Int() } for _, x := range nums { total += x } return total } $ go-perftuner esc 1.go escapedVariable example (4)

Slide 71

Slide 71 text

func sumRand() (total int) { nums := make([]int, 8191 + 1) for i := range nums { nums[i] = rand.Int() } for _, x := range nums { total += x } return total } $ go-perftuner esc 1.go esc: ./old.go:16:14: make([]int, 8191 + 1) $ escapedVariable example (4)

Slide 72

Slide 72 text

A very handy tool

Slide 73

Slide 73 text

A very handy tool

Slide 74

Slide 74 text

A very handy tool

Slide 75

Slide 75 text

A very handy tool

Slide 76

Slide 76 text

$ go get golang.org/x/perf/cmd/benchstat $ go test -bench=. -count 10 > old.txt benchstat

Slide 77

Slide 77 text

$ go get golang.org/x/perf/cmd/benchstat $ go test -bench=. -count 10 > old.txt $ # < do some coding and magic with go-perftuner > $ go test -bench=. -count 10 > new.txt benchstat

Slide 78

Slide 78 text

benchstat $ go get golang.org/x/perf/cmd/benchstat $ go test -bench=. -count 10 > old.txt $ # < do some coding and magic with go-perftuner > $ go test -bench=. -count 10 > new.txt $ benchstat old.txt new.txt name old time/op new time/op delta Foo 13.6ms ± 1% 11.8ms ± 1% -13.31% (p=0.016 n=4+5) Bar 32.1ms ± 1% 31.8ms ± 1% ~ (p=0.286 n=4+5)

Slide 79

Slide 79 text

Performance tuning summary

Slide 80

Slide 80 text

Performance tuning summary

Slide 81

Slide 81 text

Performance tuning summary

Slide 82

Slide 82 text

Performance tuning summary

Slide 83

Slide 83 text

Performance tuning summary

Slide 84

Slide 84 text

Performance tuning summary

Slide 85

Slide 85 text

That’s all folks