|
Summary
Using variables to tweak the performance of your XSLT
This article briefly discusses the ways in which XSLT variables can be used to enhance the overall performance of XSLT stylesheets. Obviously, these performance enhancements only become relevant when handling large amounts of data - although the cumulative effect may even be sufficiently beneficial to use on moderately sized input XML data.
Using the XML listed below - say you wanted to output all of the data for each <data> element plus one peice of static data from another part of the input XML (in this example, the <static_data> node). The XSLT for this would be fairly straight forward, e.g.
Example A:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:for-each select=root/data> <xsl:value-of select=/root/static_data[1]/> <xsl:value-of select=./> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Although more careful inspection might suggest that placing the static data from the /root/static_data[1] element into a variable might enhance the performance, e.g.
Example B:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:variable name=something_static select=root/static_data[1]/> <xsl:for-each select=root/data> <xsl:value-of select=$something_static/> <xsl:value-of select=./> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The performance results of these two stylesheets is quite different (comparisons made using MSXML3 with MSXML4 figures in square brackets):- Example B has a 48% [55%] reduction in time taken over Example A. The logic of this performance increase is that the XSLT has been saved from continually having to re-lookup the value of the <static_data> node.
In Example B the data from the <static_data> node is stored as a node-set in the variable - but we are repeatedly outputing this value as a string. So what about if we 'cast' the value to a string in the first place (when the value is placed into the variable) and see if this gives a better result, e.g.
Example C:- <?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:template match=/> <xsl:variable name=something_static select=string(root/static_data[1])/> <xsl:for-each select=root/data> <xsl:value-of select=$something_static/> <xsl:value-of select=./> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The performance results of these two stylesheets is:- Example C has a 39% [22%] reduction in time taken over Example B. And the cumulative results of the final modification over the first incarnation is:- Example C has a 69% [65%] reduction in time taken over Example A.
So the principles of this performance enhancement are:- * if something static is used multiple times - retrieve it only once; * cast values placed in variables to a string as early as possible.
Cheers - Marrow http://www.MarrowSoft.com - home of Xselerator (XSLT IDE and debugger) http://www.TopXML.com/Xselerator
| Further additional information | |
Updating comments...
Updating comments...
|