Presentation is loading. Please wait.

Presentation is loading. Please wait.

“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.

Similar presentations


Presentation on theme: "“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."— Presentation transcript:

1 “WALK IN” SLIDE

2 August 14-15 2006 Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft Presentation/Presenter Title Slide

3 August 14-15 2006 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?

4 August 14-15 2006 Good for What Ails You Garbage-collected memory model is the centerpiece Amortized cost of allocations in this model can be excellent! Long term fragmentation is less of a problem Who hasn’t played a game that degrades over 2 hours of play? Locality can be good due to compaction – we need all the cache hits we can get Easy to use model, immune to classic “leaks” and wild pointers

5 August 14-15 2006 Great, I’ll Take a Dozen! Wait, not so fast, you have to read the fine print… There are important practices to get these benefits The GC is like a BFG9000 Don’t shoot yourself in the foot with it!

6 August 14-15 2006 Things You Need to Know In.NET CF, the GC is a compacting mark and sweep collector Contrast with the regular.NET Collector, which is generational What does this mean? You probably should understand both models, because you can find yourself on the PC platform, too Let’s make a picture

7 August 14-15 2006 Simplified GC Model This figure shows generations that are not always present Some of the details in the following discussion are not exactly correct The idea is to have a mental model that helps you understand costs

8 August 14-15 2006 Why Do You Care? Really, there are just a few things that you need to keep in mind Allocations are cheap—basically, you increment a pointer Objects that are allocated close together in time tend to be close together in space Full collections can be expensive because the heap could be arbitrarily big

9 August 14-15 2006 Stay in Control You can get great performance in this kind of world if you stay in control In the regular CLR, for real-time or high throughput applications, I advise people to keep their eye on mid-life objects, make sure they have few of them Mid-life objects cause you to have to do full collections, and a lot more memory is implicated But what if we don’t even have generations?

10 August 14-15 2006.NET CF Considerations A good way to think about.NET CF is that it’s like you only have generation 0 Your total heap size should be roughly what your generation 0 size would have been – that means comparable to the CPU cache Collection times in that world will be excellent If your heap gets too big, collect times will kill you

11 August 14-15 2006 Allocation Rate In addition to monitoring typical heap size, you also need to keep your eye on the allocation rate Freshly allocated memory has to be zeroed, and then “constructed” (by you) Basically, that’s a lot of memory traffic At 20–40 MB/s you are going to be pleased with the memory management overhead At 60 fps, that’s around 500 KB per frame, or say 20K objects of modest size

12 August 14-15 2006 What else? Jitted code optimization is not as good as a full optimizing compiler So this is no place to do intense floating point math, that’s what Shaders are for BUT! You’ll write simpler code The fastest code is the code that isn’t there! Simplifications can easily dwarf code-gen taxes, and give you coding time elsewhere Cheap memory model can be ideal for AI logic and physics

13 August 14-15 2006 Interop with Native Just a few words here, again you can shoot yourself in the foot Keep your interop under control Use primitive types in the API whenever possible Marshalling costs are what will kill you Simple calls can be very fast Desktop CLR can get many millions of calls per second on simple signatures Native Interop is not possible on Xbox 360

14 August 14-15 2006 Resources Although I normally write about the Desktop CLR, much of what I write is applicable to.NET CF as well http://blogs.msdn.com/ricom Also, many of the standard best practices for scalability apply http://msdn.microsoft.com/perf

15 August 14-15 2006 Questions?

16 © 2006 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. DirectX Developer Center http://msdn.microsoft.com/directx Game Development MSDN Forums http://forums.microsoft.com/msdn Xbox 360 Central http://xds.xbox.com/ XNA Web site http://www.microsoft.com/xna http://msdn.microsoft.com/directx http://forums.microsoft.com/msdn http://xds.xbox.com/ http://www.microsoft.com/xna http://msdn.microsoft.com/directx http://forums.microsoft.com/msdn http://xds.xbox.com/ http://www.microsoft.com/xna End Slide


Download ppt "“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."

Similar presentations


Ads by Google