WPF for developers - optimizing your WPF application

  • View

  • Download

Embed Size (px)

Text of WPF for developers - optimizing your WPF application

  • 1. Tamir Khason Software consulter mPrest systems : . . .http //blogs microsoft co il/blogs/tamir

2. Think in terms of scenario Dont push to the limit Do only what you have to do Set your final goals Target customers expectations Dont try to do things smarter 3. WPF is managed code Dont forget about memory Do as little as possible on startup WPF is retained system You have no pixels, you have a tree Minimize trees, keep changes locally 4. DependencyDependency ObjectObject VisualVisual UIElementUIElement Framework ElementFramework Element ControlControl ShapeShape FreezableFreezable AnimatableAnimatable DrawingDrawingGeometryGeometry Style, Resources, Binding, PropertiesStyle, Resources, Binding, Properties TemplatesTemplates AnimationAnimation Transformation, Bounds, ClipTransformation, Bounds, Clip Input, Focus, Layout, Routed EventsInput, Focus, Layout, Routed Events Change notifications, multithreadingChange notifications, multithreading 5. Rendering threadRendering thread mostly GPU, unmanaged Part of tree to render, PROBLEMS (which hard to detect) UI threadUI thread - CPU only, managed All services, your code, tree, PROBLEMS 6. Bitmap Effects RenderTargetBitmap Tilebrush Operations requires more RAM, then available in video card Layered windows 7. Your code is running here It runs in CPU only Visual tree is living here Most of your performance problems are here! 8. Use smaller Visual Tree Dont force unnecessary measurements Virtualize your data Use static resources Move your code into other thread Create your own other UI threads 9. FrameworkElement is not base class Shapes vs. Drawing Polyline vs. StreamGeometry Not every control is control Each TextBox contains 30 elements Each Label contains 5 elements Each TextBlock contains only one 10. Use TextBlock Use TextFormatter Or even GlyphRuns directly TextBlock is 32 times faster then FlowDocument Use fewer elements, everything in WPF have costs 11. Label = 3 X 5 = 15Label = 3 X 5 = 15 Grid = LayoutGrid = Layout ViewBox = Layout +ViewBox = Layout + MeasurementsMeasurements ListBox is too complicated forListBox is too complicated for such layoutsuch layout VirtualizingStackPanel is 70xVirtualizingStackPanel is 70x faster, then StackPanelfaster, then StackPanel 12. ScrollViewer ScrollBarVisibility = Auto Dont calculate nothing Dont tickle Layout Engine FrameworkElement Width/Height= Auto Dont you know the real size of content? Resize = layout = tree walk Why to measure? GridLength.Star, ResizeMode, SizeToContent Want dynamic behavior do it, but not too much Make sure, its absolutely necessary Canvas in the smallest content control it much smaller, then Grid More rows and columns means bigger tree Custom cell template = more then 60 FrameworkElements 13. Instantiate on demand VirtualizingStackPanel is 70x faster, then StackPanel You can virtualize data by yourself Virtualization does delete and create tree measure before implementing 14. StaticResource vs. DynamicResource StaticResource = one evaluation DynamicResource = one reference Use ResourceDictionary to share resources You can Load and Unload resources on demand Scale your images Freeze whenever you can 15. IList vs. IEnumerable XML vs. CLR SelectedIndex calls to IndexOf Set DataContext instead XAML and switch it on Application.OnActivated 16. DependencyProperty is x3 faster, then INotifyPropertyChanged ICustomPropertyDescriptor is your friend for vary property set ObservableCollection is x90 faster accesses single item, then List ObjectDataProvider is x20 smaller, then XmlDataProvider 17. Its asynchronousIts asynchronous It applies everywhere onlyIt applies everywhere only onceonce Its manual and one wayIts manual and one way And saves a lotAnd saves a lot of unmanagedof unmanaged resourcesresources 18. public class Car : DependencyObject public static readonly DependencyProperty BigImageProperty; FrameworkPropertyMetadata( default(BitmapSource), FrameworkPropertyMetadataOptions.None)); public class Cars : ObservableCollection Setter is slower, but getter isSetter is slower, but getter is much fastermuch faster This property does notThis property does not affects neitheraffects neither measurement, normeasurement, nor renderingrendering Its much better to add andIts much better to add and remove items withoutremove items without regeneration controlregeneration control 19. DispatcherOperation oper = Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, LoadFromXML); Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, (SendOrPostCallback)delegate(object o) { CreateCarNode (o); }, node) BitmapImage image; image. DecodePixelWidth = 200; image.Freeze(); Invoke time consumingInvoke time consuming operations asynchronouslyoperations asynchronously with low prioritywith low priority Invoke recent and fast operationsInvoke recent and fast operations asynchronously with low priorityasynchronously with low priority Scale and freeze unmanaged and staticScale and freeze unmanaged and static resourcesresources 20. ColdStart - After reboot or long period of time Resources are not presents System calls (registry, disk) WormStart CLR components are already loaded Resources are allocated 21. Tier 0Tier 0 Tier 1Tier 1 Tier 2Tier 2 DX < 7DX < 7 7 = 30MB Pixel Shader >= 1.0Pixel Shader >= 1.0 Vertex Shader >= 1.0Vertex Shader >= 1.0 DX >= 9DX >= 9 Video RAM >=120 MBVideo RAM >=120 MB Pixel Shader >= 2.0Pixel Shader >= 2.0 Vertex Shader >= 2.0Vertex Shader >= 2.0 Multitexture units >=4Multitexture units >=4 22. Tier 0 Everything is unaccelerated Tier 1 (YES) Most 2-D Rendering & 3-D Rasterization 23. Tier 1 (NO) 3D lighting calculations, Color-keyed alpha and Text rendering Tier 2 (YES) Radial Gradients 3-D lighting calculations Text rendering 3-D antialiasing (Windows Vista only) 24. Always unaccelerated Bitmap Effects, Printed Content, RenderTargetBitmap Tilebrush (Tilemode == Tile), Big Surfaces Operations requires more RAM, then available in video card Layered windows 25. 0% 20% 40% 60% 80% 100% Before After 26. 99% 1% 0% Rendering Application System 61% 4% 35% Rendering Application System Before After 27. Dont put performance testing to the end Do prioritize performance in dev. Plan Kill em when they small Plan performance-oriented features Test on real world hardware Share your knowledge with designers Know how things work under the hoods 28. Just code Tamir Khason http://blogs.microsoft.co.il/blogs/tamir/ WPF Performance on MSDN http://msdn2.microsoft.com/en-us/library/aa970776.aspx Josh Smith on WPF http://joshsmithonwpf.wordpress.com/ Henry Hahn WPF Program Manager http://blogs.msdn.com/henryh/ Tim Cahill WPF Performance Guidance http://blogs.msdn.com/timothyc/ Windows Presentation Foundation SDK http://blogs.msdn.com/wpfsdk/ Ian Who VSTS profiler http://blogs.msdn.com/ianhu/ Rico Mariani Performance Tidbits http://blogs.msdn.com/ricom/ Dwayne Need Presentation Source http://blogs.msdn.com/dwayneneed/ 29. ? ! 2 30. 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. t-shirts*