inline, notinline
inline, notinline Declaration
Syntax:
(inline {function-name}*)
(notinline {function-name}*)
Arguments:
function-name—a function name.
Valid Context:
declaration or proclamation
Binding Types Aected:
functionDescription:
inline specifies that it is desirable for the compiler to produce inline calls to the functions named by function-names; that is, the code for a specified function-name should be integrated into the calling routine, appearing “in line” in place of a procedure call. A compiler is free to ignore this declaration. inline declarations never apply to variable bindings.
If one of the functions mentioned has a lexically apparent local definition (as made by flet or labels), then the declaration applies to that local definition and not to the global function definition.
inline, notinlineWhile no conforming implementation is required to perform inline expansion of user-defined functions, those implementations that do attempt to recognize the following paradigm:
To define a function f that is not inline by default but for which (declare (inline f)) will make f be locally inlined, the proper definition sequence is:
(declaim (inline f))
(defun f ...)
(declaim (notinline f))
The inline proclamation preceding the defun form ensures that the compiler has the opportunity save the information necessary for inline expansion, and the notinline proclamation following the defun form prevents f from being expanded inline everywhere.
notinline specifies that it is undesirable to compile the functions named by function-names in-line. A compiler is not free to ignore this declaration; calls to the specified functions must be implemented as out-of-line subroutine calls.
If one of the functions mentioned has a lexically apparent local definition (as made by flet or labels), then the declaration applies to that local definition and not to the global function definition.
In the presence of a compiler macro definition for function-name, a notinline declaration prevents that compiler macro from being used. An inline declaration may be used to encourage use of compiler macro definitions. inline and notinline declarations otherwise have no e↵ect when the lexically visible definition of function-name is a macro definition.
inline and notinline declarations can be free declarations or bound declarations. inline and notinline declarations of functions that appear before the body of a flet or labels form that defines that function are bound declarations. Such declarations in other contexts are free declarations.
Examples:
;; The globally defined function DISPATCH should be open-coded,
;; if the implementation supports inlining, unless a NOTINLINE
;; declaration overrides this effect.
(declaim (inline dispatch))
(defun dispatch (x) (funcall (get (car x) ’dispatch) x))
;; Here is an example where inlining would be encouraged.
(defun top-level-1 () (dispatch (read-command)))
;; Here is an example where inlining would be prohibited.
(defun top-level-2 ()
(declare (notinline dispatch))
(dispatch (read-command)))
;; Here is an example where inlining would be prohibited.
(declaim (notinline dispatch))
(defun top-level-3 () (dispatch (read-command)))
;; Here is an example where inlining would be encouraged.
Evaluation and
(defun top-level-4 ()
(declare (inline dispatch))
(dispatch (read-command)))
See Also:
declare, declaim, proclaim
Expanded Reference: inline, notinline
Requesting Inlining
The inline declaration advises the compiler to expand calls to the named function inline, potentially improving performance.
(declaim (inline square))
(defun square (x) (* x x))
=> SQUARE
;; Calls to square may be expanded inline by the compiler.
(defun sum-of-squares (a b)
(+ (square a) (square b)))
=> SUM-OF-SQUARES
Preventing Inlining with notinline
The notinline declaration tells the compiler not to inline the function. This is useful for debugging or when you need the function to be redefinable at run time.
(defun compute (x)
(declare (notinline square))
(square x))
=> COMPUTE
;; The call to square will NOT be inlined here.
Local inline and notinline
These declarations can be used locally within a form.
(defun fast-path (x)
(locally (declare (inline square))
(square x)))
=> FAST-PATH
(defun debug-path (x)
(locally (declare (notinline square))
(square x)))
=> DEBUG-PATH
Scope of the Declaration
An inline declaration applies only within the scope of the declaration, not to calls outside it.
(defun example (a b)
(let ((x (square a))) ; NOT affected by inline below
(locally (declare (inline square))
(+ x (square b))))) ; this call may be inlined
=> EXAMPLE