Re: complier optimization for DO returning REVERSE, WAS: Re: newbie exploring better ways



K Livingston <kevinlivingston.public@xxxxxxxxx> writes:

On Jan 30, 1:39 am, K Livingston <kevinlivingston.pub...@xxxxxxxxx>
wrote:
a DO with a REVERSE at the end has no side effects,
but the disadvantage of the extra traversal for the REVERSE

just out of curiosity (and possibly in bad form for replying to my own
comment), it seems that a reasonably common form is

(do ((results nil (cons <something> results))
...)
(( ... ) (reverse results)))

it strikes me that a clever compiler could unroll that, and not
require the second traversal and just build up results in reverse, as
it would while performing a MAPCAR or the like.

Well, first of all, the compiler would have to have some way of making
sure that there were no side effects contained in the ... or (even more
difficult), prove to itself that any side effects don't affect the
computation of the results.

For a simple example that would persumably give the compiler fits trying
to figure out:

(let ((counter 0)
(limit 10))
(do ((results nil (cons (incf counter) results)))
((>= counter limit) (reverse results))))

While it could do
this, do any compilers actually do that?

I doubt it. While unrolling a loop may be a standard compiler trick,
reversing the direction of loop traversal seems a bit much.


--
Thomas A. Russ, USC/Information Sciences Institute
.



Relevant Pages

  • Re: Reverse strings
    ... Your prototype for reverse says that the parameter is of type ... This will be automatically converted by the compiler to ... type char(*)[SIZE], that is pointer to array of SIZE char. ...
    (alt.comp.lang.learn.c-cpp)
  • complier optimization for DO returning REVERSE, WAS: Re: newbie exploring better ways
    ... but the disadvantage of the extra traversal for the REVERSE ... just out of curiosity (and possibly in bad form for replying to my own ... it seems that a reasonably common form is ... it strikes me that a clever compiler could unroll that, ...
    (comp.lang.lisp)
  • Re: Is it a good thing that program mix C and C++?
    ... You can call C routines from C++, but not the reverse. ... The C++ compiler does various unspeakable things to ... Since the C++ object code uses those adorned names, ...
    (comp.lang.c)
  • Re: Is it a good thing that program mix C and C++?
    ... You can call C routines from C++, but not the reverse. ... The C++ compiler does various unspeakable things to ... the function names in order to handle overloading, ...
    (comp.lang.c)
  • Re: No TR TR
    ... FYB, learn how to form a cogent post. ... I thought I was replying to you, ... Reverse name to reply" ...
    (rec.outdoors.fishing.fly)