Luis Oliveira | 3 Jul 2009 16:28
Picon
Gravatar

[PATCH] Faster WHICHEVER

Hello,

I wrote some code yesterday that used a few big WHICHEVER forms (with 30
or so possibilities) and that turned out to be a bit slow. The attached
patch simplifies the implementation of WHICHEVER and its performance
doesn't seem to degrade as the number of possibilities increases.

Here are both macro expansions for comparision:

CL-USER> (macroexpand-1 `(alexandria:whichever * * *))
(CASE (RANDOM 3) (0 *) (1 *) (2 *))
T
CL-USER> (macroexpand-1 `(alexandria::whichever-old * * *))
(LET ((#:FUNCTION940 (LAMBDA () *)))
  (DECLARE #'#:FUNCTION940)
  (WHEN (ZEROP (RANDOM 2)) (SETF #:FUNCTION940 (LAMBDA () *)))
  (WHEN (ZEROP (RANDOM 3)) (SETF #:FUNCTION940 (LAMBDA () *)))
  (FUNCALL #:FUNCTION940))
T

Am I missing some advantage of the more complex implementation?

Some timings:

CL-USER> (compile nil `(lambda () (loop repeat 1000000 do
                                    (alexandria::WHICHEVER-OLD
                                     , <at> (loop repeat 30 collect '*)))))
#<FUNCTION (LAMBDA ()) {10037E64D9}>
NIL
NIL
(Continue reading)


Gmane