SBCL优化:函数类型声明
SBCL optimization: function type declaration
如果我有一个接受函数参数的函数,出于优化目的,我可以将其声明为 function
,比方说
(defun foo (f)
(declare (type function f))
...)
不过,我可以更具体一点:
(defun foo (f)
(declare (type (function (double-float) double-float) f))
...)
即告诉 f
将接受一个 double-float
参数和 return 一个 double-float
值。然而,SBCL 似乎能够对前者进行更好的优化,而对于后者,它表示它不知道 f
是否为 fdefinition
(尝试使用 (optimize (speed 3))
进行编译声明复制)。
所以,我的问题是:
我是不是做错了什么?特别是如果 SBCL 只对 function
和 (function ...)
做完全相同的事情,我会接受它,但实际上情况更糟。还是应该将其视为 SBCL 中的错误?
出于某种原因,在优化方面,函数类型声明在 CL 中通常是无用的吗?
系统信息:SBCL 1.3.18
来自 SBCL 手册(4.2.3 将现有程序获取到 运行):
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.
这是可能的,这就是为什么你的函数在包含变量类型声明的情况下表现更差的原因。
进一步:
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 ...)
...)
这是来自当前版本 SBCL (1.4) 的手册,因此它可能适用于您的情况,也可能不适用于您的情况。
如果我有一个接受函数参数的函数,出于优化目的,我可以将其声明为 function
,比方说
(defun foo (f)
(declare (type function f))
...)
不过,我可以更具体一点:
(defun foo (f)
(declare (type (function (double-float) double-float) f))
...)
即告诉 f
将接受一个 double-float
参数和 return 一个 double-float
值。然而,SBCL 似乎能够对前者进行更好的优化,而对于后者,它表示它不知道 f
是否为 fdefinition
(尝试使用 (optimize (speed 3))
进行编译声明复制)。
所以,我的问题是:
我是不是做错了什么?特别是如果 SBCL 只对
function
和(function ...)
做完全相同的事情,我会接受它,但实际上情况更糟。还是应该将其视为 SBCL 中的错误?出于某种原因,在优化方面,函数类型声明在 CL 中通常是无用的吗?
系统信息:SBCL 1.3.18
来自 SBCL 手册(4.2.3 将现有程序获取到 运行):
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.
这是可能的,这就是为什么你的函数在包含变量类型声明的情况下表现更差的原因。
进一步:
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 ...) ...)
这是来自当前版本 SBCL (1.4) 的手册,因此它可能适用于您的情况,也可能不适用于您的情况。