performance-and-tips 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. * Prefer String.Compare to changing case
  2. If you need to compare strings ignoring case, you should use
  3. String.Compare rather than calling ToLower on both. This
  4. avoids allocations.
  5. * Prefer ToLower to ToUpper
  6. Apparently, ToLower is faster.
  7. * Use the InvariantCulture
  8. InvariantCulture should be used for all "non-linguistic
  9. identifiers" (see http://tinyurl.com/9vqus -- the 2.0 string
  10. recommendations). It is faster and more correct.
  11. Some methods in the string class that you might not think are
  12. culture sensitive really are. The following methods are
  13. culture sensitive:
  14. - .CompareTo
  15. - .ToUpper
  16. - .ToLower
  17. - .StartsWith
  18. - .EndsWith
  19. - .IndexOf (string, ...)
  20. - .LastIndexOf (string, ...)
  21. The methods in System.Web.Util.StrUtils exist to make correct
  22. calls less verbose. They use the InvariantCulture.
  23. * Size of controls
  24. In controls, it is important to keep the size of controls
  25. small. Controls can be replicated many times on a page due to
  26. data bound controls. The more memory each control allocates,
  27. the more GCs we have to go through.
  28. * Avoid extra fields
  29. There are a few techniques to save space in the number of fields.
  30. * Use [Flags] enums rather than many bool's
  31. Each bool takes up 1 byte. If you use a flags enum you can
  32. pack 8 bools into the same amount of space.
  33. * Use the Events framework
  34. Every time you say
  35. public event EventHandler x;
  36. it creates a field. Most of these fields never get
  37. used. Control has a property called Events which holds a
  38. linked list of events that get created. Thus, events only take
  39. up space when one uses them. An example of using this:
  40. static object event_name_blah = new object ();
  41. ....
  42. public event EventType EventName {
  43. add { Events.AddHandler (event_name_blah, value); }
  44. remove { Events.RemoveHandler (event_name_blah, value); }
  45. }
  46. ....
  47. If your control has a OnEventName that invokes EventName, you
  48. have to do:
  49. EventType deleg = (EventType) Events [event_name_blah];
  50. if (deleg != null)
  51. delege (arguments);
  52. * ViewState
  53. Keep the view state small.
  54. Remember that whatever gets stored in ViewState after tracking
  55. starts needs to be sent over the wire.
  56. Store in ViewState the minimum amount of objects needed to
  57. restore your state.
  58. * Store optimized classes.
  59. It is important to store things in terms of primitive types
  60. (int, short, bool, byte, string), Hashtables, ArrayLists,
  61. object arrays, and specially optimized types (Unit, Color,
  62. Pair, Triplet) when saving viewstate. Otherwise, things will
  63. have to be put in a more expensive format over the wire.
  64. * Store int values rather than enums.
  65. If you store an enum, the fully qualified name to the enum
  66. needs to be sent over the wire. Cast the enum value to an
  67. integer when storing it in view state. Keep in mind that
  68. object o = 1;
  69. MyEnum e = (MyEnum) o;
  70. Works, so you don't need any complex code when getting the
  71. stuff back.
  72. * Return null in SaveViewState when possible
  73. For example, if you normally save the state of your 3 children
  74. to a triplet, but all 3 values are null, return null, rather
  75. than new Triplet (null, null, null);
  76. Often, doing this allows the framework to avoid saving
  77. anything about many layers of controls in the viewstate.
  78. * Cache in variables
  79. Accessing a property in the controls usually means reading
  80. from ViewState. Try assigning property values to local
  81. variables if they are going to be used more than once and are
  82. known to not change while a given method runs.
  83. * Handle Enabled and Visible.
  84. If Enabled is false, your control should not handle any
  85. postback. If Visible is false, your control does not render
  86. anything (but still keeps its state). Usually the parent takes
  87. care of that.