When you design and develop your web application you always must take in consideration the performance factor. By doing this you will remove the cost of rewriting modules, modifying code or redistributing the application. A good practice that must be kept in mind, for writing quality code, is to make frequent code reviews. For example, I often find a better implementation of a certain module after a code review. Also you should test different ways of implementing your code to determine the performance impact over your application.
Here are some rules that should be fallowed for writing high performing applications.
1. Store your content by using caching
As you know ASP.NET allow you to cache entire pages, fragment of pages or controls. You can cache also variable data by specifying the parameters that the data depends. By using caching you help ASP.NET engine to return data for repeated request for the same page much faster. There is one catch here, caching consumes server memory, so it’s not recommended to be used when you need to get updated data on your page.
2. Avoid session state
Whether you store your data in in-process or on state server or in a Sql Database, session state requires memory and it’s also time consuming when you sore or retrieve data from it. If you do not want to use session state, disable it on your web form using <@%Page EnableSessionState=”false”%> directive. In case you use the session state only to retrieve data from it and not to update it, make the session state read only by using <@%Page EnableSessionState =”ReadOnly”%> directive.
3. Avoid ViewSate
ViewState allow you to keep the content of a control across trips to server. This comes with a cost, a greater amount of data is sent from the server to client end vice versa, so the speed and network bandwidth can be affected. You can avoid this drawback by setting the EnableViewState property of your web controls to false, when you don’t need them to keep their state across server trips.
4. Use low cost authentication
Authentication can also have an impact over the performance of your application.
For example passport authentication is slower than form-base authentication which in here turn is slower than Windows authentication.
5. Use Server.Transfer() method for server side page redirection
It is better to use the Server.Transfer() method for server side aspx page redirection in the same application than Response.Redirect() method. This will reduce the extra roundtrip required by the second method (Response.Redirect()) to perform client side redirection.
6. The number of web server controls
The use of web server controls increases the response time of your application because they need time to be processed on the server side before they are rendered on the client side. Therefore take in consideration the usage of HTML elements where they are suited, for example if you want to display static text.
7. Avoid frequent usage of boxing and unboxing
When a value type such as a structure it is copied to a reference type such as a class, the compiler creates an object on the heap and copies the value of the value type from the stack to this newly created object on the heap. This process is called boxing and requires more overhead than just a simple from value type to value type. When you copy a reference type to a value type, the value of the object from the heap is copied to the value type from the stack. This process is called unboxing. You should take in consideration the overhead of these processes when you design your application.
8. Avoid using string for complex strings operations
The string type is immutable; this means that after a string is created it can’t be modified. When you modify a string the CRL creates a new one based on your modifications and returns it. The old string remains in memory until garbage collector will clean it. If your application is extensively modifying strings, then use the StringBuilder class. This class stores the string as an array of characters. The StringBuilder object is mutable and dose in-place modifications of strings.
9. Use AddRange() method with collections
There is a large number of collection classes that expose AddRange() method, which you can use to add an array of items to the collection instead of calling repeatedly Add() method inside a loop.
10. Avoid throwing exceptions
Throwing exceptions is a costly operation. You should be very careful when you throw exception from your application. Exceptions should be throwing only to signify exceptional error cases. You should never throw exceptions just for managing your application flow.
11. Avoid using unmanaged code
Calls to unmanaged code are a costly marshaling operation. Try to reduce the number calls between the managed and unmanaged code. Consider to do more work in each call rather than making frequent calls to do small tasks.
12. Avoid making frequent calls across processes
If you are working with distributed applications, this involves additional overhead negotiating network and application level protocols. In this case network speed can also be a bottleneck. Try to do as much work as possible in fewer calls over the network.
13. Make use of optimized managed providers
As you know OleDb is a generic provider which offers access to data exposed by any OleDb provider. Managed providers are specifically optimized for some databases. For example when you use OleDb to connect to a Sql server database, OleDb first passes your request to the OLE DB COM components which in turn translate the request to Sql Native Tabular Data Stream (TDS) format. If you will use SqlClient, this will directly construct the TDS packets and communicate with Sql server. The removal of extra translation will significantly improve the performance of your application.
14. Use stored procedure and not sql statements
When you are working with a RDBMS as Sql server you should use stored procedures rather than sql statements given as a text command, because stored procedure are highly optimized for server side data access.
15. Use DataReader instead of DataSet for forward-only sequential access
If you are reading a table sequentially you should use the DataReader rather than DataSet. DataReader object creates a read only stream of data that will increase your application performance because only one row is in memory at a time.
16. Use connection pooling
The slowest operation performed in a database application scenario is establishing a connection with a database. The Sql server .NET Data Provider offers connection pooling to improve performance when connecting to a Sql server database. In connection pooling old connection information is stored in a connection pool so it can be reused for the next connection. If you are using dynamic connection strings, you will disallow connection pooling mechanism because connections are only pooled on the exact connection string.
17. Use transactions
Distributed transactions might add performance overhead to your application, so you should use them only when required and keep their life as short as possible.
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
ASP.NET
. Follow any responses to this post through
RSS. You can
leave a response, or trackback from your own site.