1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET...

49
1 .NET Compact Framework .NET Compact Framework 2.0 2.0 Optimizing For Optimizing For Performance Performance Roman Batoukov Roman Batoukov FUN403 FUN403 Development Lead Development Lead .NET Compact Framework .NET Compact Framework Microsoft Corporation Microsoft Corporation

Transcript of 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET...

Page 1: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

1

.NET Compact Framework .NET Compact Framework 2.02.0Optimizing For PerformanceOptimizing For Performance

Roman BatoukovRoman BatoukovFUN403FUN403Development LeadDevelopment Lead.NET Compact Framework.NET Compact FrameworkMicrosoft CorporationMicrosoft Corporation

Page 2: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

2

.NET Compact Framework.NET Compact Framework

VisualVisualStudioStudio

Windows CEWindows CE

Low level operating system-Low level operating system-specific functionalityspecific functionality

ThreadsThreads MemoryMemory

NetworkingNetworking File I/OFile I/O

CLRCLR

Type systemType system LoaderLoader JIT CompilerJIT Compiler

Execution Engine provides Execution Engine provides typesafe Runtime for managed typesafe Runtime for managed codecode Garbage Garbage

collectorcollector DebuggerDebugger

FXFX

Rich class libraries to make Rich class libraries to make your your life easy!life easy! GUI: Forms GUI: Forms

GUI: Drawing (2D & 3D)GUI: Drawing (2D & 3D) CollectionsCollections IO, Networking, CryptoIO, Networking, Crypto

Native Native interopinterop

Web Web servicesservices

Data & XmlData & Xml

GlobalizationGlobalization

Page 3: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

3

.NET Compact Framework.NET Compact FrameworkHow we are different?How we are different?

Memory constraintsMemory constraintsStorage – Flash/ROMStorage – Flash/ROMPhysical MemoryPhysical MemoryVirtual Memory – 32MB per processVirtual Memory – 32MB per process

DesignDesign28% of the surface area in 8% of the size of 28% of the surface area in 8% of the size of full .NET Frameworkfull .NET FrameworkPortable JIT CompilerPortable JIT Compiler

Fast code generation, less optimizedFast code generation, less optimizedMay pitch JIT-compiled codeMay pitch JIT-compiled codeNo NGen, install time or persisted codeNo NGen, install time or persisted code

Interpreted virtual calls (no v-tables)Interpreted virtual calls (no v-tables)Sparse loading of metadataSparse loading of metadata

Page 4: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

4

Measuring PerformanceMeasuring PerformanceOverviewOverview

Micro-benchmarks versus ScenariosMicro-benchmarks versus ScenariosBenchmarking tipsBenchmarking tips

Use Environment.TickCount to measureUse Environment.TickCount to measureMeasure times greater than 1 secondMeasure times greater than 1 secondStart from known stateStart from known stateEnsure nothing else is runningEnsure nothing else is runningMeasure multiple times, take averageMeasure multiple times, take averageRun each test in own AppDomain/ProcessRun each test in own AppDomain/ProcessLog results at the endLog results at the endUnderstand JIT-time versus run-time costUnderstand JIT-time versus run-time cost

Page 5: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

5

.NET Compact Framework .NET Compact Framework

.NET Compact Framework Performance v1-.NET Compact Framework Performance v1->v2 >v2

(Pocket PC 2003, XScale 400MHz) 1.0 1.0 SP3V2

Beta1

V2

current

Method Calls (Calls/sec) 3.7M 7.1M 8.1M

Virtual Calls (Calls/sec) 2.4M 2.7M 5.6M

Simple P/Invoke (Calls/sec) 733K 1.7M 1.8M

Primes (to 1500) (iterations/sec) 562 832 855

GC Small (8 bytes) (Bytes/sec) 1M 7M 7.5M

GC Array (100 int’s) (Bytes/sec) 25M 43M 115M

XML Text Reader 200KB (seconds) 1.7 1.2 0.72 0.69

DataSet (static data)4 tables, 1000 records (seconds) 13.1 6.6 7.3 4.0

DataSet (ReadXml)3 tables, 100 records (seconds) 12.3 6.5 5.2 3.9

Biggeris better

Smalleris better

Page 6: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

6

Measuring PerformanceMeasuring PerformancePerformance countersPerformance counters

<My App>.stat (formerly mscoree.stat)<My App>.stat (formerly mscoree.stat)http://http://msdn.microsoft.com/library/enus/dnnetcompmsdn.microsoft.com/library/enus/dnnetcomp/html//html/netcfperf.aspnetcfperf.aspRegistryRegistry

HKLM\SOFTWARE\Microsoft\.NETCompactFramework\PerfMonitorHKLM\SOFTWARE\Microsoft\.NETCompactFramework\PerfMonitorCounters (DWORD) = 1Counters (DWORD) = 1

What does .stat tell you?What does .stat tell you?Working set and performance statisticsWorking set and performance statistics

More counters added in v2More counters added in v2Generics usageGenerics usageCOM interop usageCOM interop usageNumber of boxed valuetypesNumber of boxed valuetypesThreading and timersThreading and timersGUI objectsGUI objectsNetwork activity (socket bytes send/received)Network activity (socket bytes send/received)

Page 7: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

7

.stat.statcounter total last datum n mean min maxTotal Program Run Time (ms) 55937 - - - - -App Domains Created 18 - - - - -App Domains Unloaded 18 - - - - -Assemblies Loaded 323 - - - - -Classes Loaded 18852 - - - - -Methods Loaded 37353 - - - - -Closed Types Loaded 730 - - - - -Closed Types Loaded per Definition 730 8 385 1 1 8Open Types Loaded 78 - - - - -Closed Methods Loaded 46 - - - - -Closed Methods Loaded per Definition 46 1 40 1 1 2Open Methods Loaded 0 - - - - -Threads in Thread Pool - 0 6 1 0 3Pending Timers - 0 93 0 0 1Scheduled Timers 46 - - - - -Timers Delayed by Thread Pool Limit 0 - - - - -Work Items Queued 46 - - - - -Uncontested Monitor.Enter Calls 57240 - - - - -Contested Monitor.Enter Calls 0 - - - - -Peak Bytes Allocated (native + managed) 4024363 - - - - -Managed Objects Allocated 1015100 - - - - -Managed Bytes Allocated 37291444 28 1015100 36 8 55588Managed String Objects Allocated 112108 - - - - -Bytes of String Objects Allocated 4596658 - - - - -Garbage Collections (GC) 33 - - - - -Bytes Collected By GC 25573036 41592 33 774940 41592 1096328Managed Bytes In Use After GC - 23528 33 259414 23176 924612Total Bytes In Use After GC - 3091342 33 2954574 1833928 3988607GC Compactions 17 - - - - -Code Pitchings 6 - - - - -Calls to GC.Collect 0 - - - - -GC Latency Time (ms) 279 16 33 8 0 31Pinned Objects 156 - - - - -Objects Moved by Compactor 73760 - - - - -Objects Not Moved by Compactor 11811 - - - - -Objects Finalized 6383 - - - - -Boxed Value Types 350829 - - - - -Process Heap - 1626 430814 511970 952 962130Short Term Heap - 0 178228 718 0 21532JIT Heap - 0 88135 357796 0 651663App Domain Heap - 0 741720 647240 0 833370GC Heap - 0 376 855105 0 2097152Native Bytes Jitted 7202214 152 26910 267 80 5448Methods Jitted 26910 - - - - -Bytes Pitched 1673873 0 7047 237 0 5448

Peak Bytes Allocated (native + Peak Bytes Allocated (native + managed) managed) JIT Heap JIT Heap App Domain Heap App Domain Heap GC Heap GC Heap GC Latency Time (ms) GC Latency Time (ms)

Garbage Collections (GC) Garbage Collections (GC)

Managed String Objects Allocated Managed String Objects Allocated

Boxed Value Types Boxed Value Types

Page 8: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

8

FXFX

.NET Compact Framework.NET Compact Framework

CLRCLR

Windows CEWindows CE

Globalization GUINetI/OCrypto

System.System.GlobalizationGlobalization

System.System.CryptographyCryptography

System.System.IO.PortsIO.Ports

Microsoft.Microsoft.Win32.RegistryWin32.Registry

System.IO.System.IO.FileFile

System.System.DataData

System.XmlSystem.Xml

SystemSystem

mscorlibmscorlib

Microsoft.Microsoft.VisualBasicVisualBasic

JIT CompilerJIT Compiler& GC& GCDebuggerDebugger

ClassClassLoaderLoader

AssemblyAssemblyCacheCache

NativeNativeInteropInterop

App DomainApp DomainLoaderLoader

ProcessProcessLoaderLoader

Memory and Memory and ThreadingThreading

File MappingFile MappingCert/SecurityCert/SecurityVerificationVerification

System.System.WebServicesWebServices

System.Net.System.Net.Http*Http*

System.Net.System.Net.SocketsSockets

DirectX.DirectX.DirectD3DMDirectD3DM

Windows.Windows.FormsForms

System.System.DrawingDrawing

SSLSSL

SocketsSockets

NTLMNTLM

GDI/GWESGDI/GWES

CommonCommonControlsControls

RegistryRegistry

File I/OFile I/O

EncodingsEncodings

SortingSorting Crypto APICrypto API

CalendarCalendarDataData

CultureCultureDataData

RedistRedist

HostHost

Visual StudioVisual Studio

Debug EngineDebug Engine

ICorDbgICorDbg

Managed LoaderManaged Loader

MSI SetupMSI Setup(ActiveSync)(ActiveSync)

Per Device CABPer Device CABInstall (SMS, etc)Install (SMS, etc)

System.System.ReflectionReflection

CasingCasing D3DMD3DM

Page 9: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

9

Common Language Common Language RuntimeRuntimeExecution engineExecution engine

Call pathCall pathManaged calls are more expensive than nativeManaged calls are more expensive than native

Instance call: ~2-3X the cost of a native function callInstance call: ~2-3X the cost of a native function call

Virtual call: ~1.4X the cost of a managed instance callVirtual call: ~1.4X the cost of a managed instance call

Platform invoke: ~5X the cost of managed instance call Platform invoke: ~5X the cost of managed instance call (*Marshal int parameter)(*Marshal int parameter)

Properties are callsProperties are calls

JIT compilersJIT compilersAll platforms has the same optimizing JIT compiler All platforms has the same optimizing JIT compiler architecture in v2architecture in v2

OptimizationsOptimizationsMethod inlining for simple methodsMethod inlining for simple methods

Variable enregistration Variable enregistration

String interningString interning

Page 10: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

10

Common Language Common Language RuntimeRuntimeCall path (sample)Call path (sample)public class Shape public class Shape

{{

protected int m_volume; protected int m_volume;

public public virtualvirtual int Volume int Volume

{ {

get {return m_volume;}get {return m_volume;}

}}

}}

public class Cube:Shape public class Cube:Shape

{{

public MyType(int vol) public MyType(int vol)

{ {

m_volume = vol;m_volume = vol;

}}

}}

public class Shape public class Shape

{{

protected int m_volume; protected int m_volume;

public int Volume public int Volume

{ {

get {return m_volume;}get {return m_volume;}

}}

}}

public class Cube:Shape public class Cube:Shape

{{

public MyType(int vol) public MyType(int vol)

{ {

m_volume = vol;m_volume = vol;

}}

}}

Page 11: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

11

Common Language Common Language RuntimeRuntimeCall path (sample) Call path (sample) public class MyCollectionpublic class MyCollection

{{private const int m_capacity = 10000;private const int m_capacity = 10000;private Shape[] storage = new Shape[m_capacity];private Shape[] storage = new Shape[m_capacity];……public void Sort()public void Sort(){{

Shape tmp;Shape tmp; for (int i=0; i<m_capacity-1; i++) {for (int i=0; i<m_capacity-1; i++) {

for (int j=0; j<m_capacity-1-i; j++)for (int j=0; j<m_capacity-1-i; j++) if (if (storage[j+1].Volume < storage[j].Volumestorage[j+1].Volume < storage[j].Volume){ ){

tmp = storage[j]; tmp = storage[j]; storage[j] = storage[j+1];storage[j] = storage[j+1];storage[j+1] = tmp;storage[j+1] = tmp;

}} }}}}

}}

callvirt instance int32 Shape::get_Volume()

Page 12: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

12

Common Language Common Language RuntimeRuntime Call path (sample)Call path (sample)

public class Shape public class Shape

{{

protected int m_volume; protected int m_volume;

public public virtualvirtual int Volume int Volume

{ {

get {return m_volume;}get {return m_volume;}

}}

}}

public class Cube:Shape public class Cube:Shape

{{

public MyType(int vol) public MyType(int vol)

{ {

m_volume = vol;m_volume = vol;

}}

}}

public class Shape public class Shape

{{

protected int m_volume; protected int m_volume;

public int Volume public int Volume

{ {

get {return m_volume;}get {return m_volume;}

}}

}}

public class Cube:Shape public class Cube:Shape

