Page 1 of 1

type declaration and optimization

Posted: Thu Mar 10, 2011 9:17 am
by lrodrig
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:

Code: Select all

(declaim (single-float get-prob))
The thing is that I have another function that calls "get-prob".

Code: Select all

(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.

Re: type declaration and optimization

Posted: Thu Mar 10, 2011 10:01 am
by ramarren
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

Posted: Thu Mar 10, 2011 12:35 pm
by lrodrig
Hi,

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

Regards,
Luis.