48
Developing high Developing high performance performance applications applications with .NET Compact with .NET Compact Framework Framework Deepak Gulati Deepak Gulati ISV Developer Evangelist ISV Developer Evangelist Microsoft Microsoft

Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Embed Size (px)

Citation preview

Page 1: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Developing high Developing high performance applications performance applications with .NET Compact with .NET Compact FrameworkFramework

Deepak GulatiDeepak GulatiISV Developer EvangelistISV Developer EvangelistMicrosoftMicrosoft

Page 2: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

MManagementanagementTToolsools

CCommunicationsommunications& & MMessagingessaging

Device Update Agent

Software Update Services

Live Communications Server

Exchange Server

Internet Security and Acceleration Server

Speech Server

Image Update

LLocation ocation SServiceservices

MMultimediaultimedia

MapPoint

DirectX

Windows Media

Visual Studio 2005DDevelopment evelopment TToolsools

MFC 8.0, ATL 8.0

Win32NNativeative

MManagedanaged

SServer erver SSideide

LLightweightightweight

RRelationalelationalSQL Server 2005 Express EditionEDB

DDa

taata

PPro

gra

mm

ing

ro

gra

mm

ing

MM

od

el

od

el

DDevice evice BBuilding uilding TToolsools

HHardware/ardware/DDriversrivers

Windows XP DDK

Windows Embedded Studio

Platform Builder

OEM/IHV SuppliedBSP

(ARM, SH4, MIPS)OEM Hardware and Standard Drivers

Standard PC Hardware and Drivers

SQL Server 2005SQL Server 2005 Mobile Edition

ASP.NET Mobile Controls ASP.NET

.NET Compact Framework .NET Framework

Microsoft Operations Manager

Systems Management Server

Page 3: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

Basic technique involves:Basic technique involves:Find start timeFind start time

Find end timeFind end time

Calculate deltaCalculate delta

Page 4: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

Start and End times can be measured Start and End times can be measured in various waysin various ways

GetTickCount, a Win32 API function GetTickCount, a Win32 API function

Environment.TickCount is its managed Environment.TickCount is its managed code equivalentcode equivalent

Both return int that represents time in ms that Both return int that represents time in ms that has passed since the device was bootedhas passed since the device was booted

Can also use System.DateTime and get Can also use System.DateTime and get System.TimeSpan by subtracting Start System.TimeSpan by subtracting Start and End valuesand End values

Page 5: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

There can be issues with these There can be issues with these techniques:techniques:

For a device that has been on for a long For a device that has been on for a long time, TickCount clips and goes negativetime, TickCount clips and goes negative

Not great for measuring ‘short’ operations, Not great for measuring ‘short’ operations, there can be a variation of upto 500 ms there can be a variation of upto 500 ms

System.Date also suffers from accuracy System.Date also suffers from accuracy issuesissues

Page 6: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

QueryPerformanceCounter/QueryPerformanceCounter/QueryPerformanceFrequency to the QueryPerformanceFrequency to the rescue!rescue!

High resolution timer – OEM specific High resolution timer – OEM specific implementationimplementation

Defaults to GetTickCount if not availableDefaults to GetTickCount if not available

Page 7: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

No managed implementation available for No managed implementation available for QueryPerformanceCounter or FrequencyQueryPerformanceCounter or Frequency

PInvoke QueryPerformanceFrequency and PInvoke QueryPerformanceFrequency and get the clock frequency of the device/sec. get the clock frequency of the device/sec. Divide by 1000 to get the clock frequency/msDivide by 1000 to get the clock frequency/ms

PInvoke QueryPerformanceCounter before PInvoke QueryPerformanceCounter before your call. Make your call. PInvoke your call. Make your call. PInvoke QueryPerformanceCounter againQueryPerformanceCounter again

End – Start / frequency/ms will give you time End – Start / frequency/ms will give you time for your call in msfor your call in ms

Page 8: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

DemoDemoUsing QueryPerformanceCounterUsing QueryPerformanceCounter

Page 9: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformanceOverviewOverview

Micro-benchmarks versus ScenariosMicro-benchmarks versus Scenarios

Benchmarking tipsBenchmarking tipsStart from known stateStart from known state

Ensure nothing else is runningEnsure nothing else is running

Measure multiple times, take averageMeasure multiple times, take average

Run each test in own AppDomain / ProcessRun each test in own AppDomain / Process

Log results at the endLog results at the end

Understand JIT-time versus runtime costUnderstand JIT-time versus runtime cost

Page 10: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

.NET Compact Framework.NET Compact Framework

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

