Reputation: 2386
If I have a function that accepts function argument, for optimization purposes I can declare it to be a function
, let's say
(defun foo (f)
(declare (type function f))
...)
However, I can be even more specific:
(defun foo (f)
(declare (type (function (double-float) double-float) f))
...)
i.e. telling that f
will accept one double-float
argument and return one double-float
value. SBCL, however, seem to be able to perform a better optimization on the former and for the latter it says that it doesn't know if f
is fdefinition
(try to compile with (optimize (speed 3))
declaration to reproduce).
So, my questions are:
Am I doing something wrong? Especially If SBCL would do exactly the same thing for just function
and (function ...)
I would be OK with it, but it actually does worse. Or should it be considered a bug in SBCL?
Is function type declaration in general useless in CL in terms of optimization for some reason?
SysInfo: SBCL 1.3.18
Upvotes: 3
Views: 1288
Reputation: 2282
From the SBCL Manual (4.2.3 Getting Existing Programs to Run):
Some incorrect declarations can only be detected by run-time type checking [...] because the SBCL compiler does much more type inference than other Common Lisp compilers, so an incorrect declaration can do more damage.
It's possible, that's why your function does worse with the variable type declarations included.
Further:
The most common problem is with variables whose constant initial value doesn't match the type declaration. Incorrect constant initial values will always be flagged by a compile-time type error, and they are simple to fix once located. Consider this code fragment:
(prog (foo) (declare (fixnum foo)) (setq foo ...) ...)
Here foo is given an initial value of nil, but is declared to be a fixnum. Even if it is never read, the initial value of a variable must match the declared type. There are two ways to fix this problem. Change the declaration
(prog (foo) (declare (type (or fixnum null) foo)) (setq foo ...) ...)
or change the initial value
(prog ((foo 0)) (declare (fixnum foo)) (setq foo ...) ...)
This is from the manual for the current version of SBCL (1.4), so it may or may not apply to your situation.
Upvotes: 1