Skip Navigation Links
Home
ContactExpand Contact
Performance guidelinesCleanup guidelines

Dot Net performance tips

   

  • Seal classes and methods where possible.  Makes methods candidates for inlining, improving performance.
  • Override Object.Equal() in value types.  If this method is not overridden any call to it will result in the value type being boxed first because Object is a reference type.
  • Provide setter methods for setting multiple properties simultaneously.  This avoids multiple round trips, potentially crossing process boundaries with each trip.  Note that you should still call the actual setters from within the method to maintain business and other rules.
  • Avoid unnecessary public members.  There are serialization overheads that may come into play, if serialization is involved (e.g., using state server, etc.).  Serialization may be turned off for individual members but they are generally on by default.
  • Avoid using the volatile keyword.  It forces reads from memory instead of register.  There are generally better multithreading techniques.
  • Avoid using arrays in parameters.  Provide explicit overloads, if possible, the IL generates those overloads anyway if arrays are used as parameters.
  • Use StringBuilder for string manipulation.  String type is immutable and every mutating operation results in a new copy of the object.
  • Use !Page.IsPostBack in every page & user control load event.
  • Terminate page execution unless necessary, Response.Redirect("http://www.microsoft.com", true);.  The runtime 2.0 no longer generates the resultant exception, so, don’t have to handle it.
  • Minimize use of session to communicate between pages, because,
    • It can lead to comprehensive cleanup effort.
    • Session must be cleansed, otherwise horizontal scalability is adversely affected.
    • Limits independent program reentry points.  For example, Response.Redirect("Default2.aspx?value1=Computers&value2=books", true); will take you where someone can copy and email the URL link, but if session was used to pass the two values to the next page the independent reentry point is no longer available, use it accordingly to serve the correct purpose.
  • Use for (int i=0; i<obj.count; i++) instead of foreach (string s in obj.values), unless boxing/unboxing is involved.
  • Use BasePage to capture general code that should be supported in a set of pages, e.g., sessioning the user object.
  • My personal rule of thumb for max class length, 200 lines.  Rule of thumb for max method length, 20 lines.  Any longer and you should probably reevaluate the purpose and code coherency, god class doesn’t help maintenance and general understanding.
The material is provided freely for educational purposes and not necessarily produced by Naz-Tek