RE: [xsl] Benefits of using xsl:key

Subject: RE: [xsl] Benefits of using xsl:key
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 3 Nov 2009 09:30:19 -0000
> We often hear that the use of xsl:key can speed up a 
> transformation many times. But XSLT processors seem so 
> optimized for speed, anyway, these days, that I have found it 
> difficult to construct a test input file and a test XSLT 
> stylesheet demonstrating the dramatic wonders of using xsl:key.
> 
> How big should the input document be?
> How complicated the hierarchy, etc?
> 

Try this query, which is Q8 from the XQuery XMark benchmark translated into
XSLT.

<result xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xsl:version="2.0">
<!-- Q8.  List the names of persons and the number of items they bought.
          (joins person, closed_auction) -->

  <xsl:for-each select="/site/people/person">
    <xsl:variable name="a" 
       select="/site/closed_auctions/closed_auction[buyer/@person =
current()/@id]"/>
    <item person="{name}"><xsl:value-of select="count($a)"/></item>  
  </xsl:for-each>
  
</result>  

Run it against different sizes of database from the XMark benchmark. Observe
how with Saxon-HE the performance increases quadradically with database
size, while with Saxon-EE the increase is linear. That's because Saxon-EE
creates a key automatically. Then show how you can achieve the same effect
with Saxon-HE by hand-optimizing using xsl:key: introduce

<xsl:key name="k" match="/site/closed_auctions/closed_auction"
use="buyer/@person"/>

and change the variable initializer to

select="key('k', @id)"

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

Current Thread