3 Optimizing Lisp Programs
compile
function-or-method&optional
function-definition&rest
compiler-options
compile
compiles an interpreted function or method in the current Lisp environment and replaces the interpreted function or method definition with the compiled version. For interpreted functions,compile
produces a compiled function object from a lambda expression. The lambda expression is the argument function-definition if it is present; otherwise, the function definition of the symbol function-or-method is the relevant lambda expression.
compile
specifies a function name, a method object, or the name of a method to be compiled.
compile
returns that symbol. If the function-or-method argument isnil
and the optional function-definition argument is supplied,compile
returns the function object.
compile
compiles the specified method and returns the method name.
method
generic-function-name {qualifiers}* ({specializer-name}*))
compiler-options
. These keyword arguments affect only the specified compilation. The keyword arguments are extensions to Common Lisp.
> (defun double (x) (+ x x)) DOUBLE> (compile 'double) DOUBLE
> (double 2) 4
> (funcall (compile nil '(lambda (x) (+ x x))) 3) 6
;;; Define the method ADD-ONE and create its associated ;;; generic function. > (defmethod add-one ((x number)) (+ x 1)) #<Standard-Method ADD-ONE (NUMBER)>
> (compile '(method add-one (number))) (METHOD ADD-ONE (NUMBER))
> (add-one 5) 6
compile-file
, compiler-options
, uncompile
compile-file
input-pathname&key :output-file &rest
compiler-options
compile-file
produces binary files from Lisp source files. The compiled functions contained in the binary files become available for use when the binary files are loaded into Lisp.
*default-pathname-defaults*
by using the Common Lisp functionmerge-pathnames
.compile-file
searches for a file that matches the given pathname.compile-file
searches for source files by merging successive elements of*load-source-pathname-types*
with the given pathname until it finds a match. If a source file does not exist,compile-file
signals a continuable error and prompts for a new filename.:output-file
keyword argument controls where the compiled code is written. If this option is not specified or if its value isnil
, the name of the output file is the pathname specified by the input-pathname argument with the following exception: the type component is taken from the first element of the list stored in the variable*load-binary-pathname-types*
; the initial value of this element is ("lbin"
).
load-binary-pathname-types*
.
:output-file
isnil
.
compiler-options
. These keyword arguments affect only the specified compilation.
compile
, compiler-options
, load, *load-binary-pathname-types*
Function
compiled-function-p
object
compiled-function-p
is true if its argument is a compiled function object; otherwise, it is false.
> (compiled-function-p (symbol-function 'append)) Tcompiler-options Function> (compiled-function-p #'(lambda (x) x)) NIL
compiler-options &key :messages :file-messages :optimizep-message :warnings :undef-warnings :show-optimizations :fast-entry :write-safety :read-safety :tail-merge :notinline :egc
compiler-options
sets the default values of the keyword arguments to the functionscompile
andcompile-file
.
compiler-options
: t
sends messages to the standard output stream; otherwise, the value must specify a stream to which messages can be sent. The initial value is nil
, which suppresses Compiler messages.
:file-messages
"Reading source file..."
,"Writing binary file..."
, and messages that give the current settings forspeed
,safety
, space
, andcompilation-speed
. A value ofnil
means issue no file messages; otherwise, the value should specify a stream to which messages can be sent. The initial value ist
, which sends the messages to the standard output stream.
:optimize-message
nil
suppresses mode messages. A value of:terse
allows terse messages. The initial value ist
, which allows full optimization mode messages.
:warnings
nil
means issue no warnings; otherwise, the value must be either t
or a stream to which warnings can be sent. The initial value ist
, which sends the warnings to the stream that is the value of the Common Lisp variable *error-output*
.
:undef-warnings
nil
suppresses warnings. The initial value ist
.
:show-optimizations
nil
value shows optimization reports. The value:failure-only
causes the Compiler to report only those optimizations that failed. The initial value isnil
.
:fast-entry
nil
value means the Compiler does not insert checking in the compiled code; calls to functions compiled in this manner are slightly faster. Settingsafety
to 0 has the same effect on fast entry as setting:fast-entry
tot
. The initial value of:fast-entry
isnil
.
:write-safety
nil
value means the Compiler adds checking. Settingsafety
to 2 has the same effect on write access as setting:write-safety
tot
. The initial value of:write-safety
ist
.
:read-safety
nil
value means the Compiler adds checking. Settingsafety
to 3 has the same effect on read access as setting:read-safety
tot
. The initial value of:read-safety
ist
.
:tail-merge
nil
value causes the Compiler to convert tail-recursive calls to iterative constructions and to perform other types of tail call optimizations, such as tail merging, that eliminate the overhead of some function calls. Settingspeed
to 3 has the same effect on tail merging as setting:tail-merge
tot
. The initial value of:tail-merge
isnil
.
:notinline
nil
value, the Compiler behaves as if all functions have been declarednotinline
; the compiled code uses procedure calls to functions rather than the machine-language code for the functions. Settingspeed
to 0 has the same effect on in-line calls as setting:notinline
tot
. The initial value of:notinline
isnil
.
:egc
nil
value, the Compiler produces code that can be executed when ephemeral garbage collection is on. If the value isnil
, the Compiler produces code that is incompatible with ephemeral garbage collection. Attempting to run such code when ephemeral garbage collection is already turned on signals a continuable error. The initial value of:egc
ist
. See Chapter 5, "Storage Management in Common Lisp" in The User's Guide for more information about ephemeral garbage collection.
:target
.:target
mips-little-endian
mips-big-endian
:target
option.
mips
compile
, compile-file
Syntax:declare
{decl-spec}*
decl-spec::= (special
{var}*)
| (type
type-specifier {var}*)
| (type-specifier {var}*)
| (ftype
type-specifier {function-name}*)
| (function
function-name ({type-specifier}*) {type-specifier}*
| (restrictive-ftype
type-specifier {function-name}*)
| (type-reduce
type1 type2)
| (inline
{function-name}*)
| (notinline
{function-name}*)
| (ignore
{var}*)
| (optimize
{(quality value) | quality}*)
| (declaration
{declaration-name}*)
| (arglist
declared-arguments)
| (dynamic-extent
variable)
quality::=speed
|space
|safety
|compilation-speed
value::= 0 | 1 | 2 | 3
declare
special form makes declarations within certain forms. Declarations can occur in lambda expressions and in the following forms:
defgeneric define-setf-method defmacro defmethod defsetf deftype defun do do* do-all-symbols do-external-symbols do-symbols dolist dotimes flet labels let let* locally macrolet multiple-value-bind prog prog* with-input-from-string with-open-file with-open-stream with-output-to-string with-output-to-string
> (defun example (y) ; This y is regarded (declare (special y)) ; as special. (let ((y t)) ; This y is regarded (list y ; as lexical. (locally (declare (special y)) y)))) ; This y refers to the ; special binding of y. EXAMPLE> (example nil) (T NIL)
locally
, def-compiler-macro
def-compiler-macro
name lambda-list {form}*
def-compiler-macro
defines a Compiler macro. A Compiler macro is a macro that affects only compiled code, not interpreted code.
def-compiler-macro
associated with the same name as a function defined withdefun
or a macro defined withdefmacro
. During compilation, thedef-compiler-macro
definitions override defun
anddefmacro
definitions.
def-compiler-macro
.
defmacro
form.
> (defmacro xx1 (x) '(format nil "~A interpreted" ,x)) XX1> (def-compiler-macro xx1 (x) '(format nil "~A compiled" ,x)) XX1
> (defun xx2 () (xx1 "test is")) XX2
> (xx2) "test is interpreted"
> (compile 'xx2) XX2
> (xx2) "test is compiled"
undef-compiler-macro
;defmacro
(in CLtL2)
defsubst
name lambda-list {form}*
defsubst
defines a function and makes its definition available for in-line expansion by the Compiler.
defsubst
form.
defsubst
new-function ...) is equivalent to the following expression:
(progn (proclaim '(inline new-function)) (defun new-function ...))
defsubst
has been compiled, it cannot be traced or redefined.
> (defsubst test1 () "This is a test") TEST1> (defun test2 () (test1)) TEST2
> (trace test1) (TEST1)
> (test2) 1 Enter TEST1 1 Exit TEST1 "This is a test" "This is a test"
> (compile 'test2) TEST2
> (test2) "This is a test"
> (untrace test1) (TEST1)
> (defun test1 () "This is a new test") TEST1
> (test2) "This is a test"
proclaim
;defun
(in CLtL2)
disassemble
name-or-compiled-function
disassemble
disassembles a compiled function and prints the resulting code on the standard output. It returns nil
.
*standard-output*
.
enable-stack-lists &key :max-depth :max-length
disable-stack-lists
&rest
arguments declared to have dynamic extent on a stack instead of in dynamic memory. This reduces the number of garbage collections.enable-stack-lists
turns on stack list allocation.disable-stack-lists
disables stack list allocation. The Compiler then generates code that allocates&rest
arguments in dynamic storage.:max-depth
keyword argument specifies the maximum number of stack lists that can be allocated at any time. If specified, the value of:max-depth
must be an integer between 10 and 10000 inclusive. The default value is 100.
:max-length
keyword argument specifies the maximum number of cons cells that can be allocated in stack lists at any time. If specified, the value of:max-length
must be an integer between 100 and 100000. The default value is 1000.
:max-depth
and:max-length
arguments are given smaller values than were previously used.
enable-stack-lists
to increase the available stack list space or to reenable stack list allocation after a call to disable-stack-lists
.
eval-when
({situation}*) {form}*
eval-when
specifies when a particular body of code will be executed.
eval
, the evaluator evaluates the form arguments at execution time.compile
, the Compiler evaluates the form arguments at compilation time.load
and the file containing theeval-when
is compiled, and then the forms are compiled; they are executed when the output file produced by the Compiler is loaded.eval-when
. If no forms are executed,eval-when
returnsnil
.
> (setq test 3) 3locally Macro> (eval-when (compile) (setq test 2)) NIL
> test 3
> (eval-when (eval) (setq test 2)) 2
> test 2
locally
{declaration}* {form}*
locally
macro makes local declarations that affect only its form arguments.
> (defun example (y) ; This y is regarded (declare (special y)) ; as special. (let ((y t)) ; This y is regarded (list y ; as lexical. (locally (declare (special y)) y)))) ; This y refers to the ; special binding of y. EXAMPLE> (example nil) (T NIL)
declare
proclaim
decl-spec
proclaim
function makes a global declaration, or proclamation.
special
makes all occurrences of that variable name special references.
proclaim
is evaluated. It can therefore be a computed declaration specifier.
> (proclaim '(special prosp)) T> (setq prosp 1 reg 1) 1
> (let ((prosp 2)(reg 2)) (set 'prosp 3)(set 'reg 3) (list prosp reg)) (3 2)
> (list prosp reg) (1 3)
unproclaim
;defvar
,defparameter
(in CLtL2)
report-compiler-options
report-compiler-options
produces a formatted report of the current values of the keyword arguments to the functionscompile
,compile-file
, andcompiler-options
, as well as the current values of the optimization levels.
compile
, compile-file
, compiler-options
Function
reset-compiler-options
reset-compiler-options
sets the values of the keyword arguments to the functionscompile
,compile-file
, andcompiler-options
to their initial values. It does not change the current value of the:target
keyword argument (applies to LCL/SGI only).
compile
, compile-file
, compiler-options
,report-compiler-options
Variable
*style-warnings
*
nil
, no warnings are issued. The default value ist
.
the
value-type form
the
specifies that the value produced by a form will be of a certain type.
the
special form returns the value or values that result from form.
setf
withthe
declarations. In this case the declaration is transferred to the form that specified the new value. The resultingsetf
form is then used.
> (the list '(a b)) (A B)uncompile> (the (values integer list) (values 5 '(a b))) 5 (A B)
> (let ((i 100)) (declare (fixnum i)) (the fixnum (1+ i))) 101
Function
uncompile
function-or-method
uncompile
replaces the compiled version of the specified function, macro, or method with its original interpreted definition. The function, macro, or method must have been compiled by a call to the functioncompile
; otherwise, an error is signaled.
uncompile
specifies a function name, a macro name, a method object, or the name of a method. A method name has the following form:
method
generic-function-name {qualifiers}* ({specializer-name}*))
compile
Macro
undef-compiler-macro
name
undef-compiler-macro
removes a Compiler macro definition.
def-compiler-macro
Function
unproclaim
decl-spec
unproclaim
reverses a proclamation.
declare
; otherwise, an error is signaled. With the exception of theoptimize
declaration, the declaration specifier must exactly match the most recently proclaimed declaration specifier. If it does not, the call tounproclaim
has no effect.
unproclaim
is called with a declaration specifier of the form (optimize quality), quality is set to its default value. Ifunproclaim
is called with a declaration specifier of the form (optimize (quality value)), quality is reset to its default value only if the current value of quality is value; otherwise, the call has no effect.
proclaim
Macro
with-compiler-options
options&body
body
with-compiler-options
evaluates a specified series of forms.
compiler-options
. The keyword arguments affect only the specified series of forms.
compiler-options
Macro
with-deferred-warnings
{form}*
with-deferred-warnings
defers undefined function warnings until the end of the execution of the specified forms.
Generated with Harlequin WebMaker