RE: [xsl] How smart are the XSLT processors? Are there any XSLT processors that convert tree-recursive functions into efficient iterative procedures?

Subject: RE: [xsl] How smart are the XSLT processors? Are there any XSLT processors that convert tree-recursive functions into efficient iterative procedures?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 28 Apr 2010 09:50:45 +0100
> Are there any XSLT processors that convert tree recursive 
> functions into more efficient iterative procedures?
> 
> /Roger
> 
> -------------------------------------------------
>      Version #1: Tree Recursion
> -------------------------------------------------
>     <xsl:function name="ex:fibonacci">
>       <xsl:param name="n" />
> 
>       <xsl:choose>
>           <xsl:when test="$n eq 0">
>               <xsl:value-of select="0" />
>           </xsl:when>
>           <xsl:when test="$n eq 1">
>               <xsl:value-of select="1" />
>           </xsl:when>
>           <xsl:otherwise>
>               <xsl:value-of select="ex:fibonacci($n - 1) + ex:fibonacci($n
- 2)" />
>           </xsl:otherwise>
>       </xsl:choose>
> 
>     </xsl:function>
>

I think it's unlikely that an XSLT implementor would optimize this to avoid
the exponential nature of the algorithm. The reason is that it's difficult
to find optimizations that would do this that are sufficiently general to
optimize a significant number of real user-written programs - there's no
point in spending effort on optimizations that only help with ivory-tower
examples. 

The most realistic optimization that one might attempt for this function is
to turn it automatically into a memo-function. That involves a space-time
tradeoff; automating a choice that requires a space-time tradeoff requires a
cost-based optimizer that understands the costs of space and time and is
able to estimate how much of either will be used. It's easy to look at this
particular function and produce an argument as to why it should be
implemented as a memo-function. It's not at all easy to generalize that
reasoning so it can produce a good answer for any function.

Optimization techniques are advancing all the time (just look at the
advances with just-in-time approaches that benefit from learning about the
behaviour of the code at run-time). But for this example, it's not there
yet.

I'd be interested how the performance of this compares with your other
implementation if you add saxon:memo-function="yes".

Regards,

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

Current Thread