[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How smart are the XSLT processors? Are there any X

Subject: Re: How smart are the XSLT processors? Are there any XSLT processors that convert tree-recursive functions into efficient iterative procedures?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 28 Apr 2010 00:51:27 +0100
Re:  How smart are the XSLT processors? Are there any X
On 28/04/2010 00:27, Costello, Roger L. wrote:
Hi Folks,

Below are two implementations of the Fibonacci sequence.

The first version is a direct translation of the definition of the Fibonacci sequence into XSLT -- it is declarative code.

The second version is an iterative approach -- it is imperative code.

not really, it's written in a declarative language (XSLT) and is just a direct translation of a different definition.

The advantage of the first version is that it is easy to specify and understand. It's disadvantage is that, if the XSLT processor is not smart, it is highly inefficient -- Fib(n) grows exponentially with n.


However, a "smart compiler" could transform tree-recursive functions into more efficient iterative procedures that compute the same result -- a smart XSLT processor could convert the first version into the second version.


Most (some) (saxon at least) xslt systems will do tail recursion elimination, but your first version isn't tail recursive as it calls itself twice. The second version is tail recursive.


I ran both versions on Fib(100). The second version finished in less than 1 second. As for the first (declarative) version, after 10 minutes it was still executing so I killed the process.
rewriting an arbitrary expression so it's tail recursive is pretty hard (impossible in general and hard to spot the cases where it's possible)
No doubt someone who implemets this will say that they can rewrite some cases


I conclude that, at least for the XSLT processor I used, it does not convert the first version into more efficient iterative procedures. This is sad. I would prefer writing declarative code, but if XSLT processors do not optimize the execution of declarative code then how realistic is declarative programming?


You are making it pretty inefficient in any case by using value-of (or relying on your xslt engine to optimise this away) by using value-of you are asking for a document node with a text node child with string value the decimal expansion of the number, whereas if you'd used xsl;sequence it would just have returned the integer. converting between a number and a string with its decimal expansion is a surprisingly expensive operation, as is generating a document and text node, which have unique identity.


Are there any XSLT processors that convert tree recursive functions into more efficient iterative procedures?

yes


/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>

-------------------------------------------------
      Version #2: Tree Iterator
-------------------------------------------------
     <xsl:function name="ex:fibonacci">
       <xsl:param name="n" />

<xsl:value-of select="ex:fibonacci-iterator(1, 0, $n)" />

</xsl:function>


<xsl:function name="ex:fibonacci-iterator"> <xsl:param name="a" /> <xsl:param name="b" /> <xsl:param name="count" />

       <xsl:choose>
           <xsl:when test="$count eq 0">
               <xsl:value-of select="$b" />
           </xsl:when>
           <xsl:otherwise>
               <xsl:value-of select="ex:fibonacci-iterator($a + $b, $a, $count - 1)" />
           </xsl:otherwise>
       </xsl:choose>

</xsl:function>


________________________________________________________________________ This e-mail has been scanned for all viruses by Star. ________________________________________________________________________

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.