如何避免SBCL中双精度浮点数装箱/拆箱相关编译器警告?
如何在SBCL中抑制合法的double-float返回装箱警告
当使用SBCL Common Lisp以(speed 3)优化级别编译以下代码时,编译器会针对返回double-float的triple函数发出如下效率提示:
"doing float to pointer coercion (cost 13) to "
"
示例代码:
(declaim (ftype (function (double-float) double-float) triple)) (defun triple (x) (declare (type double-float x)) (the double-float (* x 3.0d0))) (defun main () (format t "~a x 3 = ~a" 5.0d0 (triple 5.0d0)))
我理解该警告是浮点数装箱/拆箱的开销提示,但项目中存在大量操作并返回double-float的函数,此类提示会充斥编译报告,导致无法有效查看其他重要警告。现询问是否有标准做法,可通过编码调整或编译器配置,避免该类合法提示干扰编译报告。
已验证的解决方案
注:以下方案并非质疑SBCL的浮点数装箱效率提示,仅用于过滤无关干扰,聚焦重要警告。
- 将返回
double-float的被调用函数(如triple)通过flet或labels内联到调用函数(如main)中,可消除警告 - 使用
(declaim (inline triple))无法抑制该提示 - 用局部声明包裹函数定义,屏蔽编译器提示,但可能会同时阻止其他有用的编译提示输出:
(locally (declare (sb-ext:muffle-conditions sb-ext:compiler-note)) (defun triple ... )) - 不直接返回
double-float,而是返回包含该值的单元素数组,无警告但使用繁琐且会导致性能下降(即使复用数组) - 在Emacs中使用以下Elisp函数过滤编译报告中的相关提示:
(defun slime-compilation-delete-float-coercion-notes () "Remove sections containing 'float to pointer coercion' from *slime-compilation* buffer." (interactive) (with-current-buffer "*slime-compilation*" (let ((inhibit-read-only t) (filter-string "float to pointer coercion") (nb-deletions 0)) (save-excursion (goto-char (point-min)) (while (re-search-forward (regexp-quote filter-string) nil t) (setq nb-deletions (1+ nb-deletions)) ;; Find the start of this warning/note section (let ((end (line-end-position))) (beginning-of-line) ;; Search backward for the section start (typically a blank line or buffer start) (while (and (not (bobp)) (not (looking-at "^$")) (looking-at "^[; ]")) (forward-line -1)) (when (looking-at "^$") (forward-line 1)) (let ((start (point))) ;; Search forward for section end (blank line or end of buffer) (goto-char end) (while (and (not (eobp)) (not (looking-at "^$"))) (forward-line 1)) (delete-region start (point))))) (cond ((= 0 nb-deletions) (message "No deletion performed.")) ((= 1 nb-deletions) (message "1 deletion performed.")) (t (message "%s deletions performed" nb-deletions)))))))
内容的提问来源于stack exchange,提问作者Nicolas56
相关产品推荐
相关产品推荐