(Pocket PC 2003, XScale 400MHz)(Pocket PC 2003, XScale 400MHz) 1.01.0 1.0 SP31.0 SP3V2 V2

Beta1Beta1V2 B2+V2 B2+

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

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

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

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

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 3.3

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

BiggerBiggeris betteris better

SmallerSmalleris betteris better

Page 11: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Measuring PerformanceMeasuring PerformancePerformance CountersPerformance Counters

There will be times when an application runs slow and the code There will be times when an application runs slow and the code looks finelooks fine.NET CompactFramework can be made to report performance .NET CompactFramework can be made to report performance statisticsstatistics

<My App>.stat (formerly mscoree.stat)<My App>.stat (formerly mscoree.stat)http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfperf.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 12: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

DemoDemoEnabling .NET Compact Framework Enabling .NET Compact Framework Performance StatisticsPerformance Statistics

Page 13: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

.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 + managed)

JIT Heap App Domain Heap GC Heap

GC Latency Time (ms)

Garbage Collections (GC)

Managed String Objects Allocated

Boxed Value Types

Page 14: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

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

Portable JIT CompilerPortable JIT CompilerFast code generation, less optimizedFast code generation, less optimized

May pitch JIT-compiled code under May pitch JIT-compiled code under memory pressurememory pressure

No NGen, install time or persisted codeNo NGen, install time or persisted code

Interpreted virtual calls (no v-tables)Interpreted virtual calls (no v-tables)

Simple mark and sweep GC, Simple mark and sweep GC, non generationalnon generational

Page 15: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeExecution 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 callVirtual call: ~1.4X the cost of a managed instance callVirtual call: ~1.4X the cost of a managed instance callPlatform 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 v2OptimizationsOptimizations

Method inlining for simple methodsMethod inlining for simple methodsVariable enregistration Variable enregistration

Page 16: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language Runtime 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;

}}

}}

Page 17: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language Runtime Call 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].Volumestorage[j+1].Volume < < storage[j].Volumestorage[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 18: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language Runtime 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 at all)Inlined (no call overhead at all)

~ Equal to accessing field~ Equal to accessing field

57 sec57 sec 39 sec39 sec

Page 19: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeGarbage 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() (Avoid “helping” the GC!)GC.Collect() (Avoid “helping” the GC!)

What happens at GC time?What happens at GC time?Freezes all threads at safe pointFreezes all threads at safe pointFinds all live objects and marks themFinds all live objects and marks them

An object is live if it is reachable from root locationAn object is live if it is reachable from root 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 (less than 750K of GC pools are compacted if required (less than 750K of free space)free space)Return free memory to the operating systemReturn free memory to the operating system

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

Beware of side-effects of calls that may allocate objectsBeware of side-effects of calls that may allocate objects

http://blogs.msdn.com/stevenpr/archive/2004/07/26/197254.aspxhttp://blogs.msdn.com/stevenpr/archive/2004/07/26/197254.aspx

Page 20: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeGarbage 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 collection

Page 21: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeGarbage 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 rate

Page 22: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeGarbage CollectorGarbage Collector

0

10

20

30

40

50

60

70

80

90

8 400 4000 20000 40000 80000

Object size (bytes)

Allo

cati

on

th

rou

gh

pu

t M

b/s

ec

Allocation throughput

Page 23: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeWhere garbage comes from?Where garbage comes from?

Unnecessary string copiesUnnecessary string copiesStrings are immutable Strings are immutable

String manipulations (Concat(), etc.) String manipulations (Concat(), etc.) cause copies cause copies

Use StringBuilderUse StringBuilder

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 24: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

.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 Allocated 20040Garbage Collections (GC) 4912Bytes of String Objects Allocate 5,800,480,574Bytes Collected By GC 5,918,699,036GC latency 107128 ms

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

Run time 173 secRun time 173 sec

Page 25: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

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 56Bytes of String Objects Allocated 2097718Garbage Collections (GC) 2Bytes Collected By GC 1081620GC Latency 21 ms

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

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

result.Append(" Rocks!");}

Run time 0.1 secRun time 0.1 sec

Page 26: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Last notes on StringBuilderLast notes on StringBuilder

Remember it's all about reducing Remember it's all about reducing memory trafficmemory traffic

If you roughly know the expected If you roughly know the expected length of your final string – allocate length of your final string – allocate that much before hand (StringBuilder that much before hand (StringBuilder constructor)constructor)

Getting the string out of a StringBuilder Getting the string out of a StringBuilder doesn't cause a new alloc, the existing doesn't cause a new alloc, the existing buffer is converted into a stringbuffer is converted into a string

http://weblogs.asp.net/ricom/archive/2003/12/02/40778.aspx

