|when to destroy local objects w/ tail recursion ? firstname.lastname@example.org (john43) (2002-05-04)|
|Re: when to destroy local objects w/ tail recursion ? email@example.com (Joachim Durchholz) (2002-05-08)|
|Re: when to destroy local objects w/ tail recursion ? firstname.lastname@example.org (2002-05-08)|
|Re: when to destroy local objects w/ tail recursion ? email@example.com (Tim G) (2002-05-08)|
|Date:||8 May 2002 00:17:46 -0400|
|Organization:||Giganews.Com - Premium News Outsourcing|
|Posted-Date:||08 May 2002 00:17:46 EDT|
On 4 May 2002 14:17:30 -0400, john43 <firstname.lastname@example.org> wrote:
>If I have local objects that require finalization (i.e., calling some
>kind of destructor function), is it still reasonable to optimize tail
>recursion? If so, what is a good approach?
This really depends on the specific language semantics.
You can do it if the parameter is passed by value (which usually
doesn't make senses efficiency-wise) and the destructor can be called
any time after the object's last use before going out of scope (which
is undecidable in the general case).
You can also do it if the object type is really a pointer/reference to
something on the heap and the language specification does garbage
collection some time after the last reference to an object disappears
>For example, the destructor for object 'B' should be called at some
>function: f( object A)
> object B; // default constructor invoked
> return f( B ); // tail recursion
Assuming the destructor is called at the end of the object's scope
(i.e C++-like), this is equivalent to
function f(object A)
result = f(B);
which is no longer tail recursive.
Tail call optimization is implemented by turning the call into a jump.
The savings is realized by not having to re-execute the prolog code,
since the nonvolatile registers are already saved for the caller. The
smaller the function, the worse the overhead percentagewise.
Return to the
Search the comp.compilers archives again.