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@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

Reply via email to