C# High Performance Mobile Apps

28
C# High Performance Mobile Apps Laurent Testud

description

C# High Performance Mobile Apps. Laurent Testud. Mobile Platforms vs. PC. CPU: 20% ( 32 bits ) GPU: 10% ( Open GL ES 2.0 – DX 9 ) Storage: 150% ( On PCB SSD ) Network: 5% ( Often less, lost connections ). High Perf ’ Apps. Harry’s Lap Timer - PowerPoint PPT Presentation

Transcript of C# High Performance Mobile Apps

Page 1: C# High Performance  Mobile Apps

C#High Performance

Mobile Apps

Laurent Testud

Page 2: C# High Performance  Mobile Apps

Mobile Platforms vs. PC

• CPU: 20% ( 32 bits )

• GPU: 10% ( Open GL ES 2.0 – DX 9 )

• Storage: 150% ( On PCB SSD )

• Network: 5% ( Often less, lost connections )

Page 3: C# High Performance  Mobile Apps

High Perf’ Apps

• Harry’s Lap Timerhttp://www.youtube.com/watch?v=FWxT7EaBv-E

• “Angry Birds” and all interactive Games

• “Garage Band” and all Music apps

• “Hydrogen!” and most Visualization apps

Page 4: C# High Performance  Mobile Apps

Lap Timer

Page 5: C# High Performance  Mobile Apps

Hydrogen!

Page 6: C# High Performance  Mobile Apps

In High Perf’ Apps…

• About 90% of code is NOT time critical

Business as usual…

• Only 10% is Real Time

Must complete within 15msRequires special attention!

Page 7: C# High Performance  Mobile Apps

“Classic” Reactive Approach

• Iterate:– Design – Code– Debug / Test– Build

• When about to ship:– Profile => Hot Spots!!! Blame the summer intern!

– Fix– Ship it!

Page 8: C# High Performance  Mobile Apps

Sadly… Does not work • Profiling: No obvious Hot Spots• instead: Thousands of Hot Spots• Or worse: Hot Spots within Framework

• Large memory footprint • Garbage Collection Issues• Memory Warnings

Page 9: C# High Performance  Mobile Apps

“Proactive” Approach

• All dev’ on Low End device (no simulator)

• Work on Release Builds (as often as possible)

• Monitor performance (fps) • React instantly to Any performance drop• Know your Enemies!

Continuous Real Time

Page 10: C# High Performance  Mobile Apps

First Steps – Design / Think

• Review Data Structures

• Review Algorithms

• Move stuff (as much as you can!) outside the loop

• Review API’s usage (low level is usually better)

Page 11: C# High Performance  Mobile Apps

First Steps - Instrument

• Use LLVM Compiler ( iOS Xamarin )

• Instrument your ( critical ) code ( low impact )

• Permanently Measure

Page 12: C# High Performance  Mobile Apps

Code Frugally *• Use the lower level API when applicable

• Avoid big and fat third party libraries

• Do not over engineer!

• Keep it simple!

• Code only what you need* : Simple, plain, costing little.

Page 13: C# High Performance  Mobile Apps

Code for 32 bits• Careful with the Math namespace• Must avoid double and long

public static unsafe float InvSqrt ( float x ) { float xhalf = 0.5f * x ; int i = *(int*) & x ; i = 0x5f3759d5 - ( i >> 1 ) ; x = * ( float * ) & i; x = x * ( 1.5f - xhalf * x * x ) ; return x; }

Page 14: C# High Performance  Mobile Apps

Code Smaller

• Go unsafe if needed.• Precalculate as floats: Sin Cos Pow… • Use tables instead• Precalculate as floats: Random numbers• Save bandwidth: Use byte, sbyte, short• Don’t ship debug code

• Validators• ToString overrides

Page 15: C# High Performance  Mobile Apps

Code Simpler

• Avoid trivial properties, use fields

• Use readonly whenever applicable

• Avoid inheritance and virtual

• No unnecessary interfaces definitions

• Avoid Linq, anon’s, async, tasks

Page 16: C# High Performance  Mobile Apps

Code to Help *

• Use local variables to cache properties

• Pass ‘big’ value types by ref

• Mark your classes as sealed

• Use internal as your default instead of public

* : The Compiler

Page 17: C# High Performance  Mobile Apps

Code Greener!

You Shall Not Create Garbage!

Garbage needs to be collected.Garbage needs to be recycled.

And that can kill you app…

Page 18: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Enumerators - usually via foreach and Linq

• Extreme example– Three nested foreach running on 200 items– 200 + 200 x 200 = 40100 enumerator objects – 60 x 8 x 40 K = 19200 K

20 Mega Bytes of Garbage / Second!

Page 19: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Enumerators - usually via foreach and Linq

• Avoid Linq• Iterate using indexing• Use arrays when applicable/needed• Use unsafe pointers if needed:

t [ i ] == * ( t + i )

Page 20: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Objects created for temporary use• Typically ‘Containers’, EventArgs, …• Often Nodes (LinkedList, etc…)

• Simplify internal APIs• Simplify data transfers• Pool and Recycle container objects

Page 21: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Anonymous methods (with capture)

• Queued delegates (with parameters)

• Regain control of captured variables• Give a name to these animals

Page 22: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Objects you created for “convenience”• Remember: new is worse than goto

• Just don’t. • Recycle• Consider: Promote as class members

Page 23: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• Boxed value types• Improper string manipulation• Growing List<T> and other collections

• Avoid boxing• No + on string• No extension methods on value types• Always assign initial capacity to data structures

Page 24: C# High Performance  Mobile Apps

Where does your Garbage comes from?

• “Sloppy” third party libraries• Most not designed for Real Time use• Do too much

• Roll your own stuff!

Page 25: C# High Performance  Mobile Apps

GC.Collect ( ) ?

Go for it!

• Always outside the loop• Before starting your real time loop• And after ending it.• After disposing, unloading: levels, etc…• GC.Collect ( ) often

Page 26: C# High Performance  Mobile Apps

Code Example

Page 27: C# High Performance  Mobile Apps

Conclusions

• unsafe C# equivalent to C or C++• Compiled C# is FAST on iOS (Thanks Xamarin!)

• No need for Objective-C or C++

C# can be used for Real Time apps

• Adopt proper approach to performance• Code tight • Be careful with the GC

Page 28: C# High Performance  Mobile Apps

Thanks!

Questions?