Efficiency
**********

FIXME: The material in the CMUCL manual about getting good performance from the compiler should be reviewed, reformatted in Texinfo, lightly edited for SBCL, and substituted into this manual. In the meantime, the original CMUCL manual is still 95+% correct for the SBCL version of the Python compiler. See the sections

  * Advanced Compiler Use and Efficiency Hints

  * Advanced Compiler Introduction

  * More About Types in Python

  * Type Inference

  * Source Optimization

  * Tail Recursion

  * Local Call

  * Block Compilation

  * Inline Expansion

  * Object Representation

  * Numbers

  * General Efficiency Hints

  * Efficiency Notes

Besides this information from the CMUCL manual, there are a few other points to keep in mind.

  * The CMUCL manual doesn't seem to state it explicitly, but Python has a mental block about type inference when assignment is involved. Python is very aggressive and clever about inferring the types of values bound with let, let*, inline function call, and so forth. However, it's much more passive and dumb about inferring the types of values assigned with setq, setf, and friends. It would be nice to fix this, but in the meantime don't expect that just because it's very smart about types in most respects it will be smart about types involved in assignments.  (This doesn't affect its ability to benefit from explicit type declarations involving the assigned variables, only its ability to get by without explicit type declarations.)

  * Since the time the CMUCL manual was written, CMUCL (and thus SBCL) has gotten a generational garbage collector. This means that there are some efficiency implications of various patterns of memory usage which aren't discussed in the CMUCL manual. (Some new material should be written about this.)

  * SBCL has some important known efficiency problems.  Perhaps the most important are

       - There is only limited support for the ANSI dynamic-extent declaration.  See Dynamic-extent allocation.

       - The garbage collector is not particularly efficient, at least on platforms without the generational collector (as of SBCL 0.8.9, all except x86).

       - Various aspects of the PCL implementation of CLOS are more inefficient than necessary.

Finally, note that Common Lisp defines many constructs which, in the infamous phrase, ``could be compiled efficiently by a sufficiently smart compiler''. The phrase is infamous because making a compiler which actually is sufficiently smart to find all these optimizations systematically is well beyond the state of the art of current compiler technology. Instead, they're optimized on a case-by-case basis by hand-written code, or not optimized at all if the appropriate case hasn't been hand-coded. Some cases where no such hand-coding has been done as of SBCL version 0.6.3 include

  * (reduce #'f x) where the type of x is known at compile time

  * various bit vector operations, e.g.  (position 0 some-bit-vector)

  * specialized sequence idioms, e.g.  (remove item list :count 1)

  * cases where local compilation policy does not require excessive type checking, e.g.  (locally (declare (safety 1)) (assoc item list)) (which currently performs safe endp checking internal to assoc).

If your system's performance is suffering because of some construct which could in principle be compiled efficiently, but which the SBCL compiler can't in practice compile efficiently, consider writing a patch to the compiler and submitting it for inclusion in the main sources. Such code is often reasonably straightforward to write; search the sources for the string ``deftransform'' to find many examples (some straightforward, some less so).

Dynamic-extent allocation
=========================

SBCL has limited support for performing allocation on the stack when a variable is declared dynamic-extent.  The dynamic-extent declarations are not verified, but are simply trusted; if the constraints in the Common Lisp standard are violated, the best that can happen is for the program to have garbage in variables and return values; more commonly, the system will crash.

As a consequence of this, the condition for performing stack allocation is stringent: either of the speed or space optimization qualities must be higher than the maximum of safety and debug at the point of the allocation.  For example:

     (locally
       (declare (optimize speed (safety 1) (debug 1)))
       (defun foo (&rest rest)
         (declare (dynamic-extent rest))
         (length rest)))

Here the &rest list will be allocated on the stack.  Note that it would not be in the following situation:

     (defun foo (&rest rest)
       (declare (optimize speed (safety 1) (debug 1)))
       (declare (dynamic-extent rest))
       (length rest))

because both the allocation of the &rest list and the variable binding are outside the scope of the optimize declaration.

There are many cases when dynamic-extent declarations could be useful. At present, SBCL implements

  * Stack allocation of &rest lists, where these are declared dynamic-extent.

  * Stack allocation of list and list*, whose result is bound to a variable, declared dynamic-extent, such as

          (let ((list (list 1 2 3)))
            (declare (dynamic-extent list)
            ...))

     or

          (flet ((f (x)
                   (declare (dynamic-extent x))
                   ...))
            ...
            (f (list 1 2 3))
            ...)

  * Stack allocation of closures, defined with flet or labels with a bound declaration dynamic-extent. Closed-over variables, which are assigned (either inside or outside the closure) are still allocated on the heap. Blocks and tags are also allocated on the heap, unless all non-local control transfers to them are compiled with zero safety.

Future plans include

  * Stack allocation of closures, where these are declared dynamic-extent;

  * Stack allocation of list, list* and cons (including following chains during initialization, and also for binding mutation), where the allocation is declared dynamic-extent;

  * Automatic detection of the common idiom of applying a function to some defaults and a &rest list, even when this is not declared dynamic-extent;

  * Automatic detection of the common idiom of calling quantifiers with a closure, even when the closure is not declared dynamic-extent.

Modular arithmetic
==================

Some numeric functions have a property: N lower bits of the result depend only on N lower bits of (all or some) arguments. If the compiler sees an expression of form (logand EXP MASK), where EXP is a tree of such ``good'' functions and MASK is known to be of type (unsigned-byte W), where W is a ``good'' width, all intermediate results will be cut to W bits (but it is not done for variables and constants!). This often results in an ability to use simple machine instructions for the functions.

Consider an example.

     (defun i (x y)
       (declare (type (unsigned-byte 32) x y))
       (ldb (byte 32 0) (logxor x (lognot y))))

The result of (lognot y) will be negative and of type (signed-byte 33), so a naive implementation on a 32-bit platform is unable to use 32-bit arithmetic here. But modular arithmetic optimizer is able to do it: because the result is cut down to 32 bits, the compiler will replace logxor and lognot with versions cutting results to 32 bits, and because terminals (here---expressions x and y) are also of type (unsigned-byte 32), 32-bit machine arithmetic can be used.

As of SBCL 0.8.5 ``good'' functions are +, -; logand, logior, logxor, lognot and their combinations; and ash with the positive second argument. ``Good'' widths are 32 on HPPA, MIPS, PPC, Sparc and x86 and 64 on Alpha.  While it is possible to support smaller widths as well, currently this is not implemented.