Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #67 – Silverlight Performance Tips

After watching Seema’s PDC talk on Building an Optimized, Graphics-Intensive Application in Microsoft Silverlight I thought it would be useful to summarize what I learned.

1. Debugging

The following items will help you debug performance in your Silverlight application:

  • EnableRedrawRegions – Turn on this debug setting to see what and where rectangles are being redrawn within your application. When enabled you can see what is painted because the repaint region is colored in blue, yellow or pink. This property is set when you create your Silverlight plug-in.

    Example:

<div style="height:100%">
    <asp:Silverlight ID="Xaml1" EnableRedrawRegions="true" 
     
EnableFrameRateCounter="true" runat="server" PluginBackground="Transparent" 
     
Windowless="true" Source="~/ClientBin/Snowflakes.xap"
     
MinimumVersion="2.0.30523" Width="100%" Height="100%" />
</div>


  • MaxFrameRate - To see what components of your application are having the most adverse affect on the performance you can set the MaxFrameRate for your application. You can then experiment with adding and removing components to your application to see how the performance changes. To do set the following properties highlighted below:

<div  style="height:100%">
    <asp:Silverlight ID="Xaml1" MaxFrameRate="10"  EnableFrameRateCounter="true"  
      
runat="server" PluginBackground="Transparent" Windowless="true"
      
Source="~/ClientBin/Snowflakes.xap" MinimumVersion="2.0.30523" Width="100%" 
       Height
="100%" />
</div>

2. Media Pipeline

For each frame and each pixel when drawing a media element Silverlight:

  1. Decodes the image.
  2. Performs a YUV Convert
  3. Resize if media is not at its original size.
  4. Blend if needed (for example: overlays such as icons, scroll bars, etc.)
  5. Draw

Few notes on performance:

  1. Encode the media element at the minimum frame rate you can accept.
  2. Display your media element at its original size to avoid resize calculations. If you want a different size, first re-encode the media to the size you want to display it at. This will save on bandwidth and interpolation of every pixel each frame.
  3. Blending with media is expensive, avoid overlays where possible.
  4. For media, use a frame of around 60 for best results.

3. Text Rendering

Text rendering is expensive and should be avoided when possible. The following actions triggers text to be re-rendered:

  1. Translating or scaling the text
  2. Media on the page.
  3. Animation on the page.

Consider using a bitmap in replace of text for any intensive animation.

4. Silverlight Control.

  1. Running Silverlight with “Windowless = false” is faster than “Windowless=true”. Only set it to true if you need to overlay HTML content on top of your Silverlight application.
  2. Do not set the Silverlight plug-in control to PluginBackground="Transparent" unless absolutely necessary. This adds costs to each render call forcing it through the blending pipeline. If your goal is to match the Silverlight controls background color with your HTML background color simply set the background style color of your HTML page to match the Silverlight control. In the example below we set the body background to black. Make certain the size of the Silverlight control matches that which you declare in Page.xaml otherwise the difference will be represented by a white border.

<body style="background-color:Black;margin:0;">
    <form id="form1" runat="server" style="height:100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/FastAnimation.xap" MinimumVersion="2.0.30523" Width="800px" Height="600px" />
        </div>
    </form>
</body>

Final Notes:

  • Silverlight gives you full control of the UI thread. Use it wisely.
  • Don’t bloat your code, especially your XAML.
  • Turn off your application (stop threads, background calculation, etc.) when not needed.
  • When brushes are resized a new texture is allocated for the brush. Keep this in mind when resizing!
  • When Silverlight performs animation it only redraws the dirty rectangles. Avoid large scale animations.
  • Set the frame rate of your application to only be what is needed. For video, you will want around 60. For animation, around 20-30.

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

1. Debugging The following items will help you debug performance in your Silverlight application: EnableRedrawRegions

# November 3, 2008 6:13 PM

bartczernicki said:

Ahh...These are essentially the notes from Seema's talk at the PDC on Silverlight :)

# November 3, 2008 7:26 PM

mike.snow said:

Yep, I have a link to her video at the top if you haven't already viewed it. Also, check out tip of the day #68 where i summarize the links on the other PDC talks for Silverlight.

# November 3, 2008 7:45 PM

2008 November 04 - Links for today « My (almost) Daily Links said:

Pingback from  2008 November 04 - Links for today &laquo; My (almost) Daily Links

# November 4, 2008 1:37 AM

Mike Snows Tip of the Day #67 - 69 said:

Pingback from  Mike Snows Tip of the Day #67 - 69

# November 4, 2008 5:10 AM

Silverlight News for November 04, 2008 said:

Pingback from  Silverlight News for November 04, 2008

# November 4, 2008 7:08 AM

Dew Drop - November 4, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - November 4, 2008 | Alvin Ashcraft's Morning Dew

# November 4, 2008 8:09 AM

Секреты Visual Studio said:

Майк работает в группе Web Tools компании Microsoft и ведет блог, посвященный программированию на Silverlight.

# November 5, 2008 4:51 AM

Das Leben mit und ohne Pferd | oder das Leben ist (k)ein Ponyhof - in Text, Bild, Audio und Video said:

Pingback from  Das Leben mit und ohne Pferd | oder das Leben ist (k)ein Ponyhof - in Text, Bild, Audio und Video

# November 10, 2008 2:29 AM

SilverLite said:

Mike Snow posted a few of the tips from my PDC talk here, it's a great summary. You can check it out

# November 25, 2008 4:30 PM

di .NET e di altre Amenit said:

Silverlight 2.0: Ancora tip sulle performance

# November 26, 2008 3:18 AM

Zuker On Foundations said:

Read this post for great inormation about Silverlight performance tips. Read this post for details about

# November 26, 2008 4:30 AM

Silverlight Performance Tips | DavideZordan.net said:

Pingback from  Silverlight Performance Tips | DavideZordan.net

# November 26, 2008 5:59 AM

SilverTrader: Resize of Windows « Tales from a Trading Desk said:

Pingback from  SilverTrader: Resize of Windows &laquo; Tales from a Trading Desk

# December 1, 2008 7:01 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Visual Web Developer Team Blog said:

This link provides a complete Tips of the Day Summary Outline - silverlight.net/.../silverlight-tips-of-the-day-summary-outline.aspx

# January 8, 2009 6:37 PM

Zuker On Foundations said:

Read this post for great inormation about Silverlight performance tips. Read this post for details about

# January 15, 2009 8:26 AM

Performance Profiling for WPF and Silverlight « Mahesh Punyamurthula’s Blog said:

Pingback from  Performance Profiling for WPF and Silverlight &laquo; Mahesh Punyamurthula&#8217;s Blog

# April 3, 2009 4:01 PM