3.2 Making declarations

3.2.6 INLINE and NOTINLINE declarations

Aninline declaration asks the Compiler to replace the procedure call to a function with the machine-language code for that function. Using the machine-language code for a function is called in-line coding or open coding.

Using in-line code eliminates some of the run-time overhead in calling functions. In-line coding is especially useful for simple functions; it is not as useful for large functions where the time needed to call the function is small compared to the execution time of the function. In-line coding should be used selectively because it usually increases the size of compiled code.

A notinline declaration tells the Compiler not to use in-line code for a function, even for a function it would normally code in line. If a function is declarednotinline, a call to the function replaces the in-line code.Notinline declarations can be useful when you are debugging code. The Compiler can choose to ignore aninline declaration; it must obey anotinline declaration.

Local functions that have been defined by using theflet or the labels special form can be declaredinline with aninline declaration, as shown in the following example:

;;; Compute the distance between two 2-dimensional points.
(defun distance-2d (p1 p2)
  (flet ((square (x) (* x x)))
    (declare (inline square))
    (flet ((square-d (x1 x2) (square (- x2 x1))))
      (declare (inline square-d))
      (sqrt
       (+ (square-d (point-x p1) (point-x p2))
          (square-d (point-y p1) (point-y p2)))))))

To declare top-level functions, use aninline proclamation. The proclamation must precede the definition of the proclaimed function, as shown in the following example:

(proclaim '(inline square))
(defun square (x) (* x x))
You can also use the functiondefsubst to simultaneously define a function and declare itinline. The following expression produces the same result as the proclamation and definition in the preceding example:

(defsubst square (x) (* x x))
Unlessnotinline declarations are used, the Compiler automatically generates in-line code for many system-provided functions, such ascar andcdr. The Compiler normally codes the following functions in line:

Although in-line code is usually fast, you might be unable to trace calls to the in-line function during execution or to dynamically redefine the in-line function. Declaring many user-defined functions asinline can also increase code size. In particular, all themake-<foo> functions are automatically inlined, so in the case of constructors with large number of slots, compilation can dramatically increase the size of the code. This is usually not a problem, as most structures are small, and hence the constructors are small also. If your code contains large constructors, you can save space by proclaiming or declaring the constructornotinline to keep it from being expanded inline.


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker