[Mono-dev] Optimization for SystemResPool (managed windows form)

Stifu stifu at free.fr
Fri Oct 15 10:35:20 EDT 2010


Note that doing such changes wasn't possible until Mono 2.8, as your patch
relies on generics, which were introduced in .NET 2.0. With the .NET 1.1
profile no longer supported, we can now go generic happy.

Talking about WinForms optimizations, I noticed a few things that could be
improved back then:

- Accessing Control.Location creates a new Point rather than just returning
the private member, for some reason (same for Right, Bottom, etc) - which
seems pointless as Point is a value type
- Sometimes, like for tab drawing, the DrawLine method is called multiple
times, while I guess calling DrawLines once would be faster

Then again, I guess this wouldn't improve performances much.


Lionel Cuir wrote:
> 
> The SystemResPool class (in Theme.cs) is quite heavily used across managed
> windows forms. Yet, it still use the old Hashtable (instead of Dictionary)
> and mainly some of its internal caching dictionary uses strings as key
> (ex:
> to get the cached pen for a given color, it converts the color to a string
> rather than using its Argb as a key).
>  
> Here is the modified code (sorry, I'm sending this code on the fly as I'm
> not on a computer right now, and so has no access to any diff program nor
> can run the Mono's unit tests).
>  
> Note also that the CPColor (in Theme.cs) used as key of a
> dictionary/hashtable in SystemResPool, should have its Equals and
> GetHashCode overloaded, for better performance (if not, the methods of the
> ValueType class are used, which use reflection).
>  
> Cheers,
> Lionel
>  
>  
>  internal class SystemResPool
>  {
>   private Dictionary<int, Pen> pens = new Dictionary<int, Pen>();
>   private Dictionary<long, Pen> dashpens = new Dictionary<long, Pen>();
>   private Dictionary<long, Pen> sizedpens = new Dictionary<long, Pen>();
>   private Dictionary<int, SolidBrush> solidbrushes = new Dictionary<int,
> SolidBrush>();
>   
>   [...]
>  
>   public Pen GetPen(Color color)
>   {
>    int hash = color.ToArgb();
>  
>    lock (pens)
>    {
>     Pen res;
>     if (pens.TryGetValue(hash, out res))
>      return res;
>  
>     Pen pen = new Pen(color);
>     pens.Add(hash, pen);
>     return pen;
>    }
>   }
>  
>   public Pen GetDashPen(Color color, DashStyle dashStyle)
>   {
>    //string hash = color.ToString() + dashStyle;
>    long hash = color.ToArgb() << 32 + (int)dashStyle;
>  
>    lock (dashpens)
>    {
>     Pen res;
>     if (dashpens.TryGetValue(hash, out res))
>      return res;
>  
>     Pen pen = new Pen(color);
>     pen.DashStyle = dashStyle;
>     dashpens[hash] = pen;
>     return pen;
>    }
>   }
>  
>   public Pen GetSizedPen(Color color, int size)
>   {
>    //string hash = color.ToString() + size;
>    long hash = color.ToArgb() << 32 + size;
>  
>    lock (sizedpens)
>    {
>     Pen res;
>     if (sizedpens.TryGetValue(hash, out res))
>      return res;
>  
>     Pen pen = new Pen(color, size);
>     sizedpens[hash] = pen;
>     return pen;
>    }
>   }
>  
>   public SolidBrush GetSolidBrush(Color color)
>   {
>    int hash = color.ToArgb();
>  
>    lock (solidbrushes)
>    {
>     SolidBrush res;
>     if (solidbrushes.TryGetValue(hash, out res))
>      return res;
>  
>     SolidBrush brush = new SolidBrush(color);
>     solidbrushes.Add(hash, brush);
>     return brush;
>    }
>   }
>  
>   [...]
>   
> 
> 
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
> 
> 

-- 
View this message in context: http://mono.1490590.n4.nabble.com/Optimization-for-SystemResPool-managed-windows-form-tp2997115p2997136.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list