[plt-scheme] quote-syntax vs. quote

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Mar 30 23:01:42 EST 2004

At Mon, 29 Mar 2004 04:25:35 -0500, Doug Orleans wrote:
> I've been trying to track down some unbearable slowness in my code,
> and I finally isolated it to a quote-syntax expression, which changing
> to quote instead results in a 5x speedup.  I'm pretty sure that the
> slowdown must be happening somewhere further down the dataflow, but I
> thought I'd ask just to be sure: is there some reason why quote-syntax
> would be an order of magnitude slower than quote?

The compiled form of a `quote' expression is an immediate constant, but
the compiled form of a `quote-syntax' form involves an indirection
through the Scheme stack. (The indirection is necessary because the
meaning of a `quote-syntax' expression depends on the phase in which
the code is executed. Without the indirection, code in a module body
would have to be duplicated for every instantiation.)

The indirection alone couldn't account for a x5 slowdown, but it might
trigger closure allocation that makes a significant difference. For
example, a `lambda' expression that contains only a quote expression is
a compile-time constant, whereas a `lambda' expression that contains a
`quote-syntax' creates a new closure every time that the expression is
evaluated.

 > (time (let loop ([x #f][n 400000]) 
            (unless (zero? n) 
               (loop (lambda () 'a) (sub1 n)))))
 cpu time: 150 real time: 166 gc time: 0

 > (time (let loop ([x #f][n 400000]) 
            (unless (zero? n) 
               (loop (lambda () #'a) (sub1 n)))))
 cpu time: 320 real time: 340 gc time: 120

(Obviously, any decent compiler would lift the `lambda' expression out
of the loop, or even eliminate it entirely.)

Matthew



Posted on the users mailing list.