RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 127, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


ASP.NET Optimization - Tips you hear, but this time from me...

Here are some things I have done to speed up my pages (things I have picked up over time):

  • Set the Option Strict and Option Explicit to On in the Project Properties. Option Explicit requires you to declare your variables and objects before using them and is set to “On” by default.  Option Strict requires you to explicitly declare datatype conversions by disallowing late binding.  This takes the guesswork out during run time, thus faster execution.
  • Set Session State to ReadOnly where you can.  Read only maintains state, but may bounce the session when moving to a page where session state is set to true.  There is also False, which is a good idea if your page is going to be completely public and doesn't need to hold any session information at all.
  • Set ViewState to False (makes a huge difference) where you can.  If a control on the page needs session state, I leave it on for the page, but turn it off on all of the server controls that do not need it (yes, you can do this).
  • Cache items where you can.  This not only include pages, but also information, such as datasets.
  • Use System.Text.StringBuilder instead of Strings (because strings are immutable).  If you make more than 3 changes to a string (this includes concatenations), you should use the stringbuilder instead.
    • Real life example:  An application I built was pulling information from Active Directories and displaying it in a page that included contact information.  If someone pulled the entire directory for display, it took between 50-80 seconds to display everything.  I went in today and optimized it by using the string builder instead of strings.  From that change alone I went from 50+ seconds to 2-3 seconds!  Then I also started caching each of the individual OU's table information into the cache so that everything would check the cache first, and then pull from active directories if it wasn't there.  That reduced the entire directory pull to 1 second.  That is great considering I am pulling information about over 3,200 employees.
  • Build and deploy the Release version.  It is smaller and will execute faster.
  • Use ngen to precompile your application dlls after deployment.  Ngen is short for Native Image Generator.  What it does is precompile as much of the code as it can for the processor of the machine that the application is on.  This means less code to execute by the JIT (Just In Time) compiler, which means faster execution of pages.
  • Set the debug attribute in the web.config file to False.  In fact, take everything that you don't use out of the web.config file.  If you don't need it for your application, remove it.  My suggestion is to make a copy of it first, that way if you pull something out that you may have needed, you will have it right there to set it back.  You can try to pull everything out, then put things in until the application works again (to the desired results).

 

Following some tips, I have taken ngen and have made it an automated batch that I just add directories to, and it will run a nightly check on those directories and dump the output into a log file that I have put with it.  I can even have it email me, but I would rather not.  Why would I do this, you would ask?  Well, some people work in a situation where they only deploy once.  We deploy constantly in the development stages.  I can be on the phone with a user and have made changes and deployed them twice in five minutes.  We try to make our applications as bug-free as we can, but that is not always the case in coding.  When we pick up new tips, we refactor our code. 

posted on Thursday, October 07, 2004 9:18 PM

Feedback

# re: ASP.NET Optimization - Tips you hear, but this time from me...

Hi there

NGENing ASP.NET assemblies doesn't actually help at all. ASP.NET has to load its assemblies into a shared application domain, and CLR policy says that it will ignore any NGENd versions of assemblies and load the originals.

See http://support.microsoft.com/default.aspx?scid=kb;en-us;331979 for a bit more info.

HTH

John
john at johndowns dot co dot nz
11/24/2004 5:54 PM | JohnD

# re: ASP.NET Optimization - Tips you hear, but this time from me...

See? You learn something new everyday! Thanks John. You were right about a "bit" more information. That was severely lacking on the part of Microsoft, but it was straight to the point. :)

Of course when I was looking at this post the other day I was thinking that maybe it could use a mention of Output Caching, because this is also an optimization technique, or rather a great technique for holding a compiled page in memory and passing that back out to the client for a certain amount of time. This is done in the .aspx page by adding this above the html:
<%@ OutputCache Duration="20" VaryByParam="none" %>
Duration is the number of seconds to hold the cache. VaryByParam is whether it should hold different copies of the page. Say you have a parameter that is "id" that you pass into the page to display different items. You can hold a copy of each for a certain duration by setting VaryByParam="id" . There are other options to the output cache but these are the most frequently used. Duration is required, and VaryByParam is usually required. I will probably need to read up on it some more to regain a good foothold on the other options again.
12/1/2004 9:15 PM | Robz

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: