pointers vs values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf ·...

33
Pointers vs Values: digging into the performance war Mario Macías Lloret Senior software engineer at New Relic Barcelona Golang Meetup January 2020 twitter.com/MaciasUPC github.com/mariomac

Upload: others

Post on 21-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Pointers vs Values: digging into the performance war

Mario Macías LloretSenior software engineer at New Relic

Barcelona Golang MeetupJanuary 2020

twitter.com/MaciasUPC github.com/mariomac

Page 2: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Who am I?

2003 2005 2010 20202015

Page 3: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

The Core Agents and Open Standards TeamJF

Mario

CarlosDavid

Juan

Antonio

CristianFrank

Page 4: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Table of contents

Refreshing: values vs pointers

Digging more: μBenchmarks

A more realistic use case

Let New Relic measure it!

Conclusions

Page 5: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Using values vs using pointers

● Values○ Safe against nil○ Cleaner

■ no need to check for nil■ no pointer operators *, &

● Pointers○ Allow passing arguments by reference○ Allow sharing a common state between different instances

Page 6: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

The super-optimizer opinion

AWAYS USE POINTERS

IS FASTER THAN MOVING COMPLETE STRUCTS

Page 7: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

-- Donald Knuth

Page 8: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Heap

January 2019 BCN Golang Meetup● T(Heap allocation) >> T(Stack Allocation)● Golang applies “Escape Analysis” techniques to infer where an object is

allocated● Abuse of pointers escape values to the heap

r: Objo: *Obj

r: Obj

Stack???

b() ctx {a() ctx {

Page 9: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Heap

January 2019 BCN Golang Meetup● T(Heap allocation) >> T(Stack Allocation)● Golang applies “Escape Analysis” techniques to infer where an object is

allocated● Abuse of pointers escape values to the heap

r: Objo: *Obj

r: Obj

Stack???

b() ctx {a() ctx {

Page 10: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Table of contents

Refreshing: values vs pointers

Digging more: μBenchmarks

A more realistic use case

Let New Relic measure it!

Conclusions

Page 11: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Digging more: μBenchmarks● Small, localized benchmarks to test a single system functionality.● Not really meaningful from a wider application point of view

Page 12: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

μBenchmarks: initial results

Page 13: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

μBenchmarks: pre-allocated arrays

Page 14: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Adding a value to an arrayarr := make([]Foo, 0, 10)

A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0

B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0

C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

arr - length: 0 - capacity: 10

Page 15: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Adding a value to an arrayarr := make([]Foo, 0, 10)arr = append(arr, Foo{A: 1, B: 2, C: 3})

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

arr - length: 1 - capacity: 10

A: 1 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0 A: 0

B: 2 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0 B: 0

C: 3 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0 C: 0

copy

Page 16: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Adding a reference to an arrayarr := make([]*Foo, 0, 10)

⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

arr - length: 0 - capacity: 10

Page 17: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Adding a reference to an arrayarr := make([]*Foo, 0, 10)arr = append(arr, &Foo{A: 1, B: 2, C: 3})

⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚ ⏚[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

arr - length: 1 - capacity: 10

A: 1

B: 2

C: 3

Heap allocation

Page 18: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

μBenchmark: array iteration (no allocations)

Page 19: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Enforcing cache memory contiguity

A B C

[0]

A B C

[1]

A B C

[2]

A B C

[3]

A B C

[4]

...

[]Foo{ … }

Cache line (64 bytes)

[0] [1] [2] [3] [4] [6] [7] [8] [9]

A B C

A B C

A B C

A B C

A B C

A B C

A B C

A B C

A B C

Increased cache misses

Page 20: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

μBenchmark: structs >64 bytes

Page 21: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Local μoptimization: minimize local var copy

Page 22: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Table of contents

Refreshing: values vs pointers

Digging more: μBenchmarks

A more realistic use case

Let New Relic measure it!

Conclusions

Page 23: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Real world bench: dimensional metrics translator

New relic Flat sample

- event_type: SystemSample- operatingSystem: Linux- agentVersion: 1.8.82- cpuPercent: 30- diskFreePercent: 85- hostname: ip-AC1F0D60- instanceType: t2.small- memoryUsedBytes:

1109519701- etc….

Dimensional metrics sample

Common:- event_type: SystemSample- operatingSystem: Linux- agentVersion: 1.8.82- hostname: ip-AC1F0D60- instanceType: t2.small

Metrics:name: cpuPercentvalue: 30

name: diskFreePercentvalue: 85

name: memoryUsedBytesvalue: 1109519701

Page 24: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

json.Unmarshal(..., &map)

Dimensional metrics translator{ "event_type": "SystemSample", "operatingSystem": "Linux", "agentVersion": "1.8.82", "cpuPercent": 30, "diskFreePercent": 85, "hostname": "ip-AC1F0D60", "instanceType": "t2.small", "memoryUsedBytes": 1109519701}

{ "Common": { "event_type": "SystemSample", "operatingSystem": "Linux", "agentVersion": "1.8.82", "hostname": "ip-AC1F0D60", "instanceType": "t2.small" }, Metrics: [{ "name": "cpuPercent", "type": "Gauge", "value": 30 }, { "name": "diskFreePercent", "type": "Gauge", "value": 85 }, { "name": "memoryUsedBytes", "type": "Gauge", "value": 1109519701 }]}

dim.FromFlat(map)

json.Marshall(...)

submitter.Submit(...)

Page 25: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Benchmark: same code, 2 versions

Page 26: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Benchmark Results

~3%slower

~12%moreallocations

Page 27: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Benchmark Results (100 parallel goroutines)

~3%slower

~16%moreallocations

Page 28: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Table of contents

Refreshing: values vs pointers

Digging more: μBenchmarks

A more realistic use case

Let New Relic measure it!

Conclusions

Page 29: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Running the example in Kubernetes. New Relic Metrics

Page 30: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Used by Kubernetes OOM killer

Running the example in Kubernetes. New Relic Metrics

With thousands of containers, this

+35% might do the difference

Page 31: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Table of contents

Refreshing: values vs pointers

Digging more: μBenchmarks

A more realistic use case

Let New Relic measure it!

Conclusions

Page 32: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Conclusions

● First, aim for clean and robust code● If, in a hot spot, performance is so critical that you must start

micro-optimizing:○ Consider reducing your memory generation (Heap allocations) rather

than the memory copy○ Consider the memory contiguity ○ Consider adding a comment:

😅

Page 33: Pointers vs Values: digging into the performance warmacias.info/static/assets/talks/202001go.pdf · Pointers vs Values: digging into the performance war Mario Macías Lloret Senior

Thank you for your attention!

Mario Macías LloretSenior software engineer at New Relic

Barcelona Golang MeetupJanuary 2020

twitter.com/MaciasUPC github.com/mariomac