What does it mean to "open code" something in Common Lisp?
common-lisp
Solution
What It Is
Open-coding, AKA inlining, means replacing function calls with inline assembly. The idea is that `funcall` and `apply` are expensive (they require saving and restoring the stack &c) and replacing them with the few operations which constitute the function can be beneficial.
E.g., the function `1+` is a single instruction when the argument is a `fixnum` (which it usually is in practice), so turning the funcall into the two parallel branches (`fixnum` and otherwise) would be a win.
How to Control it
Declarations
They user can control this optimization explicitly by the `inline` declaration (which implementations are free to ignore).
The user can also influence this optimization by the `optimize` declaration.
Both will affect inlining the code of a function defined just as a function (see below).
Macros
The "old" way is to implement the function as a macro. E.g., instead of
(defun last1f (list)
(car (last list)))
write
(defmacro last1m (list)
`(car (last ,list)))
and `last1m` will be always open-coded. The problem with this approach is that you cannot use `last1m` as a function - you cannot pass it to, say, `mapcar`.
Thus Common Lisp has an alternative way - compiler macros, which tell the compiler how to transform the form before compiling it:
(define-compiler-macro last1f (list)
;; use in conjunction with (defun last1f ...)
`(car (last ,list)))
See also the excellent examples in the aforelinked CLHS page.
Its Effects on Optimization
A comment asked about the effects of inlining, i.e., what optimizations result from it. E.g., constant propagation in addition to eliminating a function call.
The answer to this question is left to implementations. IOW, the CL standard does not specify what optimizations must be done.
However, Minimal Compilation implies that if an implementation does something (e.g. constant folding), it will be done for compiler macros too.
For more details, you should compare the results of `disassemble` with and without the declarations and whatnot and see the effects.
For explanations, you should ask the vendor (e.g., by using the appropriate tag here - e.g., `sbcl`, `clisp`, &c).
Problem
In the SBCL user manual there are several references to the term "open code". Common Lisp hackers also use this term when referring to optimizing code. Could you please explain what it means to "open code" something and give an example of how it works?