Writing faster managed code Claudio Caldato Program Manager CLR Performance Team.

Slides:



Advertisements
Similar presentations
An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Advertisements

Borland Optimizeit™ Profiler for the Microsoft .NET Framework
Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
.NET Framework Overview Pingping Ma Nov 16 th, 2006.
1 The Compressor: Concurrent, Incremental and Parallel Compaction. Haim Kermany and Erez Petrank Technion – Israel Institute of Technology.
This presentation: Sasha GoldshteinCTO, Sela Group Garbage Collection Performance Tips.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
Introducing the Common Language Runtime for.NET. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution engine.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Patterns & practices Designing For Performance (For.NET) Vance Morrison Performance Architect.NET Runtime Team.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
Optimizing RAM-latency Dominated Applications
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
CLR: Garbage Collection Inside Out
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
Introduction to .Net Framework
.NET Overview. 2 Objectives Introduce.NET –overview –languages –libraries –development and execution model Examine simple C# program.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Ulterior Reference Counting: Fast Garbage Collection without a Long Wait Author: Stephen M Blackburn Kathryn S McKinley Presenter: Jun Tao.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.
Lecture 10 : Introduction to Java Virtual Machine
.NET Framework Danish Sami UG Lead.NetFoundry
© 2013 IBM Corporation Practical Performance Understand and improve the performance of your application San Hong Li – Technical lead of JTC Shanghai 23.
Java Virtual Machine Case Study on the Design of JikesRVM.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
Memory Management. Memory  Commemoration or Remembrance.
Computer Programming 2 Why do we study Java….. Java is Simple It has none of the following: operator overloading, header files, pre- processor, pointer.
Key Components of.NET Framework  Common Language Runtime  CLR at Design time  CLR at Runtime  Class Library  Assemblies  Namespaces  ASP.NET  Applications.
PRIOR TO WEB SERVICES THE OTHER TECHNOLOGIES ARE:.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
Ben Watson Principal Software Engineer Shared Platform Group, Application Services Group, Microsoft Author, Writing High-Performance.NET Code.
CSCI1600: Embedded and Real Time Software Lecture 18: Real Time Languages Steven Reiss, Fall 2015.
DEV441 Writing Faster Managed Code Jonathan Hawkins Lead Program Manager Common Language Runtime.
CSC Multiprocessor Programming, Spring, 2012 Chapter 11 – Performance and Scalability Dr. Dale E. Parson, week 12.
Base Operating System Services CLR TransactionsStorage Protocols Network Services Kernel Mode Base Class Libraries Memory Manager Hosting Layer Code Execution.
Microsoft .NET A platform that can be used for building and running windows and web applications such that the software is platform and device-independent.
Object Oriented Software Development 4. C# data types, objects and references.
Tips & Tricks: Writing Performant Managed Code Rico Mariani FUNL04 Performance Architect Microsoft Corporation.
Common Language Runtime Introduction  The common language runtime is one of the most essential component of the.Net Framework.  It acts.
DEV394 Windows Forms Performance Tips And Tricks Mike Henderlight Development Manager.NET Client Team Microsoft Corporation
DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation.
“WALK IN” SLIDE. August Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
DEV491.NET Framework: Writing Faster Managed Code Jan GrayAbhi Khune ArchitectDevelopment Lead //blogs.msdn.com/jangr//blogs.msdn.com/akhune CLR Performance.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Lecture #20: Profiling NetBeans Profiler 6.0.
Capacity Planning in a Virtual Environment Chris Chesley, Sr. Systems Engineer
Introduction to Performance Tuning Chia-heng Tu PAS Lab Summer Workshop 2009 June 30,
.NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group.
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Module 9: Memory and Resource Management
An Introduction to the Shared Source Common Language Infrastructure (SSCLI) Damien Watkins Copyright Watkins 2002.
.NET and .NET Core 2. .NET Runtimes Pan Wuming 2017.
Introduction to Computer Systems
Simulated Pointers.
AVG 24th 2015 ADVANCED c# - part 1.
Simulated Pointers.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CSC Multiprocessor Programming, Spring, 2011
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Writing faster managed code Claudio Caldato Program Manager CLR Performance Team

Outline Performance engineering Performance engineering Managed code performance Managed code performance –Contrast with native code –Garbage collection –Features’ cost and Pitfalls Perf problems diagnosis Perf problems diagnosis

Performance engineering Set goals Set goals Measure, measure and then measure Measure, measure and then measure Know your platform Know your platform Process: Process: –Budget, plan, verify –continuous improvement Measure, track, refine Measure, track, refine automated tests automated tests Build a performance culture Build a performance culture –User expectations –Developer attitudes – perf is my feature!

Moving to managed code Why? Productivity and quality Why? Productivity and quality –Do more with less code –Fewer bugs –Clean, modern libraries targeting modern requirements –Better software, sooner But is performance a problem? But is performance a problem? –Easier to write programs fast –Easier to write fast programs?

Know Your Garbage Collector Why? Why? GC basics GC basics –Pause threads; trace reachable objects from roots; compact live objects; recycle dead memory Self tuning Self tuning CLR is a generational mark and sweep GC CLR is a generational mark and sweep GC

Generation 1Generation 0 New objects allocated in generation 0 GC: accessible references keep objects alive GC: preserves / compacts referenced objects GC: objects left merged into older generation Once again, new objects allocated in generation 0 Garbage Collection in Action

Know Your Garbage Collector GC basics GC basics –Pause threads; trace reachable objects from roots; compact live objects; recycle dead memory Self tuning Self tuning Generational GC Heaps Generational GC Heaps –Gen0 – new objects – cache conscious; fast GC –Gen1 – objects survived a GC of gen0 –Gen2 – long lived objects – survived a GC of gen1,2 –Large object heap Server GC Server GC –Optimized for throughput and multi processor scalability

Garbage Collection Pitfalls Object lifetimes still matter! Object lifetimes still matter! Use an efficient “allocation profile” Use an efficient “allocation profile” –Short lived objects are cheap (but not free) –Don’t have a “midlife crisis” (avoid gen2 churn) –Review with perfmon counters, CLRProfiler Common Pitfalls Common Pitfalls –Keeping refs to “dead” object graphs Null out object references (where appropriate) Null out object references (where appropriate) –Implicit boxing –Pinning young objects –GC.Collect considered harmful –Finalization...

Garbage Collection Pitfalls (2) Finalization and the Dispose Pattern ~C() : non-deterministic clean up. ~C() : non-deterministic clean up. –object unref’d –promote to the next generation –queue finalizer –Costs: retains object graph, finalizer thread use Dispose Pattern use Dispose Pattern –Implement IDisposable –Call GC.SuppressFinalize –Hold few obj fields –Dispose early, when possible use ‘using’ (C#)

Pitfall: Indiscriminate Code Reuse Your choices determine your perf Your choices determine your perf –Your architecture, algorithms,... –Your uses of.NET FX types and methods No specific advice holds everywhere, so you have to do your homework No specific advice holds everywhere, so you have to do your homework –Measure, inspect the time and space costs of your platform(s), in your setting

Data locality Data locality –Remote, disk, RAM, cache –GC: objects allocated together in time, stay together in space Data representation Data representation –Complex data structures with a lot of pointers is GC cost Data Cost

Reflection Cost Fast and Light APIs: Fast and Light APIs: – TypeOf, object.GetType, get_Module, get_MemberType, new Token/Handle resolution APIs Costly APIs: Costly APIs: –MemberInfo, MethodInfo, FieldInfo, GetCustomAttribute, InvokeMember, Invoke, get_Name Only request what you need Only request what you need –minimize the use of GetMembers, GetConstructors, … Consider using the new Token/Handle resolution APIs Consider using the new Token/Handle resolution APIs

Cache members after having retrieved them Cache members after having retrieved them –For instance cache Member’s handle Avoid using Type.InvokeMember Avoid using Type.InvokeMember Avoid doing case insensitive member lookups Avoid doing case insensitive member lookups Use BindingsFlags.ExactMatch whenever possible Use BindingsFlags.ExactMatch whenever possible Use FxCop Use FxCop Insidious Insidious –.NET FX code that uses reflection –Late bound code in VB.NET, JScript.NET Enforce early binding Enforce early binding – Option Explicit On Option Strict On Reflection Cost (2)

P/Invoke, COM Interop Cost Efficient, but frequent calls add up Efficient, but frequent calls add up Costs also depend on marshaling Costs also depend on marshaling –Primitive types and arrays of same are cheap –Unicode to ANSI string conversions are not. Diagnosis Diagnosis –Perfmon:.NET CLR Interop counters (# of marshalling) –Time based profiling Mitigate interop call costs by batching calls or move the boundary Mitigate interop call costs by batching calls or move the boundary

Deployment considerations Assemblies Assemblies –Performance-wise: the fewer, the better! Use GAC Use GAC –Avoids repetitive SN signature verification Use NGEN Use NGEN –Caches pre-JIT’d DLL; code may run slower –Generally reduces startup time, improves code shareability –Try it and measure for yourself

Xml: It is not always the answer System.Xml.dll is 2MB System.Xml.dll is 2MB –Load it only when you need it Don’t use XML classes for trivial tasks Don’t use XML classes for trivial tasks –MyApp.Config.Xml: <MyApp> <Top>512</Top><Left>340</Left> </MyApp>

Analyzing Performance Problems Code Inspection Ildasm – findstr “box” Ildasm – findstr “box” Debuggers – Module loads, rebasing Debuggers – Module loads, rebasing FxCop – Static Analyzer FxCop – Static Analyzer

Analyzing Performance Problems Measure It, With Tools High level diagnostics High level diagnostics –Taskmgr, perfmon, vadump, event tracing for Windows (ETW) Space Space –CLR Profiler, code profilers, ETW Time Time –Code profilers, timing loops, ETW

Improve startup time Cold startup is typically dominated by disk accesses and warm startup by CPU usage. Cold startup is typically dominated by disk accesses and warm startup by CPU usage. Reduce dlls loaded at startup if possible. Reduce dlls loaded at startup if possible. Ngen your assemblies. Jitting consumes CPU at startup. Ngen your assemblies. Jitting consumes CPU at startup. Place strong named assemblies in the GAC. Place strong named assemblies in the GAC. The application would be doing its own computations. Is the bottleneck here? The application would be doing its own computations. Is the bottleneck here?

Resources Patterns & Practices: Improving.NET Application Performance and Scalability Patterns & Practices: Improving.NET Application Performance and Scalability –[ framework developer center.NET framework developer center –Programming Information/performance Usergroup: Usergroup: –microsoft.public.dotnet.framework.performance Blogs Blogs –RicoM, MaoniS Claudio’s Quick list Claudio’s Quick list

Q&A

Questions In the product cycle, when do you start working on performance? In the product cycle, when do you start working on performance? What are the top issues you have to deal with? What are the top issues you have to deal with? Are there good tools to do performance analysis?, what is missing? Are there good tools to do performance analysis?, what is missing? Where type of resources do you use to find answers to performance issues? Where type of resources do you use to find answers to performance issues? Comment the following statement: “In managed code it is easier to find and solve performance issues” Comment the following statement: “In managed code it is easier to find and solve performance issues”