Page 27: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeWhere 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 keyObject key;;Object valObject val; ;

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

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

Page 28: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

DemoDemoString vs. StringBuilderString vs. StringBuilder

Page 29: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeGenericsGenerics

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

ProsProsStrongly typedStrongly 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/01/0http://blogs.msdn.com/romanbat/archive/2005/01/06/348114.aspx6/348114.aspx

Page 30: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeFinalization and DisposeFinalization and Dispose

Cost of finalizersCost of finalizersNon-deterministic cleanupNon-deterministic cleanupExtends lifetime of objectExtends lifetime of object

In general, rely on GC for automatic memory In general, rely on GC for automatic memory cleanupcleanupThe exceptions to the rule…The exceptions to the rule…

If your object contains an unmanaged resource If your object contains an unmanaged resource that the GC is unaware of, you that the GC is unaware of, you need need to implement a to implement a finalizerfinalizer

Also implement Also implement DisposeDispose pattern to release unmanaged pattern to release unmanaged resource in deterministic mannerresource in deterministic mannerDispose Dispose method should suppress finalizationmethod should suppress finalization

If the object you are using implements If the object you are using implements DisposeDispose, , call it when you are done with the objectcall it when you are done with the object

Assumes an unmanaged resource in the object chainAssumes an unmanaged resource in the object chain

