4. Think in terms of scenario
Don’t push to the limit
Do only what you have to do
Set your final goals
Target customer’s expectations
Don’t try to do things “smarter”
5. WPF is managed code
Don’t 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
10. 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
13. Your code is running here
It runs in CPU only
Visual tree is living here
Most of your performance
problems are here!
14. Use smaller Visual Tree
Don’t force unnecessary
measurements
Virtualize your data
Use static resources
Move your code into other thread
Create your own other UI threads
15. 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
16. Use TextBlock
Use TextFormatter
Or even GlyphRuns directly
TextBlock is 32 times faster then
FlowDocument
Use fewer elements, everything in
WPF have costs
17. 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
18. ScrollViewer ScrollBarVisibility = Auto
Don’t calculate nothing
Don’t tickle Layout Engine
FrameworkElement Width/Height= Auto
Don’t 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, it’s 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
19. Instantiate on demand
VirtualizingStackPanel is 70x faster,
then StackPanel
You can virtualize data by yourself
Virtualization does delete and create
tree – measure before implementing
20. 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
21. IList vs. IEnumerable
XML vs. CLR
SelectedIndex calls to IndexOf
Set DataContext instead XAML and
switch it on Application.OnActivated
22. DependencyProperty is x3 faster, then
INotifyPropertyChanged
ICustomPropertyDescriptor is your friend for
vary property set
ObservableCollection<T> is x90 faster
accesses single item, then List<T>
ObjectDataProvider is x20 smaller, then
XmlDataProvider
23. <ObjectDataProvider x:Key="cars"
ObjectType="{x:Type l:Cars}"
IsAsynchronous="True“/>
…
<Canvas
DataContext="{StaticResource cars}“>
<Image
Width="1024" Height="768"
Source="{Binding Path=BigImage,
Mode=OneWay,
NotifyOnTargetUpdated=True}"
RenderOptions.CachingHint="Cache"
RenderOptions.BitmapScalingMode="LowQuality">
</Canvas>
It’s asynchronousIt’s asynchronous
It applies everywhere onlyIt applies everywhere only
onceonce
It’s manual and one wayIt’s manual and one way
And saves a lotAnd saves a lot
of unmanagedof unmanaged
resourcesresources
24. public class Car : DependencyObject
public static readonly DependencyProperty BigImageProperty;
FrameworkPropertyMetadata(
default(BitmapSource),
FrameworkPropertyMetadataOptions.None));
public class Cars : ObservableCollection<Car>
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
It’s much better to add andIt’s much better to add and
remove items withoutremove items without
regeneration controlregeneration control
25. 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
26. 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
29. Tier 0
Everything is unaccelerated
Tier 1 (YES)
Most 2-D Rendering & 3-D
Rasterization
30. 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)
31. Always unaccelerated
Bitmap Effects, Printed Content,
RenderTargetBitmap
Tilebrush (Tilemode == Tile), Big
Surfaces
Operations requires more RAM, then
available in video card
Layered windows
35. Don’t 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”
36.
37. 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/