You can define an inlinable procedure by using
define-inlinable
instead of define
. An inlinable
procedure behaves the same as a regular procedure, but direct calls will
result in the procedure body being inlined into the caller.
Bear in mind that starting from version 2.0.3, Guile has a partial evaluator that can inline the body of inner procedures when deemed appropriate:
scheme@(guile-user)> ,optimize (define (foo x) (define (bar) (+ x 3)) (* (bar) 2)) $1 = (define foo (lambda (#{x 94}#) (* (+ #{x 94}# 3) 2)))
The partial evaluator does not inline top-level bindings, though, so
this is a situation where you may find it interesting to use
define-inlinable
.
Procedures defined with define-inlinable
are always
inlined, at all direct call sites. This eliminates function call
overhead at the expense of an increase in code size. Additionally, the
caller will not transparently use the new definition if the inline
procedure is redefined. It is not possible to trace an inlined
procedures or install a breakpoint in it (see Traps). For these
reasons, you should not make a procedure inlinable unless it
demonstrably improves performance in a crucial way.
In general, only small procedures should be considered for inlining, as making large procedures inlinable will probably result in an increase in code size. Additionally, the elimination of the call overhead rarely matters for large procedures.
Define name as a procedure with parameters parameters and bodies body1, body2, ....