Re: efficiency of let used in a binding

From: Lars Thomas Hansen <lth_at_ccs.neu.edu>
Date: Mon, 20 Oct 1997 10:51:48 -0400

Writes Felix Lee <flee_at_teleport.com> in response to Brian Denheyer <briand_at_soggy.aqua.com>:

>some scheme compilers will allocate some local vars on the
>stack, but most scheme interpreters (such as Stk) don't do
>it at all, because it's hard to tell if it's safe to do so.

Some Scheme compilers allocate local variables in registers (at least
Steele's Rabbit, Kranz's Orbit, and Clinger's Twobit do) when it is
possible to do so, which is almost always: in Twobit, only locals that
are the targets of assignments get heap-allocated, and the stack is only
used for saving registers across non-tail calls.

>but I wouldn't worry too much about it. in this code:
>
>> (define (mouse-motion x y)
>> (let ((new-x (transform x))
>> (new-y (transform y)))
>> ...))
>> (bind drawing "<Motion>" mouse-motion)
>
>you're already getting heap allocation on every
>mouse-motion, for the vars 'x' and 'y'.

Well, in STk you are, which is one of the reasons STk is quite slow (or
was, last I looked). The aforementioned compilers would allocate x and
y to registers as well.

If it turns out that garbage collecting variable cells is a major
bottleneck in the program, then switching to a generational collector
might help a great deal, because it makes the garbage collection of
short-lived objects cheap. Writing a generational collector for STk is
left as an exercise for the reader.

In the given situation, rewriting the program as

 (define (mouse-motion x y)
   (set! new-x (transform x))
   (set! new-y (transform y))
    ...)

would probably fix it, in the case of STk, but might confuse other
systems.

--lars
Received on Mon Oct 20 1997 - 15:52:09 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST