ben watson principal software engineer shared platform group, application services group, microsoft...

71
Lessons in Extreme .NET Performance on l Software Engineer latform Group, Application Services Group, Microsoft Writing High-Performance .NET Code

Upload: antonia-gaines

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Lessons in Extreme .NET Performance

Ben WatsonPrincipal Software EngineerShared Platform Group, Application Services Group, MicrosoftAuthor, Writing High-Performance .NET Code

Page 2: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 3: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 4: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Why Managed Code?

Page 5: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Benefits of .NET

Isolation and safetyEasy testingDebuggingCode is easy to write*

Page 6: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

GC JIT 3rd Party Code People Factors

Page 7: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Garbage Collection

Stops your program DEAD

Page 8: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The Threat

Page 9: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

It’s very easy to build a system that spends an extraordinary amount of time in GC.

This is not inherent to the CLR!

Page 10: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

It is impossible to build a low-latency, high-performance server without taking

GC into account from the beginning.

Page 11: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

It is a myth that you can’t control the GC – you do so by controlling allocations.

Page 12: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

You need to understand the inner workings of the GC, just like you would any memory allocator.

Page 13: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Generational GC in 30 seconds

Page 14: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Gen2 Gen1 Gen0

Allocation point

Page 15: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Lessons Learned

Page 16: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The One Rule:Objects must die in a gen0 GC

- or –They must live forever in gen2.

Page 17: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

You will usually want to optimize for as little memory allocation as possible.

Page 18: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The cost of a GC is proportional to the number of surviving objects.

Page 19: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Consider pooling for objects with a long or unknown lifetime.

Page 20: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Pool everything in the large object heap.

Page 22: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

GC work never stops

Page 23: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Just In Time Compilation

Just in time to SLOW YOU DOWN

Page 24: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Should you worry about JIT?

Page 25: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The Threat

Page 26: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Reduce the amount of Code

Page 27: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Use the latest .NET

Page 28: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Warmup Code

Page 29: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Manual JITRuntimeHelpers.PrepareMethod

Page 30: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

NGEN

Page 31: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

A little of this, a little of that…

Page 32: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

3rd Party Code

Trust No One

Page 33: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

My Code vs. Your Code

Page 34: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 35: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 36: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Joking aside…

Page 37: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The Threat

Page 38: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

The Framework Class Library

Page 39: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

How would you implement Enum.HasFlag()?

Page 40: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 41: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 42: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 43: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Read the Source

Page 44: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Default collection sizes

Page 45: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Quiz: How many different XML serialization options ship with .NET?

Page 46: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

XmlTextReader

XmlValidatingReader

XDocument

XPathNavigatorXPathDocument

LINQ-to-XML

DataContractSerializer

XmlSerializer

Page 47: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Methods that throw exceptions in normal conditions.

Page 48: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Static analysis

Page 49: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

FxCop

Stupid String Tricks: ToLower, ToUpper, Format, Join

Page 50: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

FxCop

Stupid String Tricks: ToLower, ToUpper, Format, Join

Large Static Allocations (>85K go on LOH)

Page 51: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

FxCop

Stupid String Tricks: ToLower, ToUpper, Format, Join

Large Static Allocations (>85K go on LOH)

Prevent finalizers

Page 52: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

FxCop

Stupid String Tricks: ToLower, ToUpper, Format, Join

Large Static Allocations (>85K go on LOH)

Force use of pooling APIs for some types

Prevent finalizers

Page 53: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

FxCop

Stupid String Tricks: ToLower, ToUpper, Format, Join

Large Static Allocations (>85K go on LOH)

Prevent finalizersWarn against LINQ

Force use of pooling APIs for some types

Page 54: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

It looks like you’re writing awful, poorly

performing code. Would you like to stop

already?

Projects like RoslynClrHeapAllocationAnalyzer

This is the future…

(note: Clippy not actually included)

Page 55: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

People Factors

(No, you can’t actually use a hammer and sword.)

Page 56: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 57: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 58: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

You must train people

Page 59: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Code does not exist in a vacuum

Page 60: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Understand the Tradeoffs

Page 61: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Understand the Tradeoffs

Benefits

• Memory safety

• Type safety

• Fast allocations

• Debugging is MUCH better

• Better static code analysis

• Code locality

Costs

• JIT• Garbage Collection

• Bounds checking, type checking

• Training

• Hidden code

Page 62: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

It is impossible to build a low-latency, high-performance server without taking

GC into account from the beginning.

Page 63: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Performance is a Feature

Page 64: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

GC JIT 3rd Party Code People Factors

Page 65: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code
Page 66: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Want to know more?

.NET Performance Deep-Dive WorkshopThursday, 9am, Garden A

Page 67: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Resources

• Books• Writing High-Performance .NET Code• Book Site – Can get the following for free: • Intro, Chapter 1 – general measurement guidelines, tools, how to use Windbg

and PerfView (and others)• Chapter 5 – Performance Considerations of Class Design and General Coding

• Advanced .NET Debugging by Mario Hewardt• Article: .NET memory allocation details

Page 68: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Resources

• Vance Morrison’s CLR Inside Out columns on performance: Measure Early and Often for Performance, Parts 1 & 2• http://msdn.microsoft.com/en-us/magazine/cc500596.aspx• http://msdn.microsoft.com/en-us/magazine/cc507639.aspx

• Interface Dispatch: http://blogs.msdn.com/b/vancem/archive/2006/03/13/550529.aspx• SOS command reference• http://msdn.microsoft.com/en-us/library/bb190764.aspx

• Windbg command reference• http://windbg.info/doc/1-common-cmds.html

Page 69: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Tools

• Reflector, ILSpy, ILDASM – tools to convert compiled binaries into readable code. Essential for understanding how 3rd-party code (including the FCL) works.• FxCop• Performance rules are limited, but useful• Warns about things like multiple casts, Equals() on value types, empty finalizers, etc.• Lots of other style and correctness rules• Get in the habit of running these as part of your build• DON’T get in the habit of issuing exceptions for rule violations -- just fix the issue if

possible

Page 70: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Tools

• MeasureIt• Small, simple tool for running microbenchmarks on the .Net framework• http://blogs.msdn.com/b/vancem/archive/2009/02/06/measureit-update-tool-for-doing-micro

benchmarks.aspx• PerfView• ETW event analyzer with clever views (like a CPU profiler)• Can analyze both CPU and memory events• Can tell you who is allocating memory, who owns memory, who is allocating on the LOH• How often are GCs? How long do they last?• Exceptions being thrown and handled• Jitting events• Anything else that’s an ETW event (which is most things)

Page 71: Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code

Tools

• Windbg & SOS• The best way in many cases to understand what’s really going on• SOS is the managed debugging extensions• Comes with CLR, already setup to work with WinDbg in PHX• See http://msdn.microsoft.com/en-us/library/bb190764.aspx for SOS commands• Getting started:• Attach to your process• .loadby sos clr• !bpmd program.exe Namespace.Type.Method• g• !U @rip• !ClrStack