{{

public MyType(int vol) public MyType(int vol)

{ {

m_volume = vol;m_volume = vol;

}}

}}

No virtual call overheadNo virtual call overhead

Inlined (no call overhead Inlined (no call overhead at all)at all)

~ Equal to accessing field~ Equal to accessing field

57 sec57 sec 39 sec39 sec

Page 13: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

13

Common Language Common Language RuntimeRuntime‘The Memory Bill’‘The Memory Bill’

Shared by all .NET applications runningShared by all .NET applications running.NET Compact Framework CLR DLLs.NET Compact Framework CLR DLLs.NET assemblies (memory mapped).NET assemblies (memory mapped)

Dynamic, per process memory costsDynamic, per process memory costsObjects allocatedObjects allocatedThreads stacksThreads stacksNumber of classes and methodsNumber of classes and methods

Runtime representation of metadataRuntime representation of metadataJIT compiled codeJIT compiled code

Unmanaged allocations (not under control of Unmanaged allocations (not under control of the CLR)the CLR)

Operating SystemOperating SystemNative DLLs called by application via P/InvokeNative DLLs called by application via P/Invoke

Page 14: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

14

Common Language Common Language RuntimeRuntimeMemory heapsMemory heaps

Five memory heaps to reduce Five memory heaps to reduce fragmentationfragmentation

App-domainApp-domain CLR dynamic representation of CLR dynamic representation of metadata for the assembly loadermetadata for the assembly loader

JITJIT JIT compiled code buffersJIT compiled code buffers

Garbage Collector Garbage Collector (GC)(GC)

Application and Framework object Application and Framework object allocationsallocations

Short-termShort-term CLR temporary/short lived allocation CLR temporary/short lived allocation heapheap

ProcessProcess Other CLR allocationsOther CLR allocations

Page 15: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

15

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

0 25 50 75 100 125 150Time (seconds)

Mem

ory

(M

B)

GC Heap

J IT Heap

Managed Bytes Allocated

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

0 25 50 75 100 125 150Time (seconds)

Mem

ory

(M

B)

GC Heap Usage

Going Into The Going Into The BackgroundBackgroundYahtzee gameYahtzee game

Application goesApplication goesinto background into background or low on memoryor low on memory

Page 16: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

16

Real World MeasurementsReal World MeasurementsYahtzee gameYahtzee game

WhereWhere What is the memory?What is the memory? PeakPeak‘‘On On MinimizeMinimize’’

Shared,Shared,

RO RO Demand Demand PagedPaged

Code, .NET Code, .NET AssembliesAssemblies

Mscoree.dll, mscoree2_0.dll,Mscoree.dll, mscoree2_0.dll,netcfagl2_0.dllnetcfagl2_0.dll 1MB1MB 500KB500KB

Mscorlib, Yahtzee.exe, system, Mscorlib, Yahtzee.exe, system, System.Drawing,System.Drawing,System.Windows.FormsSystem.Windows.Forms

1.7MB1.7MB 1MB1MB

Process Process MemoryMemory

CLR HeapsCLR Heaps

JIT HeapJIT Heap 220KB220KB 30KB30KB

Process HeapProcess Heap 47KB47KB 11KB11KB

App Domain HeapApp Domain Heap 177KB177KB 177KB177KB

ApplicationApplication GC Heap – object allocationsGC Heap – object allocations 1MB1MB 64KB64KB

TotalTotal 1.5MB1.5MB 282KB282KB

TotalTotal 4.2MB4.2MB 1.7MB1.7MB

Page 17: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

17

Common Language Common Language RuntimeRuntime Garbage Collector (GC)Garbage Collector (GC)

Managed allocations are FASTManaged allocations are FAST7.5MB per sec (allocating 8 byte objects)7.5MB per sec (allocating 8 byte objects)

GC manages it’s own heapGC manages it’s own heapAllocates 64KB blocks, 1MB cacheAllocates 64KB blocks, 1MB cache

Use VirtualAlloc to enable release of virtual and Use VirtualAlloc to enable release of virtual and physical memory back to systemphysical memory back to system

Compacts heap when fragmentation occursCompacts heap when fragmentation occurs

Page 18: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

18

Common Language Common Language RuntimeRuntimeGarbage CollectorGarbage Collector

What triggers a GC?What triggers a GC?Memory allocation failureMemory allocation failure1M of GC objects allocated (v2)1M of GC objects allocated (v2)Application going to backgroundApplication going to backgroundGC.Collect() (AGC.Collect() (Avoid “helping” the GC!)void “helping” the GC!)

In general, if you don’t allocate objects, GC won’t In general, if you don’t allocate objects, GC won’t occuroccur

Beware of side-effects of calls that may allocateBeware of side-effects of calls that may allocate objectsobjectsWhat happens at GC time?What happens at GC time?

Freezes all threads at Freezes all threads at safesafe point pointFinds all Finds all livelive objects and marks them objects and marks them

An object is An object is livelive if it is if it is reachablereachable from from root locationroot locationUnmarked objects are freed and added to finalizer queueUnmarked objects are freed and added to finalizer queue

Finalizers are run on a separate threadFinalizers are run on a separate threadGC pools are compacted if required GC pools are compacted if required Return free memory to the operating systemReturn free memory to the operating system

Page 19: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

19

Common Language Common Language RuntimeRuntimeGarbage CollectorGarbage Collector

0

10

20

30

40

50

60

70

80

90

0 100000 300000 500000

Number of Live Objects

GC

late

nc

y (

ms

)

GC Latency per collectionGC Latency per collection

Page 20: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

20

Common Language Common Language RuntimeRuntimeGarbage CollectorGarbage Collector

0

20000

40000

60000

80000

100000

120000

140000

160000

400 4000 20000 40000 80000

Object size (bytes)

Allo

cati

on

rate

ite

r/sec

Allocation rateAllocation rate

Page 21: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

21

Unnecessary string allocationsUnnecessary string allocationsStrings are immutable Strings are immutable

String manipulations (Concat(), etc.) cause String manipulations (Concat(), etc.) cause copies copies Use StringBuilder Use StringBuilder http://weblogs.asp.net/ricom/archive/2003/12/02/40778.ahttp://weblogs.asp.net/ricom/archive/2003/12/02/40778.aspxspx

Common Language Common Language RuntimeRuntimeWhere garbage comes from?Where garbage comes from?

String result = "";String result = "";

for (int i=0; i<10000; i++) {for (int i=0; i<10000; i++) {

result += result +=

".NET Compact Framework";".NET Compact Framework";

result += " Rocks!";result += " Rocks!";

} }

StringBuilder result = StringBuilder result =

new StringBuilder();new StringBuilder();

for (int i=0; i<10000; i++){for (int i=0; i<10000; i++){

result.Append(".NET Compact result.Append(".NET Compact Framework");Framework");

result.Append(" Rocks!");result.Append(" Rocks!");

}}

Page 22: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

22

.stat.statcounter total last datum n mean min maxTotal Program Run Time (ms) 11843 - - - - -App Domains Created 1 - - - - -App Domains Unloaded 1 - - - - -Assemblies Loaded 2 - - - - -Classes Loaded 175 - - - - -Methods Loaded 198 - - - - -Closed Types Loaded 0 - - - - -Closed Types Loaded per Definition 0 0 0 0 0 0Open Types Loaded 0 - - - - -Closed Methods Loaded 0 - - - - -Closed Methods Loaded per Definition 0 0 0 0 0 0Open Methods Loaded 0 - - - - -Threads in Thread Pool - 0 2 0 0 1Pending Timers - 0 2 0 0 1Scheduled Timers 1 - - - - -Timers Delayed by Thread Pool Limit 0 - - - - -Work Items Queued 1 - - - - -Uncontested Monitor.Enter Calls 2 - - - - -Contested Monitor.Enter Calls 0 - - - - -Peak Bytes Allocated (native + managed) 3326004 - - - - -Managed Objects Allocated 60266 - - - - -Managed Bytes Allocated 5801679432 28 60266 96267 8 580020Managed String Objects Allocated 20041 - - - - -Bytes of String Objects Allocated 5800480578 - - - - -Garbage Collections (GC) 4912 - - - - -Bytes Collected By GC 5918699036 1160076 4912 1204946 597824 1572512Managed Bytes In Use After GC - 580752 4912 381831 8364 580752Total Bytes In Use After GC - 1810560 4912 1611885 1097856 1810560GC Compactions 0 - - - - -Code Pitchings 0 - - - - -Calls to GC.Collect 0 - - - - -GC Latency Time (ms) 686 0 4912 0 0 16Pinned Objects 0 - - - - -Objects Moved by Compactor 0 - - - - -Objects Not Moved by Compactor 0 - - - - -Objects Finalized 1 - - - - -Boxed Value Types 3 - - - - -Process Heap - 278 235 2352 68 8733Short Term Heap - 0 278 986 0 10424JIT Heap - 0 360 12103 0 24444App Domain Heap - 0 1341 46799 0 64562GC Heap - 0 35524 2095727 0 3276800Native Bytes Jitted 22427 140 98 228 68 1367Methods Jitted 98 - - - - -Bytes Pitched 0 0 0 0 0 0Methods Pitched 0 - - - - -Method Pitch Latency Time (ms) 0 0 0 0 0 0Exceptions Thrown 0 - - - - -Platform Invoke Calls 0 - - - - -

Managed String Objects AllocatedManaged String Objects Allocated 2004020040Garbage Collections (GC)Garbage Collections (GC) 49124912Bytes of String Objects AllocateBytes of String Objects Allocate 5,800,480,5745,800,480,574Bytes Collected By GC Bytes Collected By GC 5,918,699,0365,918,699,036GC latencyGC latency 107128 ms 107128 ms

String result = "";String result = "";for (int i=0; i<10000; i++) {for (int i=0; i<10000; i++) { result += ".NET Compact Framework";result += ".NET Compact Framework"; result += " Rocks!";result += " Rocks!";}}

Run time 173 secRun time 173 sec

Page 23: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

23

counter total last datum n mean min maxTotal Program Run Time (ms) 11843 - - - - -App Domains Created 1 - - - - -App Domains Unloaded 1 - - - - -Assemblies Loaded 2 - - - - -Classes Loaded 175 - - - - -Methods Loaded 198 - - - - -Closed Types Loaded 0 - - - - -Closed Types Loaded per Definition 0 0 0 0 0 0Open Types Loaded 0 - - - - -Closed Methods Loaded 0 - - - - -Closed Methods Loaded per Definition 0 0 0 0 0 0Open Methods Loaded 0 - - - - -Threads in Thread Pool - 0 2 0 0 1Pending Timers - 0 2 0 0 1Scheduled Timers 1 - - - - -Timers Delayed by Thread Pool Limit 0 - - - - -Work Items Queued 1 - - - - -Uncontested Monitor.Enter Calls 2 - - - - -Contested Monitor.Enter Calls 0 - - - - -Peak Bytes Allocated (native + managed) 3326004 - - - - -Managed Objects Allocated 60266 - - - - -Managed Bytes Allocated 5801679432 28 60266 96267 8 580020Managed String Objects Allocated 20041 - - - - -Bytes of String Objects Allocated 5800480578 - - - - -Garbage Collections (GC) 4912 - - - - -Bytes Collected By GC 5918699036 1160076 4912 1204946 597824 1572512Managed Bytes In Use After GC - 580752 4912 381831 8364 580752Total Bytes In Use After GC - 1810560 4912 1611885 1097856 1810560GC Compactions 0 - - - - -Code Pitchings 0 - - - - -Calls to GC.Collect 0 - - - - -GC Latency Time (ms) 686 0 4912 0 0 16Pinned Objects 0 - - - - -Objects Moved by Compactor 0 - - - - -Objects Not Moved by Compactor 0 - - - - -Objects Finalized 1 - - - - -Boxed Value Types 3 - - - - -Process Heap - 278 235 2352 68 8733Short Term Heap - 0 278 986 0 10424JIT Heap - 0 360 12103 0 24444App Domain Heap - 0 1341 46799 0 64562GC Heap - 0 35524 2095727 0 3276800Native Bytes Jitted 22427 140 98 228 68 1367Methods Jitted 98 - - - - -Bytes Pitched 0 0 0 0 0 0Methods Pitched 0 - - - - -Method Pitch Latency Time (ms) 0 0 0 0 0 0Exceptions Thrown 0 - - - - -Platform Invoke Calls 0 - - - - -

.stat.stat

Managed String Objects Allocated 56Managed String Objects Allocated 56Bytes of String Objects Allocated Bytes of String Objects Allocated 20977182097718Garbage Collections (GC) 2Garbage Collections (GC) 2Bytes Collected By GCBytes Collected By GC 10816201081620GC LatencyGC Latency 21 ms21 ms

StringBuilder result = new StringBuilder();StringBuilder result = new StringBuilder();for (int i=0; i<10000; i++){for (int i=0; i<10000; i++){

result.Append(".NET Compact result.Append(".NET Compact Framework");Framework");

result.Append(" Rocks!");result.Append(" Rocks!");}}

