Search code examples
compilationlispcommon-lispsbcl

What's the differences from inline and block compilation of SBCL?


Several weeks ago, SBCL updated 2.0.2 and brought the Block compilation feature. I have read this article to understand what it is.

I have a question, what's the difference between (declaim (inline 'some-function)) and Block compilation? Block compilation is automatic by the compiler?

Thanks.


Solution

  • Inline compilation is a specific optimization technique. A function being called is directly integrated into the calling function - usually using its source code - and then compiled.

    This means that the inlined function might not be inlined only in one function, but in multiple functions.

    Advantage: the overhead of calling a function disappears.

    Disadvantage: the code size increases and the calling function(s) needs to be recompiled, when the inlined function changed and we want this change to become visible. Macros have the same problem.

    Block compilation means that a bunch of code gets compiled together with different semantic constraints and that this enables the compiler to do a bunch of new optimizations.

    Common Lisp has in the standard support for block compilation of single files. It allows the file compiler to assume that a file is such a block of code.

    Example from the Common Lisp standard:

    3.2.2.3 Semantic Constraints

    A call within a file to a named function that is defined in the same file refers to that function, unless that function has been declared notinline. The consequences are unspecified if functions are redefined individually at run time or multiply defined in the same file.

    This allows the code to call a global function and not use the symbol's function cell for the call. Thus this disables late binding for global function calls - in this file and for functions in this file.

    It's not said how this can be achieved, but the compiler might just allocate the code somewhere and the calls just jump there.

    So this part of block compilation is defined in the standard and some compilers are doing that.

    Block compilation for multiple files

    If the file compiler can use block compilation for one file, then what about multiple files? A few compilers can also tell the file compiler that several files make a block for compilation. CMUCL does that. SBCL was derived and simplified from CMUCL and lacks it until now. I think Lucid Common Lisp (which is no longer actively sold) did support something like that, too.

    Might be useful to add this to SBCL, too.