[xslt] Stack depth question...



I have these two scripts.  I wrote them to test if libxslt support tail
recursion optimization.  I know now that it does not, but I did find
some behavior I do not understand in the process.  The first script
works when num is set to 2998, but fails when it is higher with a
message about max depth being 3000...  The second script fails when num
is set to 1875, but there is no error.  A debug/verbose is not showing
me any errors.  Now I know that libxslt can sum up to 2998 = 4495501. 
The sum through 1874 is 1756875.  So its not like the the number is
getting too big.  Can someone help me understand this?

###script 1####
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
   
    <xsl:template match="/">
   
        <output>
            <xsl:call-template name="additup">
                <xsl:with-param name="num" select="3000"/>
            </xsl:call-template>
        </output>
    </xsl:template>
   
    <xsl:template name="additup">
        <xsl:param name="sum_acc" select="0"/>
        <xsl:param name="num" select="0"/>
       
        <xsl:choose>
            <xsl:when test="$num = 0">               
                    <xsl:value-of select="$sum_acc"/>               
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="additup">
                    <xsl:with-param name="sum_acc" select="$sum_acc +
$num"/>
                    <xsl:with-param name="num" select="$num - 1"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
 
</xsl:stylesheet>
################




####script 2#########
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
   
    <xsl:template match="/">
   
        <output>
            <xsl:call-template name="additup">
                <xsl:with-param name="num" select="1875"/>
            </xsl:call-template>
        </output>
       
    </xsl:template>
   
    <xsl:template name="additup">       
        <xsl:param name="num" select="0"/>
       
        <xsl:choose>
            <xsl:when test="$num = 0">
                <xsl:value-of select="0"/>               
            </xsl:when>
            <xsl:otherwise>               
                <xsl:variable name="tailsum">
                    <xsl:call-template
name="additup">                       
                        <xsl:with-param name="num" select="$num - 1"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="$num + $tailsum"/>
            </xsl:otherwise>
        </xsl:choose>
       
       
    </xsl:template>
 
</xsl:stylesheet>
########################



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]