Page 31: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeSample Code: Finalization and Dispose Sample Code: Finalization and Dispose class SerialPort : IDisposable { IntPtr SerialPortHandle;

public SerialPort(String name) { // Platform invoke to native code to open serial port SerialPortHandle = SerialOpen(name); }

~SerialPort() { // Platform invoke to native code to close serial port SerialClose(SerialPortHandle); }

public void Dispose() { // Platform invoke to native code to close serial port SerialClose(SerialPortHandle); GC.SuppressFinalize(this); }}

Page 32: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeSample Code: Finalization and DisposeSample Code: Finalization and Dispose

class SerialTrace : IDisposable { SerialPort serialPort;

public SerialTrace() { serialPort = new SerialPort(); }

public void Dispose() { serialPort.Dispose(); }}

Page 33: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeExceptionsExceptions

Exceptions are cheap…until you throwExceptions are cheap…until you throwThrow exceptions in exceptional Throw exceptions in exceptional circumstancescircumstancesDo not use exceptions for normal Do not use exceptions for normal flow controlflow controlUse performance counters to track the Use performance counters to track the number of exceptions thrownnumber of exceptions thrownReplace “On Error/Goto” with Replace “On Error/Goto” with “Try/Catch/Finally” in Microsoft Visual “Try/Catch/Finally” in Microsoft Visual BasicBasic®® .NET .NET

Page 34: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeReflectionReflection

Reflection can be expensiveReflection can be expensiveReflection performance costReflection performance cost

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

Working set costWorking set costRuntime 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 effectOverride Override

Object.ToString() Object.ToString() GetHashCode() and Equals() (for value types)GetHashCode() and Equals() (for value types)

Page 35: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Common Language RuntimeCommon Language RuntimeBuilding a Cost Model for Managed MathBuilding a Cost Model for Managed Math

Math performanceMath performance32 bit integers: Similar to native math32 bit integers: Similar to native math

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

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

Page 36: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

FXFX

.NET Compact Framework.NET Compact Framework

CLRCLR

Windows CEWindows CE

GlobalizationGlobalization GUIGUINetNetI/OI/OCryptoCrypto

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& GC

DebuggerDebugger

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: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Base Class LibraryBase Class LibraryCollectionsCollections

Pre-size collection classes appropriatelyPre-size collection classes appropriatelyResizing creates unnecessary copiesResizing creates unnecessary copies

Beware of foreach overhead, use indexer Beware of foreach overhead, use indexer when availablewhen available

ArrayList al = new ArrayList(string_array);ArrayList al = new ArrayList(string_array); foreach (MyType mt in al){//do something;}foreach (MyType mt in al){//do something;} will be compiled into:will be compiled into: callvirt instance class callvirt instance class

IEnumerator::GetEnumerator()IEnumerator::GetEnumerator() … … callvirt instance object callvirt instance object

IEnumerator::get_Current()IEnumerator::get_Current() … … callvirt instance bool callvirt instance bool

IEnumerator::MoveNext()IEnumerator::MoveNext()

Page 38: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

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 asynchronouslyBlockingBlocking in event handlers will affect UI responsiveness 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: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Graphics And GamesGraphics And GamesBest PracticesBest Practices

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

Approximately 50% fasterApproximately 50% faster

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

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

Consider using pre-rendered images versus Consider using pre-rendered images versus using System.Drawing rendering primitivesusing System.Drawing rendering primitives

Need to measure on a case-by-case basisNeed to measure on a case-by-case basis

Page 40: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

XMLXMLBest Practices for Managing Large XML Data FilesBest Practices for Managing Large XML Data Files

Use XMLTextReader/XMLTextWriterUse XMLTextReader/XMLTextWriter

Smaller memory footprint than using XmlDocumentSmaller memory footprint than using XmlDocument

XmlTextReader 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 data

XmlDocument builds a generic, untyped object model XmlDocument builds a generic, untyped object model using a treeusing a tree

Type stored as stringType stored as string

OK to use with smaller documents (64K XML: ~0.25s)OK to use with smaller documents (64K XML: ~0.25s)

Optimize the structure of XML document Optimize the structure of XML document Use elements to groupUse elements to group

Allows use of Skip() in XmlReaderAllows 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

Page 41: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

XMLXMLCreating optimized Reader/WriterCreating optimized Reader/Writer

In v2 use XmlReader/XmlWriter factory In v2 use XmlReader/XmlWriter factory classes to create optimized reader or writerclasses to create optimized reader or writer

Applying proper XMLReaderSettings can Applying proper XMLReaderSettings can improve performanceimprove performance

XmlReaderSettings settings = new XmlReaderSettings();XmlReaderSettings settings = new XmlReaderSettings();

settings.IgnoreWhitespace = true;settings.IgnoreWhitespace = true;

XmlReader reader = XmlReader.Create(“my.xml”,settings);XmlReader reader = XmlReader.Create(“my.xml”,settings);

Up to 30% performance increase when Up to 30% performance increase when IgnoreWhitespace = true is specified IgnoreWhitespace = true is specified (depends on document format)(depends on document format)

Page 42: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

DemoDemoXmlDocument vs. XmlTextReaderXmlDocument vs. XmlTextReader

Page 43: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

XMLXMLReading local data with DataSetReading local data with DataSet

DataSet is a database independent DataSet is a database independent container of relational datacontainer of relational dataAllows you to work with XMLAllows you to work with XML

ReadXml Allows you to load XML data into ReadXml Allows you to load XML data into DataSetDataSet

Simple to use, but performs badly, Simple to use, but performs badly, especially with large XML filesespecially with large XML filesIf you must use DS.ReadXml, make sure If you must use DS.ReadXml, make sure that you first supply the schemathat you first supply the schemaUse XmlReader whereever possible for Use XmlReader whereever possible for traversing through your datatraversing through your data

Page 44: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

DemoDemoDataSet and .NET CompactFrameworkDataSet and .NET CompactFramework

Page 45: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Non-XML local dataNon-XML local dataReading files locallyReading files locally

It might be required to read text file It might be required to read text file stored locally on the devicestored locally on the device

StreamReader and FileStream classes StreamReader and FileStream classes are typically employedare typically employed

For large file sizes (>100 K), FileStream For large file sizes (>100 K), FileStream outperforms StreamReaderoutperforms StreamReader

StreamReader specifically looks for line-StreamReader specifically looks for line-breaks, FileStream does notbreaks, FileStream does not

Page 46: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

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 the messageof the 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 for speedscheme for speed

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

Page 47: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

Moving ForwardMoving Forward

More toolsMore toolsLive Remote Performance Counters Live Remote Performance Counters (new in v2)(new in v2)

Under construction:Under construction:Allocation profiler (CLR profiler)Allocation profiler (CLR profiler)Call profilerCall profiler

Working set improvementsWorking set improvementsMore speedMore speed

Page 48: Developing high performance applications with.NET Compact Framework Deepak Gulati ISV Developer Evangelist Microsoft

http://blogs.msdn.com/netcfteam/archive/2005/05/04/414820.aspx

SummarySummaryMake performance a requirement Make performance a requirement and measureand measureUnderstand the APIsUnderstand the APIsIsolate exactly what is being measured Isolate exactly what is being measured Repeat tests several times and ignore the first time which is Repeat tests several times and ignore the first time which is affected by JITting affected by JITting Track the results in order for later comparisons and review Track the results in order for later comparisons and review Ensure comparison of Apples to Apples Ensure comparison of Apples to Apples Use real code when possible Use real code when possible Test multiple designs and strategies - Understand the Test multiple designs and strategies - Understand the differences or variation differences or variation Avoid unnecessary object allocation and copies due toAvoid unnecessary object allocation and copies due to

String manipulationsString manipulationsBoxingBoxingNot pre-sized collectionsNot pre-sized collections

Performance FAQPerformance FAQ