Recommended

Skip Navigation LinksHome / Articles / View Article

Stories from the Perf lab: Part I

+ Add to SilverlightShow Favorites
0 comments   /   aggregated from Tales from the Smart Client on Sep 06, 2006  /  original article
(0 votes)
Categories: Learn

During the past year, we’ve found and fixed a lot of perf issues in Expression and WPF.  I’d like to relate a few of them, not so much because you’ll have the exact same problems, but that the pattern of finding and resolving the bugs may be helpful experiences.  To start:

 

Expression uses a tree data structure to keep track of the XAML source code.  Each node in the tree has a property called Children that returns a List of all its child nodes. For leaf nodes this collection was always empty, but even so a new collection object was always allocated. In some scenarios, this resulted in megabytes of allocations.  To fix this, we implemented a singleton EmptyList class.  Leaf nodes always return the same instance of this collection, removing all the allocation costs. 

There are two implementation patterns here.  The simplest is to have a static EmptyList, like this:

 

public static List EmptyList = new List();

 

and just always return that.  This will get you most of the performance you want.  We went a little further and implemented a whole class, giving us greater correctness and even slightly better perf.  Here for example is the implementation of the ICollection members on EmptyList:

 

            public void Add(T item)

            {

                  throw new NotSupportedException();

            }

 

            public void Clear()

            {

            }

 

            public bool Contains(T item)

            {

                  return false;

            }

 

            public void CopyTo(T[] array, int arrayIndex)

            {

            }

 

            public int Count

            {

                  get { return 0; }

            }

 

            public bool IsReadOnly

            {

                  get { return true; }

            }

 

            public bool Remove(T item)

            {

                  return false;

            }

In general, collection classes are a good place for teams to look at optimization: Expression, WPF and other Microsoft teams have all implemented specialized collections for degenerate cases and other cases where the BCL collections are overly general or expensive.

Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 7 and 8 and type the answer here:

Join the free SilverlightShow webinar 'Silverlight's Power Features: Data Binding in Action' on August 19th 2010, 10:00 am PDT.
In this session Gill Cleeren will make with a complete overview of the data binding features Silverlight 4 has to offer. He will discuss how data binding is the enabler for applying the MVVM pattern and commanding, and will end up reviewing Blend's support for data binding, including the use of design-time data. Read more | Register Now (hide this)