chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Performance difference with named let?


From: Felix
Subject: Re: [Chicken-users] Performance difference with named let?
Date: Sat, 13 Jul 2013 16:51:53 +0200 (CEST)

From: Frank <address@hidden>
Subject: [Chicken-users] Performance difference with named let?
Date: Fri, 12 Jul 2013 15:45:09 -0400

> Hi,
>     Is there any benefit/cost associated with using a named let instead of
> creating a separate procedure?
> 

It doesn't make a difference whether you use an internal "define" or named let.
In the end the compiler is likely to create the same code. If you move a
named let (often a loop) to toplevel, on the other hand, then it will make
a difference: the global variable may be redefined, so every call must
read the value of the variable that contains the procedure being called,
and this has a certain cost. An internal definition will not be checked
and called directly (unless there happens to be an assignment), a named
let is mostly equivalent:

(define (loop ...) 
  ...
  (loop))            ; <- "loop" may be redefined, call must be indirect

(let loop (...)
  ...
  (loop))            ; <- "loop" is known here, call can be direct and is faster

If you compile in "block" mode or call a nonexported procedure inside
a module, and the first variant will be the same as the second, again
only if there are no assignments to "foo".

A historical detail that may be of interest: the MacScheme compiler
(and perhaps others of the Clinger-heritage) had an optimization setting called
"benchmark-mode", that transformed

(define (foo x) ...)

into

(define foo
  (letrec ((foo (lambda (x) ...)))
    foo))

thus assuming recursive calls will always be unaffected by redefinitions.

And finally, calls to unexported global or internally defined local procedures
can sometimes be back-transformed into "direct style" (instead of the normal
continuation-passing style used in code generated by CHICKEN), if they don't 
allocate
any storage (say if you perform integer calculations in a loop) which achieves
very tight and fast code.


cheers,
felix



reply via email to

[Prev in Thread] Current Thread [Next in Thread]