From: Szymon
Subject: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <87hdod3n2s.fsf@eva.rplacd.net>
Hi.

Example code:

(defun is-in-simple-string-p (ch str)
  (declare
   (optimize speed) (inline is-in-simple-string-p) (type character ch) (type simple-string str))
  (prog ((index -1) (str-len (length str)))
	(declare
	 (type fixnum index str-len))
	|TAG| (if (not (eql (schar str (if (eql (incf index) str-len) (return NIL) index)) ch))
		  (go |TAG|)
		(return T))))

Compiler(SBCL)=>luser output:

; in: LAMBDA NIL
;     (INCF INDEX)
; --> LET* SETQ 
; ==>
;   (THE #<SB-KERNEL:NUMERIC-TYPE FIXNUM> #:G0)
; 
; note: doing signed word to integer coercion (cost 20), for:
;       the first argument of CHECK-FIXNUM
; compilation unit finished

How to get rid of this? I declared index FIXNUM...

TIA, Szymon.

From: Thomas A. Russ
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <ymid5z18ncv.fsf@sevak.isi.edu>
Szymon <············@o2.pl> writes:

> (defun is-in-simple-string-p (ch str)
>   (declare
>    (optimize speed) (inline is-in-simple-string-p) (type character ch) (type simple-string str))
>   (prog ((index -1) (str-len (length str)))
> 	(declare
> 	 (type fixnum index str-len))
> 	|TAG| (if (not (eql (schar str (if (eql (incf index) str-len) (return NIL) index)) ch))
> 		  (go |TAG|)
> 		(return T))))
> 
> Compiler(SBCL)=>luser output:
> 
> ; in: LAMBDA NIL
> ;     (INCF INDEX)
> ; --> LET* SETQ 
> ; ==>
> ;   (THE #<SB-KERNEL:NUMERIC-TYPE FIXNUM> #:G0)
> ; 
> ; note: doing signed word to integer coercion (cost 20), for:
> ;       the first argument of CHECK-FIXNUM
> ; compilation unit finished
> 
> How to get rid of this? I declared index FIXNUM...

You have an optimization request for SPEED, but you don't reduce SAFETY,
so there is a type check inserted.  You probably want to change your
optimize declaration from
   (optimize speed)  to
   (optimize (speed 3) (safety 0) (debug 0))








-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Szymon
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <87is8tdy7d.fsf@eva.rplacd.net>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Szymon <············@o2.pl> writes:
> 
> > (defun is-in-simple-string-p (ch str)
> >   (declare
> >    (optimize speed)

> > (inline is-in-simple-string-p)

stupid me...

> >(type character ch) (type simple-string str))
> >   (prog ((index -1) (str-len (length str)))
> > 	(declare
> > 	 (type fixnum index str-len))
> > 	|TAG| (if (not (eql (schar str (if (eql (incf index) str-len) (return NIL) index)) ch))
> > 		  (go |TAG|)
> > 		(return T))))
> > 
> > Compiler(SBCL)=>luser output:

> > [.....]

> > 
> > How to get rid of this? I declared index FIXNUM...
> 
> You have an optimization request for SPEED, but you don't reduce SAFETY,
> so there is a type check inserted.  You probably want to change your
> optimize declaration from
>    (optimize speed)  to
>    (optimize (speed 3) (safety 0) (debug 0))

It works :)

Thank you, Szymon.
From: szymon
Subject: [btw] Simple optimizatiaon problem [.....]
Date: 
Message-ID: <clt69t$47u$1@atlantis.news.tpi.pl>
Szymon wrote:
> 
> Hi.
> 
> Example code:
> 
> (defun is-in-simple-string-p (ch str)
>   (declare
>    (optimize speed) (inline is-in-simple-string-p) (type character ch) (type simple-string str))
>   (prog ((index -1) (str-len (length str)))
> 	(declare
> 	 (type fixnum index str-len))
> 	|TAG| (if (not (eql (schar str (if (eql (incf index) str-len) (return NIL) index)) ch))
> 		  (go |TAG|)
> 		(return T))))

This is example. I know about LOOP (btw, LOOP's MACROEXPAND output is 
[imho] messy).

(defun is-in-simple-string-p (ch str)
   (declare
    (optimize speed)
    (inline is-in-simple-string-p)
    (type base-char ch)
    (type simple-string str))
(loop for current-char of-type base-char across (the simple-string str)
       if (eql current-char ch) return T))

Regards, Szymon.
From: Kalle Olavi Niemitalo
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <87brelffd7.fsf@Astalo.kon.iki.fi>
Szymon <············@o2.pl> writes:

> (defun is-in-simple-string-p (ch str)
>   (declare
>    (optimize speed) (inline is-in-simple-string-p) (type character ch) (type simple-string str))

I don't think this is your problem, but that INLINE declaration
applies only to calls from within IS-IN-SIMPLE-STRING-P itself.
From: Szymon
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <87mzy5dyou.fsf@eva.rplacd.net>
Kalle Olavi Niemitalo <···@iki.fi> writes:

> Szymon <············@o2.pl> writes:
> 
> > (defun is-in-simple-string-p (ch str)
> >   (declare
> >    (optimize speed) (inline is-in-simple-string-p) (type character ch) (type simple-string str))
> 
> I don't think this is your problem, but that INLINE declaration
> applies only to calls from within IS-IN-SIMPLE-STRING-P itself.

Thank you. I misunderstand ...

Regards, Szymon.
From: Alan Crowe
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <86oeik9uww.fsf@cawtech.freeserve.co.uk>
Szymon was notified:
; note: doing signed word to integer coercion (cost 20), for:
;       the first argument of CHECK-FIXNUM
; compilation unit finished

When I encountered that note, I changed my declaration from
FIXNUM to (INTEGER 0 30000). 
Perhaps (integer 0 #.most-positive-fixnum) is useful.

This doesn't work for you because your range includes -1.
Re-arranging the code slightly, so that index is never
negative:

  (defun check-string-for-char(ch str)
    (declare (optimize speed)
	     (inline is-in-simple-string-p)
	     (type character ch)
	     (type simple-string str))
    (prog ((index 0)
	   (str-len (length str)))
      (declare (type fixnum index str-len))
      |TAG|
      (if (eql index str-len) (return nil))
      (when (not (eql (schar str index) ch))
	(incf index)
	(go |TAG|))
      (return T)))

Now CMUCL issues no note :-) so I didn't actually have to
make the declaration more specific.

Alan Crowe
Edinburgh
Scotland
From: Szymon
Subject: Re: Simple optimizatiaon problem -- signed word to integer coercion.
Date: 
Message-ID: <87sm7vx2zh.fsf@eva.rplacd.net>
Alan Crowe <····@cawtech.freeserve.co.uk> writes:

> Szymon was notified:
> ; note: doing signed word to integer coercion (cost 20), for:
> ;       the first argument of CHECK-FIXNUM
> ; compilation unit finished
> 
> When I encountered that note, I changed my declaration from
> FIXNUM to (INTEGER 0 30000). 
> Perhaps (integer 0 #.most-positive-fixnum) is useful.
> 
> This doesn't work for you because your range includes -1.
> Re-arranging the code slightly, so that index is never
> negative:
> 
>   (defun check-string-for-char(ch str)
>     (declare (optimize speed)

> (inline is-in-simple-string-p)

> 	     (type character ch)
> 	     (type simple-string str))
>     (prog ((index 0)
> 	   (str-len (length str)))
>       (declare (type fixnum index str-len))
>       |TAG|
>       (if (eql index str-len) (return nil))
>       (when (not (eql (schar str index) ch))
> 	(incf index)
> 	(go |TAG|))
>       (return T)))

Your version is more readable...

Btw, as Kalle Olavi Niemitalo pointed out declaration: (inline ...) make no
sense in this context.

> Now CMUCL issues no note :-)

indeed :-)

> so I didn't actually have to make the declaration more specific.

I used (INTEGER 0 #.ARRAY-TOTAL-SIZE-LIMIT).

[btw, I just 'discovered' that in CMUCL
(= array-total-size-limit array-total-size-limit) is true.]

Thanks for your reply. Szymon.