Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the effect of ftype declarations on built-in functions in SBCL?

I'm building on some old Common Lisp code written by others, which includes lines such as the following at the start of a few 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 floats. The compiler may use this information to create more efficient code.

Some Lisps 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 placing

(unlock-package 'common-lisp)

in the .sbclrc initialization file. That's what I've been doing for the last year or so. I assume that this is needed because +, -, etc. are in that package, and the code alters these functions' declarations.

My question is: Can declaring the function type of built-in functions such as + and min have a beneficial effect on compiled code in SBCL? (If it can, then why does SBCL complain about these declarations by default?) Would I be better off removing such ftype declarations, and then getting rid of the unlock-package line in .sbclrc?

Thanks.

like image 411
Mars Avatar asked Jan 14 '23 15:01

Mars


1 Answers

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 floats. The compiler may use this information to create more efficient code.

Also, they will only return floats. With certain optimization settings, the a Common Lisp compiler does not generate runtime checks and may only generate code for float computations. Also SBCL may show compile-time warnings in certain cases, where it detects that code violates type declarations.

It's also a source for errors, since from now on (within the scope of the declaration) basic functions like +and - are declared not to work on other number types (integer, complex, ...).

So, what is the purpose for these declarations? Since it is portable code (and most implementations don't implement compile-time type checking), it can only be for optimization purposes. Some of that might not be necessary in SBCL, since it uses type inference.

Why does SBCL not allow to alter the built-in functionality by default? It is so to prevent shooting in your foot: you are altering the base language. Now basic numeric operations may lead to errors.

Ways to deal with that:

  • use only local declarations, don't alter the language globally. You indicate that these are only locally declared - that's good.

  • declare the values of variables instead

  • write special functions for the float case and declare them inline.

  • only unlock the package CL during compilation of these few functions. keep it locked later.

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

You can check that by looking at the disassembled code and also by profiling. Make sure that you compile the function with the right optimization setting. In Common Lisp the function DISASSEMBLE should show you machine code in a readable way. The SBCL compiler should also tell you if it can't optimize the compiled code.

like image 189
Rainer Joswig Avatar answered Apr 06 '23 17:04

Rainer Joswig