type declaration and optimization

Discussion of Common Lisp
Post Reply
lrodrig
Posts: 17
Joined: Thu Sep 16, 2010 4:52 pm

type declaration and optimization

Post by lrodrig » Thu Mar 10, 2011 9:17 am

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.
Last edited by lrodrig on Thu Mar 10, 2011 10:20 am, edited 1 time in total.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: type declaration and optimization

Post by ramarren » Thu Mar 10, 2011 10:01 am

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.

lrodrig
Posts: 17
Joined: Thu Sep 16, 2010 4:52 pm

Re: type declaration and optimization

Post by lrodrig » Thu Mar 10, 2011 12:35 pm

Hi,

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

Regards,
Luis.

Post Reply