3.1 Introduction to the Compiler
To turn on optimization reporting, call the functioncompile
,compile-file
, orcompiler-options
with the value of the:show-optimizations
keyword argument specified ast
:
> (compiler-options :show-optimizations t) (:SHOW-OPTIMIZATIONS T)This optimization report indicates that the function square would be more efficient if both the type of the argument x and the result of the form (* x) were explicitly declared as fixnum integers. Fixnum integers are integers that are represented as a single machine word; arithmetic that uses fixnum integers can be directly coded in the corresponding machine operations and is extremely fast. Thus, many optimization reports suggest fixnum declarations.> (defun square (x) (* x x)) SQUARE
> (compile 'square) ;;; ;;; FAILED TO OPTIMIZE the form: (* x x) ;;; For arg x: float ;;; fixnum ;;; For the result: fixnum SQUARE
The following example shows a version of the function square that has enough information to allow the Compiler to optimize the compiled code fully:
> (defun square (x) (declare (fixnum x)) (the fixnum (* x x))) SQUARENote that the report of a failure to optimize code does not mean that the code is incorrectly written. If the function square is intended to take fixnum and nonfixnum arguments, such as floating-point numbers, then no declarations should be added. Reports of unsuccessful optimizations merely show you when the Compiler could make optimizations if you want them.> (compile 'square) ;;; ;;; OPTIMIZED the form: (* x x) ;;; x is of type fixnum ;;; Result is of type fixnum SQUARE
The use of type declarations to optimize code is discussed in Section 3.3 on page 49.
If optimization reports were turned on by a call to the functioncompiler-options
, you can turn off the reports by calling the function with the:show-optimization
keyword argument set tonil
. If optimization reports were turned on by a call tocompile
or tocompile-file
, the reports are automatically turned off at the end of that compilation. You can show reports for only those optimizations that failed by setting the:show-optimization
keyword argument to:failure-only
.
Generated with Harlequin WebMaker