3.1 Introduction to the Compiler

3.1.4 Showing optimization reports

The Compiler can help you optimize code by displaying reports about the optimization attempts that it makes while it is compiling code. Optimization reports show when the Compiler has optimized a portion of code and when it has not optimized a portion of code but could if the source code had more type information. Optimization reports also tell you when the Compiler optimizes tail calls and shared data objects.

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)

> (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

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.

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)))

> (compile 'square) ;;; ;;; OPTIMIZED the form: (* x x) ;;; x is of type fixnum ;;; Result is of type fixnum SQUARE

Note 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.

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.

The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker