Re: when to destroy local objects w/ tail recursion ?

journeyman@compilerguru.com (journeyman)
8 May 2002 00:17:46 -0400

          From comp.compilers

Related articles
when to destroy local objects w/ tail recursion ? john43@temple.edu (john43) (2002-05-04)
Re: when to destroy local objects w/ tail recursion ? joachim_d@gmx.de (Joachim Durchholz) (2002-05-08)
Re: when to destroy local objects w/ tail recursion ? journeyman@compilerguru.com (2002-05-08)
Re: when to destroy local objects w/ tail recursion ? timgspam@comcast.net (Tim G) (2002-05-08)
| List of all articles for this month |
From: journeyman@compilerguru.com (journeyman)
Newsgroups: comp.compilers
Date: 8 May 2002 00:17:46 -0400
Organization: Giganews.Com - Premium News Outsourcing
References: 02-05-024
Keywords: OOP, optimize
Posted-Date: 08 May 2002 00:17:46 EDT

On 4 May 2002 14:17:30 -0400, john43 <john43@temple.edu> 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
(i.e. Java-like).


>For example, the destructor for object 'B' should be called at some
>point
>
>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)
      {
            object B;
            sometype result;
            ...
            result = f(B);
            destroy B;
            return result;
      }
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.


HTH,


Morris


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.