[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

the weird warnings from the sbcl compiler are always correct, even more so when they are confusing

Jim Newton

7/28/2015 12:45:00 PM

sbcl issues some warnings compiling this code.
Does anyone see what's happening here?


(defun median-pick (list)
"Pick the median value in a LIST."
(nth (round (/ (length list) 2)) list))



------ *slime-compilation* ------------

cd ~/
1 compiler notes:

sw/climb/src/algorithm/median.lisp:30:3:
note:
deleting unreachable code
--> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK
--> LET
==>
LIST

5 Answers

Zach Beane

7/28/2015 12:50:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> sbcl issues some warnings compiling this code.
> Does anyone see what's happening here?
>
>
> (defun median-pick (list)
> "Pick the median value in a LIST."
> (nth (round (/ (length list) 2)) list))
>
>
>
> ------ *slime-compilation* ------------
>
> cd ~/
> 1 compiler notes:
>
> sw/climb/src/algorithm/median.lisp:30:3:
> note:
> deleting unreachable code
> --> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK
> --> LET
> ==>
> LIST
>

I don't get that note with the latest sbcl. Is there anything else in
median.lisp? What version of SBCL are you using? Maybe the note is in
error and a new version fixed it.

Zach

Barry Margolin

7/28/2015 2:29:00 PM

0

In article <87h9ooigz2.fsf@zpob.site.sys>, Zach Beane <xach@xach.com>
wrote:

> Jim Newton <jimka.issy@gmail.com> writes:
>
> > sbcl issues some warnings compiling this code.
> > Does anyone see what's happening here?
> >
> >
> > (defun median-pick (list)
> > "Pick the median value in a LIST."
> > (nth (round (/ (length list) 2)) list))
> >
> >
> >
> > ------ *slime-compilation* ------------
> >
> > cd ~/
> > 1 compiler notes:
> >
> > sw/climb/src/algorithm/median.lisp:30:3:
> > note:
> > deleting unreachable code
> > --> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK
> > --> LET
> > ==>
> > LIST
> >
>
> I don't get that note with the latest sbcl. Is there anything else in
> median.lisp? What version of SBCL are you using? Maybe the note is in
> error and a new version fixed it.

The only issue I can see with that code is that

(round (/ <expr> 2))

can be written as

(round <expr> 2)

But I would just expect a good compiler to optimize one into the other,
not warn about it.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

rwiker

7/28/2015 2:54:00 PM

0

Zach Beane <xach@xach.com> writes:

> Jim Newton <jimka.issy@gmail.com> writes:
>
>> sbcl issues some warnings compiling this code.
>> Does anyone see what's happening here?
>>
>>
>> (defun median-pick (list)
>> "Pick the median value in a LIST."
>> (nth (round (/ (length list) 2)) list))
>>
>>
>>
>> ------ *slime-compilation* ------------
>>
>> cd ~/
>> 1 compiler notes:
>>
>> sw/climb/src/algorithm/median.lisp:30:3:
>> note:
>> deleting unreachable code
>> --> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK
>> --> LET
>> ==>
>> LIST
>>
>
> I don't get that note with the latest sbcl. Is there anything else in
> median.lisp? What version of SBCL are you using? Maybe the note is in
> error and a new version fixed it.
>
> Zach

Could be related to optimization settings, perhaps?

Unless this function is actually at line 30 in the source file, it may
even be necessary to look at something else.

Pascal J. Bourguignon

7/28/2015 4:54:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> sbcl issues some warnings compiling this code.
> Does anyone see what's happening here?
>
>
> (defun median-pick (list)
> "Pick the median value in a LIST."
> (nth (round (/ (length list) 2)) list))
>
>
>
> ------ *slime-compilation* ------------
>
> cd ~/
> 1 compiler notes:
>
> sw/climb/src/algorithm/median.lisp:30:3:
> note:
> deleting unreachable code
> --> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK
> --> LET
> ==>
> LIST
>

Those notes also depend on the optimization levels. I get them with
(optimize (safety 0) (debug 0) (speed 3) (space 0)), but not with
(optimize (safety 3) (debug 3)).

My first hunch was that (round (/ â?¦)) was the culprit. Indeed, if you
write (round (length list) 2) instead it doesn't complain.


The problem with sbcl is that it complains on dead code that it
generates itself!

That said, notice that you can win 20% of the processor cycles by using
a classical hare/tortoise algorithm. The difference is in the order of
the memory accesses. With length+nth, you scan the list once, and then
you scan the first half of the list. With hare+toirtoise, you scan the
list and the first half at the same time. The same memory is read once
or twice, but in the later case it's intermixed. At the beginning of
the list, it will be better for the cache, but I don't see how it can
account for 20% of processor cycles spared.

(Oh and don't worry, this makes a small difference only on large lists,
like in the benchmark. For usual list lengths, your form is perfectly ok).

------------------------------------------------------------------------
;; (declaim (optimize (safety 3) (debug 3)))
(declaim (optimize (safety 0) (debug 0) (speed 3) (space 0)))

(defun median-pick (list)
"Pick the median value in a LIST."
(check-type list list)
(nth (round (/ (length list) 2)) list))

(defun median-pick-1 (list)
"Pick the median value in a LIST."
(check-type list list)
(nth (round (length list) 2) list))

(defun median-pick-2 (list)
"Pick the median value in a LIST."
(check-type list list)
(loop
:for hare := list :then (cddr hare)
:for tortoise := list :then (cdr tortoise)
:while hare
:finally (return (car tortoise))))

(defparameter *list* (make-list 10000000 :initial-element 't))
(time (median-pick-1 *list*))
(time (median-pick-2 *list*))
------------------------------------------------------------------------

* (load (compile-file "/tmp/a.lisp"))

; compiling file "/tmp/a.lisp" (written 28 JUL 2015 06:46:34 PM):
; compiling (DECLAIM (OPTIMIZE # ...))
; compiling (DEFUN MEDIAN-PICK ...)
; file: /tmp/a.lisp
; in: DEFUN MEDIAN-PICK
; (NTH (ROUND (/ (LENGTH LIST) 2)) LIST)
; --> CAR NTHCDR BLOCK FLET TYPECASE LET COND IF COND THE PROGN DO BLOCK LET
; ==>
; LIST
;
; note: deleting unreachable code

; compiling (DEFUN MEDIAN-PICK-1 ...)
; compiling (DEFUN MEDIAN-PICK-2 ...)
; compiling (DEFPARAMETER *LIST* ...)
; compiling (TIME (MEDIAN-PICK-1 *LIST*))
; compiling (TIME (MEDIAN-PICK-2 *LIST*));
; compilation unit finished
; printed 1 note


; /tmp/a.fasl written
; compilation finished in 0:00:00.011
Evaluation took:
0.030 seconds of real time
0.028001 seconds of total run time (0.028001 user, 0.000000 system)
93.33% CPU
90,766,148 processor cycles
0 bytes consed

Evaluation took:
0.024 seconds of real time
0.024002 seconds of total run time (0.024002 user, 0.000000 system)
100.00% CPU
72,707,208 processor cycles
0 bytes consed

T
*

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Pascal J. Bourguignon

7/28/2015 5:16:00 PM

0

Barry Margolin <barmar@alum.mit.edu> writes:

> The only issue I can see with that code is that
>
> (round (/ <expr> 2))
>
> can be written as
>
> (round <expr> 2)
>
> But I would just expect a good compiler to optimize one into the other,
> not warn about it.

But sbcl is not a good compiler: when it optimizes, it slips up and
leaves dead code around.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk