Windows Phone 7 Performance TipsChris KoenigSenior Developer EvangelistMicrosoft Corporation
@chriskoenig | chriskoenig.net | [email protected]
verydemotivational.com
If only you could attach it to a hat
SUN SHADE
delicious.com/chriskoenig/wp7+performance
Windows Phone
Optimizations for DirectX 9 GPUTouch, 800x480 display1 GHz ARM Scorpion
Silverlight 3
UI Threading ModelsDesktop (up to SL4, today) • UI thread
– Input – Layout + Render– Events + Binding– Animation– Rasterize
Windows Phone• UI thread
– Input– Render + Layout – Events + Binding – Some Animation– Rasterize
• Compositor thread – Basic Animation – Draw buffers
Threading ModelsUI Thread vs. Compositor Thread
Desktop to Mobile (mindset) • ~1 GHz is not that much • User expects immediacy• Network latencies are high • Small Screen = More transitions • Touch is primary input • Preserve battery life
Basic Rules of Thumb• Perceived vs. Actual Performance– How slow is slow?
• Performance vs. Complexity• Identify potential problem areas• Monitor as changes are made• Test on a device early and often• Involve designers early and often
Startup Performance Tips• Use a Splash screen• Keep assemblies small
– Partition assemblies– Include media as Content, not as Resources– Use Satellite assemblies
• Delay load assemblies • Avoid ApplicationSettings class
– Be granular, choose faster formats
• Delay load content (esp. Panorama, avoid Item parsing)
Navigating to an External Resource
private void button1_Click(object sender, RoutedEventArgs e) {
var s = "/OtherAssembly;component/ExternalPage.xaml"var uri = new Uri(uriString, UriKind.Relative); NavigationService.Navigate(uri);
}
Using LayoutUpdatedprivate bool _onNavigatedToCalled = false; public Page() {
InitializeComponent(); LayoutUpdated += new EventHandler(Page_LayoutUpdated);
} protected override void OnNavigatedTo(NavigationEventArgs e) { _onNavigatedToCalled = true; } private void Page_LayoutUpdated(object sender, EventArgs e) {
if (_onNavigatedToCalled == true) { _onNavigatedToCalled = false; Dispatcher.BeginInvoke(() => { DoWork() } );
} }
UI Performance Tips• Less is more• Minimize UI thread work• Leverage the GPU• Opacity vs. Visibility • BitmapCache• Never ever use OpacityMask• Use Manipulation Events over Mouse Events
UI - Things to Avoid• Color Animation• Non-rectangular clips• Opacity masks• Popup• Converters• Watch for Callbacks that come back to UI
thread– e.g. WebClient, GeoCoordinateWatcher, etc.
BitmapCacheCacheMode=“BitmapCache”
Useful for…• Render Transform
– Scale Transform– Rotate Transform– Translate Transform
• Change the opacity• Change the clipping
region
Danger Areas• Caches Bitmaps – not
Vectors!– You will lose fidelity– Try different
RenderAtScale
• Items frequently redrawn
• Non-rectangular clips
Performance Counters• Composition Thread FPS• UI Thread FPS• Texture Memory Usage• Surface Counter• Intermediate Surface Counter• Screen Fill Rate Counter
UI Performance
BitmapCacheFillRateTest
ListBox Performance Tips• Don’t load so darn much data!• Simplify ListBox ItemTemplate• Avoid converters on bindings • Load images in the background (LowProfileImageLoader)
• Use Data Virtualization (e.g. LazyListBox)
• ListBox alternatives– StackPanel for short lists– LongListSelector for complex lists– LazyListBox + LowProfileImageLoader for alt. templates
Control Alternatives• PerformanceProgressBar• LowProfileImageLoader• LongListSelector• LazyListBox• DeferredLoadListBox• DeferredLoadStackPanel • GroupingItemsControlConverter
Custom ControlsPerformanceProgressBarLowProfileImageLoaderLazyLoadListBoxVirtualizingDataTest
Data Performance Tips• Consider network latency• Consider impact of holding data in
memory• Parallelize data requests to save battery• Process data on background threads*• Use HttpWebRequest instead of WebClient
Media• Use Jpeg over Png whenever possible• Use Images over Xaml whenever
possible• Limit image size to 2K x 2K• Set Build Action to Content• Encode media for optimal playback rate
and resolution
Fast is not good enough!! • Perceived performance is important!
– Feedback on clicks – ProgressBar (use Jeff Wilcox) – Transitions across pages
• That said,– Don’t rely on our magic. – Use the counters and flags to verify. – Balance ‘feedback’ with real work ...
Summary• How slow is slow?• Plan ahead for problem areas• Utilize the counters• Test, test, test – especially on a
device
chriskoenig.net/WP7Resources
Thanks to Jaime Rodriguez, Yochai Kiriati, Jeff Wilcox, Peter Torr and all the other great presenters, speakers and bloggers from whom I found all this great content about performance tuning Silverlight applications for Windows Phone 7.
Credits