Run time 0.1 secRun time 0.1 sec

Page 24: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

24

Common Language Common Language RuntimeRuntimeWhere garbage comes from?Where garbage comes from?

Unnecessary boxingUnnecessary boxingValue types allocated on the stack Value types allocated on the stack (fast to allocate)(fast to allocate)Boxing causes a heap allocation and a copyBoxing causes a heap allocation and a copyUse strongly typed arrays and collectionsUse strongly typed arrays and collections(Framework collections are (Framework collections are NOTNOT strongly typed) strongly typed) class Hashtable {class Hashtable {

struct bucket {struct bucket {Object key;Object key;Object val; Object val;

} } bucket[] buckets;bucket[] buckets;

public public ObjectObject this[ this[Object keyObject key] { get; set; }] { get; set; } }}

Page 25: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

25

Common Language Common Language RuntimeRuntimeSample Code: Value Types and Sample Code: Value Types and boxing boxing

public struct AccountId {public struct AccountId { public int m_number;public int m_number; public override int GetHashCode() { return m_number; }public override int GetHashCode() { return m_number; }}}public struct AccountData {public struct AccountData {

private int m_balance;private int m_balance;

public int Balance {public int Balance {

get {return m_balance;}get {return m_balance;}

set {m_balance=value;} set {m_balance=value;}

}}

}}public class Accounts {public class Accounts {

public const int num = 10000;public const int num = 10000;

Object[] accounts = new Object[] accounts = new

Object[num];Object[num];

public Object this[Object id] {public Object this[Object id] {

get {return get {return accounts[id.GetHashCode()];}accounts[id.GetHashCode()];}

set {accounts[id.GetHashCode()] = set {accounts[id.GetHashCode()] = value;}value;}

}}

}}

public class Accounts {public class Accounts {

public const int num = 10000;public const int num = 10000;

AccountData[] accounts = newAccountData[] accounts = new

AccountData[num];AccountData[num];

public AccountData this[AccountId id] {public AccountData this[AccountId id] {

get {return get {return accounts[id.GetHashCode()];}accounts[id.GetHashCode()];}

set {accounts[id.GetHashCode()] = set {accounts[id.GetHashCode()] = value;}value;}

}}

}}

Page 26: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

26

Common Language Common Language RuntimeRuntimeSample Code: Value Types and Sample Code: Value Types and boxing boxing

Accounts ac = new Accounts(); int i;Accounts ac = new Accounts(); int i;for (i = 0; i < Accounts.num_accounts; i++) {for (i = 0; i < Accounts.num_accounts; i++) {

AccountData rec = new AccountData();AccountData rec = new AccountData();rec.Balance = 100;rec.Balance = 100;AccountId id; id.m_number = i;AccountId id; id.m_number = i;ac[id] = rec;ac[id] = rec;

}}long iterations = 0;long iterations = 0;long start = Environment.TickCount;long start = Environment.TickCount;do {do {

for (i = 0; i < Accounts.num_accounts; i++) {for (i = 0; i < Accounts.num_accounts; i++) {AccountId id; id.m_number = i;AccountId id; id.m_number = i;AccountData rec = (AccountData)ac[ id ]; AccountData rec = (AccountData)ac[ id ]; rec.Balance-=10;rec.Balance-=10;ac[ id ]=rec;ac[ id ]=rec;

}}iterations += i;iterations += i;

} while (Environment.TickCount - start < 1000);} while (Environment.TickCount - start < 1000);

Page 27: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

27

Common Language Common Language RuntimeRuntimeSample Code: Value Types and Sample Code: Value Types and boxing boxing

public class Accounts {public class Accounts {

public const int num = 10000;public const int num = 10000;

Object[] accounts = new Object[] accounts = new

Object[num];Object[num];

public Object this[Object id] {public Object this[Object id] {

get {return get {return accounts[id.GetHashCode()];}accounts[id.GetHashCode()];}

set {accounts[id.GetHashCode()] = set {accounts[id.GetHashCode()] = value;}value;}

}}

}}

public class Accounts {public class Accounts {

public const int num = 10000;public const int num = 10000;

AccountData[] accounts=newAccountData[] accounts=new

AccountData[num];AccountData[num];

public AccountData this[AccountId id] {public AccountData this[AccountId id] {

get {return get {return accounts[id.GetHashCode()];}accounts[id.GetHashCode()];}

set {accounts[id.GetHashCode()] = set {accounts[id.GetHashCode()] = value;}value;}

}}

}}

0.15M iter/sec0.15M iter/sec

Boxed value types 4138460Boxed value types 4138460Garbage Collections (GC) 4 Garbage Collections (GC) 4 Bytes Collected By GC Bytes Collected By GC 41384604138460GC Latency TimeGC Latency Time 132 ms 132 ms

2.5M iter/sec2.5M iter/sec

Boxed value types 2Boxed value types 2Garbage Collections (GC) 0 Garbage Collections (GC) 0 Bytes Collected By GC 0Bytes Collected By GC 0GC Latency TimeGC Latency Time 0 ms 0 ms

Page 28: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

28

Common Language Common Language RuntimeRuntimeSample Code: GenericsSample Code: Genericspublic class Accounts<U, V>public class Accounts<U, V>{{

public const int num_accounts = 10000;public const int num_accounts = 10000;private U[] accounts = new U[num_accounts];private U[] accounts = new U[num_accounts];public U this[V id] {public U this[V id] {

get {return accounts[id.GetHashCode()];}get {return accounts[id.GetHashCode()];}set {accounts[id.GetHashCode()] = value;}set {accounts[id.GetHashCode()] = value;}

}}}}

Accounts<AccountData, AccountId> ac = new Accounts<AccountData, AccountId>();Accounts<AccountData, AccountId> ac = new Accounts<AccountData, AccountId>();int i;int i;for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {

AccountData rec = new AccountData(); rec.Balance = 100;AccountData rec = new AccountData(); rec.Balance = 100;AccountId id; id.m_number = i;AccountId id; id.m_number = i;ac[id] = rec;ac[id] = rec;

}}long iterations = 0; long start = Environment.TickCount;long iterations = 0; long start = Environment.TickCount;do {do { for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {

AccountId id; id.m_number = i; AccountData rec = (AccountData)ac[id]; AccountId id; id.m_number = i; AccountData rec = (AccountData)ac[id]; rec.Balance-=10; ac[id]=rec;rec.Balance-=10; ac[id]=rec;

}} iterations += i;iterations += i;} while (Environment.TickCount - start < 1000);} while (Environment.TickCount - start < 1000);

