Re: [xsl] How is memory allocated in recursive XSLT templates?

Subject: Re: [xsl] How is memory allocated in recursive XSLT templates?
From: "Rashmi Rubdi" <rashmi.sub@xxxxxxxxx>
Date: Wed, 2 May 2007 16:29:49 -0400
Hi Michael,

Thank you for your reply, it is very informative. I now understand the
concept of what happens with the tail recursion.

Also the fact that recursive functions can be turned into iterations
is very interesting and something that's new to me, and I will take a
look at that further.

Thank you for clearing my doubts of infiniteness with Recursion.

-Regards
Rashmi

On 5/2/07, Michael Kay <mike@xxxxxxxxxxxx> wrote:
Well, clearly implementations may differ.

At first sight you are right: recursive template calls will consume an
amount of stack space proportional to the depth of recursion, and if you
code goes into infinite recursion you will hit some limit on the amount of
stack space available, which will cause execution to crash in some way.

However, there's a well-known optimization in functional programming
languages whereby recursive calls can sometimes be turned into iterations,
and if this happens then an infinite recursion can become an infinite
iteration, which won't terminate due to lack of resources. This applies to
functions that are "tail-recursive": basically, if the calling code doesn't
need to do anything with the result of the called code, then instead of the
sequence (make recursive call, then pop stack) it can do (pop stack, then
make recursive call). Saxon will do this for the template in your example.

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

Current Thread