This post series is the translation of the performance chapter of the Windows Phone developer book I co-authored with many of my Hungarian peers. If you read Hungarian, I suggest you get the book – either online or in a paper format. There are also some seriously good videos that come with it, if you are more of a session watcher kind. I think that this information would be useful for other WP developers, too, who do not speak Hungarian. Therefore I started this blog post series, translating my Performance chapter. This post series also acts as a draft – if the feedback is good, I will clean it up and publish the performance chapters as an e-book. So, please do provide feedback, even if it is just a grammar mistake on my part!
1. The Big Challenge
If you compare the computing power of a desktop pc or even a laptop to a phone, the numbers are pretty disappointing. The battery of a phone could only deliver a few minutes of power to a desktop computer. The CPU of a Windows Phone has only one core, and even though it runs at 1 GHz, it is about the equivalent of a 600 MHz Pentium III from 1999. A two year old Core 2 processor is about 30 times more powerful. As for the GPU, the Snapdragon has approximately the performance of a graphics card from the same era.
What do we do with all this amazing computing power? Well, mostly we are using it to make user experience better. As computers became faster, they can serve us better – with shorter waiting times, more help for our work, richer graphics, bigger resolutions, and animations that turned out to be useful even for line of business applications. Users expect a certain level of user experience today – the UX from 2000 just won’t cut it in 2012.
The arrival of touch screens have increased these expectations even further. While the mouse has an indirect interaction model, on a touch screen you activate and move the things on the screen directly. This is all great, but these things have to react just like real world objects. And in the real world, the objects don’t move with 5-10 fps (frame per second). When you push a physical object, it doesn’t sit around and wait a few hundred milliseconds before starting to move. On a desktop computer you probably won’t even notice a delay of a third of a second between clicking the mouse and the software’s reaction – but on a touch screen, this can be really disturbing.
The developer experience has gone through a lot of changes, especially in the Microsoft world. We are using the fourth generation of the .NET framework, and we expect to have the same productivity during mobile development as when creating desktop or web applications. The increased performance allows us to use more advanced, higher level tools during development – most of the time we don’t even think about bits, bytes, memory management. Often we define databases and even control schemes using graphical tools. The client-server communication is now part of the framework itself, we don’t have to reinvent the wheel with every project. We, developers are just as surely don’t want to step back ten years in time as our users.
It is a small miracle to have the hardware of a ten year old desktop computer in our pockets. But if you think about how we have to satisfy the increased needs of our users, the power of the phone seems to be rather inadequate. This is the big challenge of mobile development. Let’s see how a Silverlight on Windows Phone 7 can help us face this problem!
2. Typical Performance Problems
While Average Joe can only say “erm… slow”, developers have to know the symptoms better in order to handle the problems. Let’s see what kind of performance problems we can encounter:
- Lagging animations: everybody has different tolerance levels with regards to when an animation becomes disturbingly jerky. But one thing for sure: Windows Phone users have extraordinarily high expectations in this regard. One of the most striking and most often mentioned advantage of the platform in how silky smooth animations users are being pampered with.
- Slow loading applications: no one likes to wait a long time for an application to be usable after launching it. If loading takes more than five seconds and there is no splash screen, even the OS will kill it. (This timeout is set to 20 seconds if we use a splash screen.)
- Long wait times (responsiveness): how much time does it take for the application to give an answer after pressing a button? If there is no feedback, the user will probably press the button again and again, thinking that the phone has not registered the touch.
- Empty area appears while scrolling (blanking): scrolling through a long list, the user sees empty, blank areas, since drawing cannot keep up with the scrolling.
- Running out of memory: as a developer, we mostly meet this symptom at the latest during Marketplace digestion – Microsoft simply rejects apps that use too much memory.
3. The Task of the CPU and the GPU
The CPU is a general purpose processor – it can perform mathematical operations, execute commands and algorithms. The app we write runs on the CPU. The CPU is a neat and essential component, but there are certain tasks it is not ideal for – such as graphic operations and video decoding.
Because of this, phones are also equipped with a GPU – a Graphical Processing Unit. This GPU works much like its big brother in desktop computers – it can place display triangles in space, millions per second. Fortunately, as a Windows Phone developer, we don’t have to deal with the GPU at such a low level – most of the time we don’t even realize that the Silverlight engine uses the GPU. But it is still very useful to know some things about the GPU if we want to solve performance issues. The GPU helps the CPU by bearing the burden of the following operations:
- Composition – placing two or more textures (bitmaps) on the screen that cover each other and/or are semi-transparent. Moving things on the screen also happens with composition – after changing the position of an element, the composition restarts in the next frame.
- Rotation – rotating a bitmap
- Resizing – increasing or decreasing the size of a texture. The GPU can also smooth the edges of a texture, thus help with pixelating or aliasing.
- Perspective transforms – rotating a texture in 3D. This is not a real 3D, more like 2.5D, since we are only talking about flat textures and not 3D bodies. However, using perspective transforms properly can still provide the illusion of full 3D, just as it happened in SurfCube 3D Browser.
- Rectangular cropping – the GPU can be used to crop a texture, as long as the clipping geometry is rectangular.
- Video decoding and encoding – on Windows Phone, the video encoding, decoding, resizing is performed by a dedicated hardware – which is great news because the CPU wouldn’t be able to play HD videos on its own. This way it is free to do other tasks, such as displaying subtitles, a seek bar, handling input, and so on.
Mindful readers may have realized that the GPU is all about manipulating textures or bitmaps. But how is a texture of a control (such as a Button) created? This process is called rendering or rasterizing, and is performed by the CPU. The CPU is also tasked with interpreting XAML, creating controls, calculating layout, and of course – running our application.
In the next post, I will introduce our first sample application, examine the performance characteristics of the emulator, and show some techniques to decrease the app’s launch time. Later on, we will examine the reasons of jerky animations, the performance diagnostic tools. We will talk about lists, subjective performance, memory optimizations, and even meet a ten-eyed drunk space-captain buzzed by a TIE Fighter! Let me know what you think in the comments and follow me on twitter at www.twitter.com/vbandi.
Feb 15 2012, 11:02 PM