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