Wednesday, June 9, 2010

WPF Performance Tips

WPF Performance Tips

First off, let me say that this list isn't mine. I saw it on a presentation and wanted to make sure I had it to reference later. Enjoy!

  1. Reduce unnecessary invocations of the layout pass -- update a Transform rather than replacing it
  2. Use the most efficient Panel where possible -- don't use Grid if you need is Canvas
  3. Use Drawing which are cheaper than Shape objects
  4. Use StreamGeometry, which is a light-weight alternative to PathGeometry for creating geometric shapes
  5. Use DrawingVisual to render shapes, images, or text
  6. If you need thumbnail images, create a reduced-sized version of the image
    Note: By default, WPF loads your image and decodes it to its full size
  7. Decode the image to desired size and not to the default size
  8. Combine images into a single image, such as a film strip composed of multiple images
  9. Set BitmapScalingMode to LowQuality for smother animating when scaling bitmap to instruct WPF to use speed-optimized algorithm
  10. Set CachingHint when possible, to conserve memory and increase perf
  11. Brush selection is critical to good performance; don’t use a VisualBrush or DrawingBrush when an ImageBrush or SolidColorBrush would be sufficient
  12. To draw complex, static vector content that may need to be repeatedly re-rendered, consider rendering it into a RenderTargetBitmap first and drawing it as an image instead
    Note: Rendering images is usually faster than rendering vector content
  13. Remember to clear event handlers to prevent object from remaining alive (see this post on finding memory leaks
  14. Use Freezable objects when possible to reduce working set and improve perf
  15. Remember to virtualize
  16. Share resources -- if you use custom controls, define control resources at the application or window level or in the default theme for the custom controls
  17. Share a brush w/o copying by defining it as a resource
  18. Use static vs. dynamic resources
  19. Don’t use FlowDocument element when all you need is a Label or TextBlock
  20. Avoid binding to Label.Content property, use TextBlock instead if source is frequently changes.
    Note: String is immutable, so Label object’s ContentPresenter must regenerate new content
  21. Don't convert CLR object to XML if the only purpose is binding
    Note: Binding to XML content is slower than CLR objects
  22. Always prefer binding to an ObservableCollection<T> vs. List<T>
  23. When a brush is used to set the Fill or Stroke of an element, use the brush's Opacity value rather than element's Opacity property
  24. Disable hit testing (IsHitTestVisible = false) when possible
    Note: Hit test on large 3D surfaces is expensive


If you're hungry for more, feel free to dig thru the Optimizing WPF Application Performance section on MSDN.

No comments:

Post a Comment