Summary: Most Active Server Pages (ASP) applications use string concatenation to create HTML-formatted data that is presented to the user. This article compares several methods for creating this HTML data stream, and in certain cases some methods are superior to other methods in performance. This article assumes that you already have some knowledge of ASP and visual Basic programming.
Brief introduction
When writing an ASP page, the developer actually creates a formatted text stream that is written to the Web client through the Response object provided by the ASP. There are a number of ways to create this text stream, and the method you choose will have a significant impact on the performance and scalability of your WEB application. Many times, when I helped clients optimize the performance of their WEB applications, it was found that one of the more effective ways to change the way HTML streams were created. This article will introduce several common techniques and test their impact on the performance of a simple ASP page.
ASP Design
Many ASP developers follow good software engineering principles and modularize their code as much as possible. This design typically uses a number of include files that contain functions that are formatted for a particular discontinuous part of the page. The string output of these functions, usually the HTML form code, creates a complete page from a variety of combinations. Some developers have improved this approach by moving these HTML functions into Visual Basic COM components, hoping to take full advantage of the extra performance that has been provided by compiled code.
Although this design approach is good, the method used to create strings that make up these discrete HTML code components will have a significant impact on the performance and scalability of the Web site, regardless of whether the actual operation is performed in an ASP include file or in a Visual Basic COM component.
string concatenation
Take a look at the code snippet for the following writehtml function. The parameter named data is just an array of strings that contain some data to be formatted as a table structure (for example, data returned from a database).
Function writehtml (Data)
Dim nrep
for nrep = 0 to $
SHTML = SHTML & vbCrLf _
& "& (Nrep + 1) &" "_
& Data (0, Nrep) &" "_
& Data (1, nrep) &" _
& Data (2, Nrep) & "/td> " _
& Data (3, Nrep) & "" td> "_
& Data (4 , Nrep) & " " _
& Data (5, Nrep) & " "
Next
writehtml = SHTML
End Functio N
This is a common way for many ASP and visual Basic developers to create HTML code. The text contained in the SHTML variable is returned to the calling code, which is then written to the client using Response.Write. Of course, this can also be expressed as a similar code for embedding pages that do not contain writehtml functions directly. The problem with this code is that the string data type (BSTR or basic string) used by ASP and visual Basic cannot actually change the length. This means that each time the string length changes, the original representation of the string in memory is corrupted, and a new representation is created that contains the new string data: This increases the amount of memory allocated and the allocation of memory. Of course, ASP and visual Basic have solved this problem for you, so the actual overhead is not immediately apparent. Allocating and releasing memory requires basic Run-time code to unlock individual locks, and therefore requires significant overhead. This problem becomes particularly noticeable when strings become large and large chunks of memory are allocated and unassigned quickly and continuously, as they would appear during a large string connection. Although this problem has little impact on single-user environments, it can cause serious performance and scalability problems in a server environment, such as an ASP application running on a WEB server.
Let's go back to the code snippet above: How many string assignment operations do you want to perform in this code? The answer is 16. In this case, each application of the "&" operator will cause the string that the variable SHTML refers to be corrupted and recreated. As mentioned earlier, string allocation is expensive and increases as the string increases, so we can improve on this code.
Quick Solution
There are two ways to mitigate the effect of string concatenation, the first of which is to try to reduce the size of the string being processed, and the second is to try to reduce the number of string assignment operations performed. See the revised version of the writehtml code shown below.
Function WriteHTML( Data )
Dim nRep
For nRep = 0 to 99
sHTML = sHTML & ( vbcrlf _
& "<TR><TD>" & (nRep + 1) & "< /TD><TD>" _
& Data( 0, nRep ) & "</TD>< TD>" _
& Data( 1, nRep ) & "</TD><TD> " _
& Data( 2, nRep ) & "</TD><TD>" _
& Data( 3, nRep ) & "</TD><TD>" _
& Data( 4, nRep ) & "</TD><TD>" _
& Data( 5, nRep ) & "</TD></TR>" )
Next
WriteHTML = sHTML
End Function
At first glance, it may be difficult to see the difference between this code and the previous code example. In fact, this code simply adds parentheses outside the SHTML = sHTML & content. This actually reduces the size of the strings processed in most string concatenation operations by changing the precedence order. In the original code example, the ASP compiler looks at the expression to the right of the equal sign and evaluates it from left to right. As a result, 16 connection operations are performed for each repetition, and these operations are performed for the ever-increasing sHTML. In the new version, we prompt the compiler to change the order of operations. It now evaluates the expression in order from left to right, from parentheses to outside parentheses. This technique allows each repetition to include 15 connection operations, which are for smaller strings that do not grow, and only one for the ever-growing large sHTML. Figure 1 shows the comparison between this optimization method and the standard connection method in memory usage patterns.
Figure 1: Comparison of standard and parenthesis connections in memory usage patterns
In certain cases, the use of parentheses can have a significant impact on performance and scalability, which will be further explained later in this article.