Page 29: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

29

Common Language Common Language RuntimeRuntimeSample Code: GenericsSample Code: Genericspublic class Accounts<U, V>public class Accounts<U, V>{{

public const int num_accounts = 10000;public const int num_accounts = 10000;private U[] accounts = new U[num_accounts];private U[] accounts = new U[num_accounts];public U this[V id] {public U this[V id] {

get {return get {return accounts[id.GetHashCode()];}accounts[id.GetHashCode()];}

set {accounts[id.GetHashCode()] = set {accounts[id.GetHashCode()] = value;}value;}

}}}}

Accounts<AccountData, AccountId> ac = new Accounts<AccountData, AccountId>();Accounts<AccountData, AccountId> ac = new Accounts<AccountData, AccountId>();int i;int i;for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {

AccountData rec = new AccountData(); rec.Balance = 100;AccountData rec = new AccountData(); rec.Balance = 100;AccountId id; id.m_number = i;AccountId id; id.m_number = i;ac[id] = rec;ac[id] = rec;

}}long iterations = 0; long start = Environment.TickCount;long iterations = 0; long start = Environment.TickCount;do {do { for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {for (i = 0; i < Accounts<AccountData, AccountId>.num_accounts; i++) {

AccountId id; id.m_number = i; AccountData rec = (AccountData)ac[id]; rec.Balance-=10; ac[id]=rec;AccountId id; id.m_number = i; AccountData rec = (AccountData)ac[id]; rec.Balance-=10; ac[id]=rec; }} iterations += i;iterations += i;} while (Environment.TickCount - start < 1000);} while (Environment.TickCount - start < 1000);

Untyped Untyped 0.15M iter/sec 0.15M iter/sec

Strongly typed 2.5M iter/secStrongly typed 2.5M iter/sec

Generic Generic 2.5M iter/sec 2.5M iter/sec

Page 30: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

30

counter total last datum n mean min maxTotal Program Run Time (ms) 11843 - - - - -App Domains Created 1 - - - - -App Domains Unloaded 1 - - - - -Assemblies Loaded 2 - - - - -Classes Loaded 175 - - - - -Methods Loaded 198 - - - - -Closed Types Loaded 0 - - - - -Closed Types Loaded per Definition 0 0 0 0 0 0Open Types Loaded 0 - - - - -Closed Methods Loaded 0 - - - - -Closed Methods Loaded per Definition 0 0 0 0 0 0Open Methods Loaded 0 - - - - -Threads in Thread Pool - 0 2 0 0 1Pending Timers - 0 2 0 0 1Scheduled Timers 1 - - - - -Timers Delayed by Thread Pool Limit 0 - - - - -Work Items Queued 1 - - - - -Uncontested Monitor.Enter Calls 2 - - - - -Contested Monitor.Enter Calls 0 - - - - -Peak Bytes Allocated (native + managed) 3326004 - - - - -Managed Objects Allocated 60266 - - - - -Managed Bytes Allocated 5801679432 28 60266 96267 8 580020Managed String Objects Allocated 20041 - - - - -Bytes of String Objects Allocated 5800480578 - - - - -Garbage Collections (GC) 4912 - - - - -Bytes Collected By GC 5918699036 1160076 4912 1204946 597824 1572512Managed Bytes In Use After GC - 580752 4912 381831 8364 580752Total Bytes In Use After GC - 1810560 4912 1611885 1097856 1810560GC Compactions 0 - - - - -Code Pitchings 0 - - - - -Calls to GC.Collect 0 - - - - -GC Latency Time (ms) 686 0 4912 0 0 16Pinned Objects 0 - - - - -Objects Moved by Compactor 0 - - - - -Objects Not Moved by Compactor 0 - - - - -Objects Finalized 1 - - - - -Boxed Value Types 3 - - - - -Process Heap - 278 235 2352 68 8733Short Term Heap - 0 278 986 0 10424JIT Heap - 0 360 12103 0 24444App Domain Heap - 0 1341 46799 0 64562GC Heap - 0 35524 2095727 0 3276800Native Bytes Jitted 22427 140 98 228 68 1367Methods Jitted 98 - - - - -Bytes Pitched 0 0 0 0 0 0Methods Pitched 0 - - - - -Method Pitch Latency Time (ms) 0 0 0 0 0 0Exceptions Thrown 0 - - - - -Platform Invoke Calls 0 - - - - -

.stat.stat

Boxed value types 2Boxed value types 2Garbage Collections (GC) 0 Garbage Collections (GC) 0 Bytes Collected By GC 0Bytes Collected By GC 0GC Latency TimeGC Latency Time 0 ms 0 ms

Closed Types Loaded 1 Closed Types Loaded 1 Closed Types per definition mean=1 Closed Types per definition mean=1 max=1max=1

Page 31: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

31

Common Language Common Language RuntimeRuntimeGenericsGenericsStrong typing without code duplicationStrong typing without code duplication

Fully specialized implementation in .NET Fully specialized implementation in .NET Compact Framework v2Compact Framework v2

ProsProsAlways strongly typedAlways strongly typed

No unnecessary boxing and type castsNo unnecessary boxing and type casts

Specialized code is more efficient than sharedSpecialized code is more efficient than shared

ConsConsInternal execution engine data structures and JIT-Internal execution engine data structures and JIT-compiled code aren’t sharedcompiled code aren’t shared

List<int>, List<string>, List<MyType>List<int>, List<string>, List<MyType>

http://blogs.msdn.com/romanbat/archive/2005/0http://blogs.msdn.com/romanbat/archive/2005/01/06/348114.aspx1/06/348114.aspx

Page 32: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

32

Common Language Common Language RuntimeRuntimeFinalization and DisposeFinalization and Dispose

Cost of finalizersCost of finalizersNon-deterministic cleanupNon-deterministic cleanup

Extends lifetime of objectExtends lifetime of object

In general, rely on GC for automatic memory In general, rely on GC for automatic memory cleanupcleanup

The exceptions to the rule…The exceptions to the rule…If your object contains an unmanaged resource that the If your object contains an unmanaged resource that the GC is unaware of, you need to implement a finalizerGC is unaware of, you need to implement a finalizer

Also implement Dispose pattern to release unmanaged Also implement Dispose pattern to release unmanaged resource in deterministic mannerresource in deterministic manner

Dispose method should suppress finalization (FxCop rule)Dispose method should suppress finalization (FxCop rule)

If the object you are using implements Dispose, call it If the object you are using implements Dispose, call it when you are done with the objectwhen you are done with the object

