16

“WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

Embed Size (px)

DESCRIPTION

August Managed Code for Real Time? It may seem like a crazy notion to some people, but gamers probably aren’t nearly as surprised Managed runtimes in the game world are common My first AI code was written in LPC for LPMud You can get great results out of a managed runtime Why?

Citation preview

Page 1: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

“WALK IN” SLIDE

Page 2: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Taming the CLR: Taming the CLR: How to Write Really Fast Managed How to Write Really Fast Managed CodeCode

Rico MarianiRico MarianiArchitectArchitectDeveloper Division Performance TeamDeveloper Division Performance TeamMicrosoftMicrosoft

Presentation/Presenter Title Slide

Page 3: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Managed Code for Real Managed Code for Real Time?Time?

It may seem like a crazy notion to It may seem like a crazy notion to some people, but gamers probably some people, but gamers probably aren’t nearly as surprisedaren’t nearly as surprisedManaged runtimes in the game world Managed runtimes in the game world are commonare common

My first AI code was written in LPC for My first AI code was written in LPC for LPMudLPMud

You can get great results out of a You can get great results out of a managed runtimemanaged runtimeWhy?Why?

Page 4: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Good for What Ails YouGood for What Ails YouGarbage-collected memory model is the Garbage-collected memory model is the centerpiececenterpieceAmortized cost of allocations in this Amortized cost of allocations in this model model can can be excellent!be excellent!Long term fragmentation is less of a Long term fragmentation is less of a problemproblem

Who hasn’t played a game that degrades Who hasn’t played a game that degrades over 2 hours of play?over 2 hours of play?

Locality can be good due to compaction Locality can be good due to compaction – we need all the cache hits we can get– we need all the cache hits we can getEasy to use model, immune to classic Easy to use model, immune to classic “leaks” and wild pointers“leaks” and wild pointers

Page 5: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Great, I’ll Take a Dozen!Great, I’ll Take a Dozen!Wait, not so fast, you have to read the Wait, not so fast, you have to read the fine print…fine print…There are important practices to get There are important practices to get these benefitsthese benefitsThe GC is like a BFG9000The GC is like a BFG9000

Don’t shoot yourself in the foot with it!Don’t shoot yourself in the foot with it!

Page 6: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Things You Need to KnowThings You Need to KnowIn .NET CF, the GC is a compacting In .NET CF, the GC is a compacting mark and sweep collectormark and sweep collector

Contrast with the regular .NET Collector, Contrast with the regular .NET Collector, which is generationalwhich is generational

What does this mean?What does this mean?You probably should understand both You probably should understand both models, because you can find yourself models, because you can find yourself on the PC platform, tooon the PC platform, tooLet’s make a pictureLet’s make a picture

Page 7: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Simplified GC ModelSimplified GC ModelThis figure shows This figure shows generations that generations that are not always are not always presentpresentSome of the details Some of the details in the following in the following discussion are not discussion are not exactly correctexactly correctThe idea is to have The idea is to have a mental model a mental model that helps you that helps you understand costsunderstand costs

Page 8: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Why Do You Care?Why Do You Care?Really, there are just a few things that Really, there are just a few things that you need to keep in mindyou need to keep in mind

Allocations are cheap—basically, you Allocations are cheap—basically, you increment a pointerincrement a pointerObjects that are allocated close Objects that are allocated close together in time tend to be close together in time tend to be close together in spacetogether in spaceFull collections can be expensive Full collections can be expensive because the heap could be arbitrarily because the heap could be arbitrarily bigbig

Page 9: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Stay in ControlStay in ControlYou can get great performance in this You can get great performance in this kind of world if you stay in controlkind of world if you stay in controlIn the regular CLR, for real-time or In the regular CLR, for real-time or high throughput applications, I advise high throughput applications, I advise people to keep their eye on mid-life people to keep their eye on mid-life objects, make sure they have few of objects, make sure they have few of themthem

Mid-life objects cause you to have to do Mid-life objects cause you to have to do full collections, and a lot more memory is full collections, and a lot more memory is implicatedimplicated

But what if we don’t even have But what if we don’t even have generations?generations?

Page 10: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

.NET CF Considerations.NET CF ConsiderationsA good way to think about .NET CF is A good way to think about .NET CF is that it’s like you that it’s like you onlyonly have generation have generation 00Your Your totaltotal heap size should be roughly heap size should be roughly what your generation 0 size would what your generation 0 size would have been – that means comparable have been – that means comparable to the CPU cacheto the CPU cacheCollection times in that world will be Collection times in that world will be excellentexcellent

If your heap gets too big, collect times will If your heap gets too big, collect times will kill youkill you

Page 11: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Allocation RateAllocation RateIn addition to monitoring typical heap In addition to monitoring typical heap size, you also need to keep your eye size, you also need to keep your eye on the allocation rateon the allocation rateFreshly allocated memory has to be Freshly allocated memory has to be zeroed, and then “constructed” (by zeroed, and then “constructed” (by you)you)

Basically, that’s a lot of memory trafficBasically, that’s a lot of memory trafficAt 20–40 MB/s you are going to be At 20–40 MB/s you are going to be pleased with the memory pleased with the memory management overheadmanagement overhead

At 60 fps, that’s around 500 KB per frame, At 60 fps, that’s around 500 KB per frame, or say 20K objects of modest sizeor say 20K objects of modest size

Page 12: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

What else?What else?Jitted code optimization is not as good Jitted code optimization is not as good as a full optimizing compileras a full optimizing compiler

So this is no place to do intense floating So this is no place to do intense floating point math, that’s what Shaders are forpoint math, that’s what Shaders are for

BUT! You’ll write simpler codeBUT! You’ll write simpler codeThe fastest code is the code that isn’t The fastest code is the code that isn’t there!there!Simplifications can easily dwarf code-gen Simplifications can easily dwarf code-gen taxes, and give you coding time taxes, and give you coding time elsewhereelsewhere

Cheap memory model can be ideal for Cheap memory model can be ideal for AI logic and physicsAI logic and physics

Page 13: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Interop with NativeInterop with NativeJust a few words here, again you can Just a few words here, again you can shoot yourself in the footshoot yourself in the foot

Keep your interop under controlKeep your interop under controlUse primitive types in the API whenever Use primitive types in the API whenever possiblepossible

Marshalling costs are what will kill youMarshalling costs are what will kill youSimple calls can be Simple calls can be very very fast fast

Desktop CLR can get many millions of calls Desktop CLR can get many millions of calls per second on simple signaturesper second on simple signatures

Native Interop is not possible on Xbox Native Interop is not possible on Xbox 360360

Page 14: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

ResourcesResourcesAlthough I normally write about the Although I normally write about the Desktop CLR, much of what I write is Desktop CLR, much of what I write is applicable to .NET CF as wellapplicable to .NET CF as well

http://blogs.msdn.com/ricomhttp://blogs.msdn.com/ricom

Also, many of the standard best Also, many of the standard best practices for scalability applypractices for scalability apply

http://msdn.microsoft.com/perfhttp://msdn.microsoft.com/perf

Page 15: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

August 14-15 2006

Questions?Questions?

Page 16: “WALK IN” SLIDE. August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft

© 2006 © 2006 MicrosoftMicrosoft Corporation. All rights reserved. Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

DirectX Developer CenterDirectX Developer Centerhttp://msdn.microsoft.com/directxhttp://msdn.microsoft.com/directx

Game Development MSDN ForumsGame Development MSDN Forumshttp://forums.microsoft.com/msdnhttp://forums.microsoft.com/msdn

Xbox 360 CentralXbox 360 Centralhttp://xds.xbox.com/http://xds.xbox.com/

XNA Web siteXNA Web sitehttp://www.microsoft.com/xnahttp://www.microsoft.com/xna