Re: [xsl] Are xsl:key's best going into the future?

Subject: Re: [xsl] Are xsl:key's best going into the future?
From: Trevor Nash <tcn@xxxxxxxxxxxxx>
Date: Fri, 15 Mar 2002 15:22:34 +0000
On Fri, 15 Mar 2002 04:58:48 -0800, Robert Koberg wrote:

>I have heard a developer from Xalan say that he is working on making an
>operation like:
><xsl:apply-templates select="//*[@id=$id]"/>
>as fast as:
><xsl:apply-templates select="key('the-key', $id)">
In all cases?  I doubt it.

These are functionally equivalent (assuming the right declaration of
'the-key' of course!).  They are just two different ways of writing
the same thing, so any XSLT engine is free to use the same
implementation.  The reason most do not is that there is no algorithm
which is optimial in both the cases:
a) the expression is evaluated once
b) the expression is evaluated many times
So they tend to assume (a) for the XPath form and (b) for the key()
form.  In other words, by using XPath or key() you are telling the
XSLT engine more about the application, which it can then use to
produce a better optimised result.

>[ I also asked if you would gain the same performance if you did something
><xsl:apply-templates select="document($doc)//*[@id=$id]"> but no answer... I
>assume it is different?? ]
Probably the same, all you are doing is changing the context.

>In fact, if you want to get the incredible performance gains from XSLTC,
>[ Xalan's XSLTC team -
> ]
>warn that you should not use keys.
>Should keys be avoided?

Think about it: if you can rewrite a key() expression as an XPath,
then the XSLT compiler can as well.  Its not actually very difficult.

If they really do have a scheme where the XPath form is faster than
the key() form, then they should be doing the rewriting for you.
Otherwise, you are simply tying yourself to one processor.

Its the same with any other programming language.  When it's new, you
can get significant performance gains by writing your code in a
particular form, because the compilers are none too bright.  As time
goes on and more optimisation algorithms are implemented, you get the
freedom to write the code the way it reads best, because the compiler
rewrites it for you into the most efficient form.

Today, you make the choice between XPath or key() mostly on grounds of
performance, because XSLT compilers are relatively immature.  As time
goes on it should make less difference, so you get to use the form
which is easier to read.  I don't understand why the Xalan people seem
to be going in the opposite direction - perhaps you misuderstood them,
and this is a temporary situation.

Trevor Nash
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@xxxxxxxxxxxxx

 XSL-List info and archive:

Current Thread