Following are some of the performance
data that can be focused by WPF application developers so as to improve
performance.
Planning for Application
Performance
You must develop a performance strategy
before you start developing any WPF application. You must draft out the
scenarios where the performance plays a vital factor. Application start-up time,
Per-frame animation frequency rate, maximum working set allowed in a window etc,
are some of the scenarios you could plan and strategise well before.
Once you come up with the scenarios,
you can define goals and measure, investigate, refine performance of your
application on a cyclic/iterative basis.
Taking Advantage of
Hardware
WPF supports both the Software and
Hardware rendering pipelines.
a) Software rendering is totally CPU dependent – more pixels you wants
to render, greater the cost. As a developer, you may reduce the redrawing of
pixels on main components such as background, transparency etc.
b) Hardware rendering allows you to offload the rendering to the
Graphics Processing Unit (GPU) fully utilizing the Microsoft’s DirectX
v 7.0 or later and Pixel Shader 2.0+ features. WPF has an API that
determines the rendering tier (0, 1, and 2) required for a specific hardware to
accelerate the performance.
Layout and Design
a) Avoid un-necessary changes in the layout. A slightest change in a
position of a control would invoke a process called “Layout Pass” that
measures and arranges the controls in a Panel derived containers.
b) Choose the right panel; Stack Panel and Grid provides
more functionalities than a Canvas.
c)
Whenever required, while performing Transform, do
the updates for the properties, in place of replacing the whole transform as it
costs more for designing the layout once again.
d) A Top-Down tree like hierarchical pattern of child elements in a
DockPanel or TextBlock should be followed so as to ensure the
right validations.
2D Graphics and Imaging
a) Graphical drawing content has its own performance factors.
Drawing objects are simpler constructs than Shape objects and
provide better performance characteristics. Shape objects are derived from
FrameworkElement class and can be used inside Panel and other controls.
But Drawing objects are not derived from FrameworkElement class and provides a
lighter-weight implementation for rendering shapes, images, and text.
b) The DrawingVisual object is a lightweight drawing class that
is used to render shapes, images, or text and does not provide layout or event
handling, thus improving performance.
c)
You can reduce the application working set and
gain execution speed by requesting WPF to decode your image to desired size or
thumbnail size rather than to default size.
d) When animating the scale of any bitmap, the default high-quality
image re-sampling algorithm can sometimes consume sufficient system resources to
cause frame rate degradation, effectively causing animations to stutter. By
setting the BitmapScalingMode property of the RenderOptions object
to LowQuality you can create a smoother animation when scaling a
bitmap.
e) By default, WPF does not cache the rendered contents of
TileBrush objects, such as DrawingBrush and VisualBrush. By
setting the CachingHint property of the RenderOptions object to
Cache you can increase performance by using cached versions of the tiled
brush objects.
Object Behavior
a) When performing clean up of an object that has registered to listen
to an object's event, it is essential to remove the associated event handler
delegates before releasing the object.
b) Accessing a dependency property of a DependencyObject is
faster than accessing a CLR property. Furthermore, dependency properties
support robust features, such as data binding, animation, inheritance, and
styling.
c)
Explicitly converting a dependency property
inheritable will impact the performance as it increases the length of time for
property invalidation.
d) Freezing objects whenever possible improves the performance of your
application and reduces its working set. Also, it no longer needs to expend
resources on maintaining change notifications. You may call the Freeze
method or set the Freezable property on a SolidColorBrush that
signifies the performance.
e) It is intensive, both in terms of memory and processor, to generate a
large number of UI elements within a StackPanel when only a few may be on
the screen at a given time. As a performance optimization, you may use the
VirtualizingStackPanel control so that visual objects for these items are
generated or kept alive only if they are visible on the screen.
|