What is the effect of ftype declaration for inline functions in SBCL?

I am using old Common Lisp code written by others, which includes the following lines at the beginning of several functions:

(declare (ftype (function (&rest float) float) + - * min max)) 

My understanding is that the purpose of this is to tell the compiler that the five functions listed at the end of the form will only be passed to floats. The compiler can use this information to create more efficient code.

Some Lisp do not complain about this declaration (ABCL, CCL, ECL, LispWorks, CLISP), but SBCL will not accept this declaration in the default configuration. SBCL can be made to accept it by posting

 (unlock-package 'common-lisp) 

in the .sbclrc initialization file. This is what I did last year or so. I assume this is necessary because this package has +, -, etc., and the code modifies the declarations of these functions.

My question is: can a function type declaration of built-in functions, such as + and min, have a beneficial effect on compiled code in SBCL? (If possible, why is SBCL complaining about these ads by default?) Would it be better to delete such ads like ftype and then get rid of the unlock-package in .sbclrc?

Thanks.

+4
source share
1 answer

My understanding is that the purpose of this is to tell the compiler that the five functions listed at the end of the form will only be passed to floats. The compiler can use this information to create more efficient code.

In addition, they will only return floats. With certain optimization settings, the Common Lisp compiler does not generate runtime checks and can only generate code for floating-point calculations. In addition, SBCL may display warnings at compile time in certain cases when it detects that code violates type declarations.

It is also a source of errors, since now (within the framework of the declaration) basic functions, such as + and - , are declared not working on other types (integer, complex, ...).

So what is the purpose of these declarations? Since this is portable code (and most implementations do not implement compilation type checking), this can only be for optimization purposes. Some of them may not be needed in SBCL because it uses type inference.

Why does SBCL not allow changing the default built-in functionality? This is to prevent shooting in the leg: you are changing the base language. Now basic numerical operations can lead to errors.

Ways to deal with this:

  • use only local ads, do not change the language all over the world. You indicate that they are only locally declared - that’s good.

  • declare variable values ​​instead

  • write special functions for the flag and declare them inline.

  • only unlock the CL package while compiling these few functions. save it later.

My question is: can a function type declaration of built-in functions, such as + and min, have a beneficial effect on compiled code in SBCL?

You can verify this by looking at the parsed code as well as the profiling. Make sure you compile the function with the correct optimization setting. In Common Lisp, the `DISASSEMBLE 'function should display machine code in a readable manner. The SBCL compiler should also tell you if it can optimize the compiled code.

+8
source

All Articles