‘‘Objects Finalized’ performance counterObjects Finalized’ performance counter

Page 33: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

33

Common Language Common Language RuntimeRuntimeExceptionsExceptions

Exceptions are cheap…until you throwExceptions are cheap…until you throw

Throw exceptions in exceptional Throw exceptions in exceptional circumstancescircumstances

Do not use exceptions for normal flow Do not use exceptions for normal flow controlcontrol

Use performance counters to track the Use performance counters to track the number of exceptions thrownnumber of exceptions thrown

Replace “On Error/Goto” with Replace “On Error/Goto” with “Try/Catch/Finally” in Microsoft Visual Basic “Try/Catch/Finally” in Microsoft Visual Basic .NET.NET

Page 34: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

34

Common Language Common Language RuntimeRuntimeReflectionReflection

Reflection can be expensiveReflection can be expensive

Reflection performance costReflection performance costType comparisons (for example: typeof() )Type comparisons (for example: typeof() )

Member access (for example: Type.InvokeMember())Member access (for example: Type.InvokeMember())Think ~10-100x slowerThink ~10-100x slower

Working set costWorking set costType and Member enumerations (for example: Type and Member enumerations (for example: Assembly.GetTypes(), Type.GetMethods())Assembly.GetTypes(), Type.GetMethods())

Runtime data structures Runtime data structures Think ~100 bytes per loaded type, ~80 bytes per loaded methodThink ~100 bytes per loaded type, ~80 bytes per loaded method

Be aware of APIs that use reflection as a side effectBe aware of APIs that use reflection as a side effect

Override Override Object.ToString() Object.ToString()

GetHashCode() and Equals() (for value types)GetHashCode() and Equals() (for value types)

Page 35: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

35

Common Language Common Language RuntimeRuntimeBuilding a Cost Model for Managed Building a Cost Model for Managed MathMathMath performanceMath performance

32 bit integers: Similar to native math32 bit integers: Similar to native math

64 bit integers: ~5-10X cost of native 64 bit integers: ~5-10X cost of native mathmath

Floating point: Similar to native mathFloating point: Similar to native mathARM processors do not have FPU ARM processors do not have FPU

Page 36: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

36

FXFX

.NET Compact Framework.NET Compact Framework

CLRCLR

Windows CEWindows CE

Globalization GUINetI/OCrypto

System.System.GlobalizationGlobalization

System.System.CryptographyCryptography

System.System.IO.PortsIO.Ports

Microsoft.Microsoft.Win32.RegistryWin32.Registry

System.IO.System.IO.FileFile

System.System.DataData

System.XmlSystem.Xml

SystemSystem

mscorlibmscorlib

Microsoft.Microsoft.VisualBasicVisualBasic

JIT CompilerJIT Compiler& GC& GCDebuggerDebugger

ClassClassLoaderLoader

AssemblyAssemblyCacheCache

NativeNativeInteropInterop

App DomainApp DomainLoaderLoader

ProcessProcessLoaderLoader

Memory and Memory and ThreadingThreading

File MappingFile MappingCert/SecurityCert/SecurityVerificationVerification

System.System.WebServicesWebServices

System.Net.System.Net.Http*Http*

System.Net.System.Net.SocketsSockets

DirectX.DirectX.DirectD3DMDirectD3DM

Windows.Windows.FormsForms

System.System.DrawingDrawing

SSLSSL

SocketsSockets

NTLMNTLM

GDI/GWESGDI/GWES

CommonCommonControlsControls

RegistryRegistry

File I/OFile I/O

EncodingsEncodings

SortingSorting Crypto APICrypto API

CalendarCalendarDataData

CultureCultureDataData

RedistRedist

HostHost

Visual StudioVisual Studio

Debug EngineDebug Engine

ICorDbgICorDbg

Managed LoaderManaged Loader

MSI SetupMSI Setup(ActiveSync)(ActiveSync)

Per Device CABPer Device CABInstall (SMS, etc)Install (SMS, etc)

System.System.ReflectionReflection

CasingCasing D3DMD3DM

Page 37: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

37

Base Class LibraryBase Class LibraryCollectionsCollections

Pre-size collection classes Pre-size collection classes appropriatelyappropriately

Default capacity is small (for example 4 Default capacity is small (for example 4 for ArrayList)for ArrayList)

Resizing creates unnecessary copiesResizing creates unnecessary copies

Avoid unnecessary boxing and type Avoid unnecessary boxing and type casts – use generic collectionscasts – use generic collections

Full support for all generic collections in Full support for all generic collections in the .NET Compact Framework v2!the .NET Compact Framework v2!

Page 38: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

38

Windows FormsWindows FormsBest PracticesBest Practices

Load and cache Forms in the backgroundLoad and cache Forms in the backgroundPopulate data separate from Form.Show()Populate data separate from Form.Show()

Pre-populate data, orPre-populate data, orLoad data async to Form.Show()Load data async to Form.Show()

Use BeginUpdate/EndUpdate when it is availableUse BeginUpdate/EndUpdate when it is availablee.g. ListView, TreeViewe.g. ListView, TreeView

Use SuspendLayout/ResumeLayout when Use SuspendLayout/ResumeLayout when repositioning controlsrepositioning controlsKeep event handling code tightKeep event handling code tight

Process bigger operations asynchronouslyProcess bigger operations asynchronouslyBlocking in event handlers will affect UI responsivenessBlocking in event handlers will affect UI responsiveness

Form load performanceForm load performanceReduce the number of method calls during initializationReduce the number of method calls during initialization

Page 39: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

39

Graphics And GamesGraphics And GamesBest PracticesBest Practices

Compose to off-screen buffers to Compose to off-screen buffers to minimize direct to screen blittingminimize direct to screen blitting

Approximately 50% fasterApproximately 50% faster

Avoid transparent blitting in areas Avoid transparent blitting in areas that require performancethat require performance

Approximate 1/3 speed of normal blittingApproximate 1/3 speed of normal blitting

Consider using pre-rendered images Consider using pre-rendered images vs using System.Drawing rendering vs using System.Drawing rendering primitivesprimitives

Need to measure on a case-by-case Need to measure on a case-by-case basisbasis

Page 40: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

40

XMLXMLBest Practices for Managing Large XML Data Best Practices for Managing Large XML Data FilesFilesUse XMLTextReader/XMLTextWriterUse XMLTextReader/XMLTextWriter

Smaller memory footprint than using XmlDocumentSmaller memory footprint than using XmlDocumentXmlTextReader is a pull model parser which only reads a XmlTextReader is a pull model parser which only reads a “window” of the data“window” of the dataXmlDocument builds a generic, untyped object model XmlDocument builds a generic, untyped object model using a treeusing a tree

