patterns for high - qconsp.com · patterns for high performance c# federico lois twitter:...

49

Upload: trinhdiep

Post on 15-Feb-2019

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The
Page 2: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

Patterns for High Performance C#

Federico LoisTwitter: @federicoloisGithub: redknightloisRepo: performance-course

Page 3: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

“The best programs are written so that

computing machines can perform them

quickly and so that human beings can understand them clearly.”

Donald Knuth

Page 4: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Asymptotic Notation

“Big-O notation is a mathematical notation that

describes the limiting behavior of a function when the

argument tends towards a particular value or infinity.”

Bachmann–Landau notation

Page 5: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

BigO notation

• Instruction Counting. (Turing model)

• Simple, effective for most problems.

• Cache-Oblivious (based on RAM model)

• Incorporates a simple cache to the model.

• Doesn’t explicitly model it’s size.• It can include the tall-cache assumption.

• Cache-Aware (based on RAM model)

• It models explicitely size, structuction, eviction policy.

• Theoretical analysis is pretty complex.

Page 6: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Big O in practice

• Useful to evaluate general behavior.• Not necessarily a deal-breaker

• Guides your hypothesis.

• Usually will not represent the behavior

• Until sizes are big enough to dominate• Which may never happen

• Simple models add uncertainty.• Our job is to adjust those variables.

Page 7: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Performance Bounds

• Compute Bound

• Memory Bound.

• Input/Output Bound

Page 8: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

20% of the code consumes 80% of the resources

Pareto Rule (80-20)

…especially bad when they are in the critical path

Page 9: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto

20% of the code consumes 80% of the CPU/Memory/IO

Page 10: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto

20% of the code consumes 80% of the resources

Page 11: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

20% of the

20% of the code consumes 64% of the resources

Pareto2

…around of 4% of the code.

Page 12: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

20% of the

20% of the

20% of the code consumes51% of the resources

Pareto3

…roughly 0,8% of the code.

Page 13: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto

Architecture/Network/Algorithm

Optimization Land

Page 14: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto

• Choosing the wrong algorithm/data structure.

• Systems outgrowing design parameters.

• Chatty network interfaces: nano-services

• Physical (and not so physical) distance.

• CPU is doing nothing, nichts, nada!

Page 15: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto2

Algorithm Time Optimization Land

Page 16: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto2

• Doing things more than once.

• CPU is doing stuff, just nothing useful (for you)!

• Memory pressure on GC or allocators

• Thread state hand-off

• Using data structures wrong• I’m watching at you int.GetHashCode() & long.GetHashCode()

Page 17: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto3

Micro Optimization Land

Page 18: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto3

Voodoo Land

Page 19: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Pareto3

... function calls will hurt you

... code alignment will hurt you... useless instructions will hurt you

You get the idea

… false sharing will hurt you

… cache line pollution will hurt you

… memory layout will hurt you

…loop size in bytes will hurt you

Page 20: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Secret Sauce for High Performance (???)

• Adopt laziness as a way of life.• Why do things twice when you can do them once.

• Choose the right data structures/algorithms

• Avoid being chatty over the network (aka IO)

• Design for no less than 20x your expected requirements

• Diminish allocations (like the plague)

Page 21: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

Measure, Measure and when you are sure,

Measure Again!!(just in case, you know!)

Page 22: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

The End!

…of not talking about C#

Page 23: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

High Performance C#(even though most would apply to other platforms / frameworks / languages out there…)

Page 24: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

IF-Switch

Page 25: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

IF-Switch

• If you know the statistical distribution• IF tends to be more efficient, except when

• You face a uniform distribution.

• You face a non tail distribution.

• Switch builds a perfect hash

• Unless values are consecutive.

Page 26: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Try-Catch

Page 27: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Try-Catch

CanThrow

Page 28: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Try-Catch

Page 29: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Try-Catch

Without Try-Catch

Page 30: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Interfaces vs Class vs Struct

• Stack Allocation vs Heap Allocation• At least in C#, etc.

• Accessing an struct via interface will allocate on the Heap.• Aka Boxing

• Struct is subject to special optimization.• You can abuse the Dead Code Elimination mechanism to do

simple metaprogramming.

Page 31: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

Allocations

• Pooling

• Generalized

• Contextual

• Per operation

• Stack Allocations

• Fixed

• Structs

• Ref/Out Trick

• Ref Return (C# 7)

Page 32: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

Allocations

Page 33: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Inlining• Compilers do it, and allow you to suggest them targets

• Avoiding the call helps you because it diminishes:

• Instruction Cache Misses

• Push/Pop en el stack

• Number of retired instructions

• Call context changes at the processor

• Avoiding the call increments

• Caller size in bytes.

• Locality of reference

• Collateral effects (among others)

• Dead code elimination

• Contant propagation

Page 34: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Inlining – Call Cost

Page 35: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Inlining – Call Cost

Page 36: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Inlining - Virtual Calls

• They cant be removed without devirtualization.

• The cost of a virtual call is higher than static ones.

Page 37: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Constant Propagation

Page 38: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Constant Propagation

Page 39: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Constant Propagation

Page 40: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Simple Metaprogramming

Page 41: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Comparers

Page 42: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Comparers

Page 43: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Comparers

Page 44: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Code Flow

Page 45: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Page 46: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Page 47: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

But is it really all thiswork worth the

trouble?

Page 48: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

c o r v a l i u s . c o m

Page 49: Patterns for High - qconsp.com · Patterns for High Performance C# Federico Lois Twitter: @federicolois Github: redknightlois Repo: performance-course. c o r v a l i u s . c o m “The

Thanks for coming!