This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

type declaration and optimization

3 posts · 1031 views

Hi,

I'm trying to optimize some Common Lisp code. I have a function called "get-prob", which should return a float number. I use the following declaration:
(declaim (single-float get-prob))
The thing is that I have another function that calls "get-prob".
(defun get-final-prob (x)
 (let ((prob (get-prob model str)))
   (declare (single-float prob) (optimize (speed 3) (safety 0)))
    	 (incf prob (get-prob2 ...)))
When compiling the function (SBCL+slime), I get the following message on the "incf" line

Unable to optimize due to type uncertainty. The second argument is a NUMBER not a RATIONAL.
Unable to optimize due to type uncertainty. The second argument is a NUMBER not a SINGLE-FLOAT.
Forced to do GENERIC+ (cost 10)
unable to do inline float arithmetic (cost 2) because:
The second argument is a NUMBER not a SINGLE-FLOAT

I have a similar error message when using "double-float".

Does someone know how what I'm doing wrong?

Thanks in advance.
Regards,
Luis.

Last edited by lrodrig on , edited 1 time in total.

Re: type declaration and optimization

lrodrig wrote: I use the following declaration:
This declaration declares the global variable get-prob to be of type single float, which is not what you want. You can declare function types using FTYPE declaration, but that is fairly complex and most implementations do not use it, since it can lead to indirect crashes, when the function type changes but call sites are not recompiled.

SBCL might in some circumstances. But generally, if your function is small enough that call cost would significantly affect performance, then it should be inlined, which will allow the type inference mechanism to optimize.

Re: type declaration and optimization

Hi,

Thank you very much for your answer. I finally used FTYPE and now my code is significantly faster.

Regards,
Luis.