Type stored as stringType stored as stringOK to use with smaller documents (64K XML: ~0.25s)OK to use with smaller documents (64K XML: ~0.25s)

Optimize the structure of XML documentOptimize the structure of XML document Use elements to group (allows use of Skip() in XmlReader)Use elements to group (allows use of Skip() in XmlReader)Use attributes to reduce size - processing attribute-centric Use attributes to reduce size - processing attribute-centric documents is fasterdocuments is faster Keep it short! (attribute and element names)Keep it short! (attribute and element names)Avoid gratuitous use of white spaceAvoid gratuitous use of white space

Use XmlReader/XmlWriter factory classes to create Use XmlReader/XmlWriter factory classes to create optimized reader or writeroptimized reader or writer

Applying proper XMLReaderSettings can improve Applying proper XMLReaderSettings can improve performanceperformance

Page 41: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

41

DataDataBusiness logicBusiness logicand presentationand presentation

GUI GUI controlscontrols

In-memoryIn-memorydata copydata copy

Custom dataCustom dataStructures Structures

arrays, collectionsarrays, collectionsDataSetDataSet

SerializationSerializationCustom Custom binarybinary XmlSerializerXmlSerializer IXmlSerializableIXmlSerializable

File systemFile system

TransportsTransports Active Active SyncSync HTTPHTTP SocketsSockets ReplicationReplication

Or RDAOr RDA

Remote systemRemote system

XmlDocumentXmlDocument

BusinessBusinesslogiclogic

DataDataAdaptersAdapters

WebWebservicesservices

DataDataReadersReadersBinary orBinary or

text filetext fileXMLXMLfilefile

SQLSQLServerServerMobile Mobile

SQLSQLDBDB

OtherOtherDataData

Sources Sources

MSMQMSMQ

OtherOtherDBDB

Page 42: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

42

DataDataBusiness logicBusiness logicand presentationand presentation

GUI GUI controlscontrols

In-memoryIn-memorydata copydata copy

Custom dataCustom dataStructures Structures

arrays, collectionsarrays, collectionsDataSetDataSet

SerializationSerializationCustom Custom binarybinary XmlSerializerXmlSerializer IXmlSerializableIXmlSerializable

File systemFile system

XmlDocumentXmlDocument

BusinessBusinesslogiclogic

Binary orBinary ortext filetext file

XMLXMLfilefile

SQLSQLServerServerMobile Mobile

SqlCEResultSetSqlCEResultSetDataReaderDataReader

Page 43: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

43

DataDataBusiness logicBusiness logicand presentationand presentation

GUI GUI controlscontrols

In-memoryIn-memorydata copydata copy

Custom dataCustom dataStructures Structures

arrays, collectionsarrays, collectionsDataSetDataSet

SerializationSerializationCustom Custom binarybinary XmlSerializerXmlSerializer IXmlSerializableIXmlSerializable

File systemFile system

TransportsTransports Active Active SyncSync HTTPHTTP SocketsSockets ReplicationReplication

Or RDAOr RDA

Remote systemRemote system

XmlDocumentXmlDocument

BusinessBusinesslogiclogic

DataDataAdaptersAdapters

WebWebservicesservices

DataDataReadersReadersBinary orBinary or

text filetext fileXMLXMLfilefile

SQLSQLServerServerMobile Mobile

SQLSQLDBDB

OtherOtherDataData

Sources Sources

MSMQMSMQ

OtherOtherDBDB

Page 44: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

44

Web ServicesWeb ServicesWhere is a bottleneckWhere is a bottleneck

Are you network bound or CPU bound?Are you network bound or CPU bound?Use perf counters: socket bytes sent / received. Use perf counters: socket bytes sent / received. Do you come close to the network capacity? Do you come close to the network capacity?

If you are network bound - work on reducing the size If you are network bound - work on reducing the size of of the messagethe message

Create a “canned” message, send over HTTP. Create a “canned” message, send over HTTP. Compare performance with the web service.Compare performance with the web service.

If you are CPU bound, optimize the serialization If you are CPU bound, optimize the serialization scheme scheme for speedfor speed

http://blogs.msdn.com/mikezintel/archive/2005/http://blogs.msdn.com/mikezintel/archive/2005/03/30/403941.aspx03/30/403941.aspx

Page 45: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

45

Moving ForwardMoving Forward

More toolsMore toolsLive Remote Performance CountersLive Remote Performance Counters

Under constructionUnder constructionAllocation profiler (CLR profiler)Allocation profiler (CLR profiler)Call profilerCall profiler

Working set improvementsWorking set improvementsMore speedMore speed

Page 46: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

46

SummarySummary

Make performance a requirement Make performance a requirement and measureand measure

Understand the APIsUnderstand the APIs

Avoid unnecessary object allocation Avoid unnecessary object allocation and copies due toand copies due to

String manipulationsString manipulations

BoxingBoxing

Not pre-sized collectionsNot pre-sized collections

Understand data access performance Understand data access performance bottlenecks bottlenecks

Page 47: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

47

Community ResourcesCommunity Resources

At PDCAt PDCILL03 Intelligent Data Synchronization in a Semi-ILL03 Intelligent Data Synchronization in a Semi-Connected EnvironmentConnected EnvironmentILL04 Write Once, Display Anywhere: UI for Windows ILL04 Write Once, Display Anywhere: UI for Windows Mobile DevicesMobile DevicesTLN316 Windows Mobile: New Emulation Technology TLN316 Windows Mobile: New Emulation Technology for Building Mobile Applications with Visual Studio 2005for Building Mobile Applications with Visual Studio 2005

After PDCAfter PDCMSDN dev center: MSDN dev center: http://msdn.microsoft.com/mobility/http://msdn.microsoft.com/mobility/.NET Compact Framework Team Blog: .NET Compact Framework Team Blog: http://blogs.msdn.com/netcfteam/http://blogs.msdn.com/netcfteam/.NET Compact Framework Performance FAQ: .NET Compact Framework Performance FAQ: http://blogs.msdn.com/netcfteam/archive/2005/http://blogs.msdn.com/netcfteam/archive/2005/05/04/414820.aspx05/04/414820.aspx

Page 48: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

48

Your FeedbackYour Feedbackis Important!is Important!

Please Fill Out a SurveyPlease Fill Out a Survey

Page 49: 1.NET Compact Framework 2.0 Optimizing For Performance Roman Batoukov FUN403 Development Lead.NET Compact Framework Microsoft Corporation.

49

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