[snip]
Chisheng> * (compile 'test1)
Chisheng> ; In: LAMBDA (XMIN)
Chisheng> ; (LOOP FOR I OF-TYPE FIXNUM ...)
Chisheng> ; --> BLOCK LET LET ANSI-LOOP::LOOP-BODY TAGBODY
ANSI-LOOP::LOOP-REALLY-DESETQ
Chisheng> ; ==>
Chisheng> ; (SETQ X (RANDOM 100.0))
Chisheng> ; Note: Doing float to pointer coercion (cost 13) to X.
This not is not from get-x-index. It's from the call to random in
main body of test1. I guess random returns a boxed float. I'm not
Thanks a lot for pointing this out. The original TEST1 did not mimic
the original situation as well as I thought. Here is another try without
using RANDOM:
(defun test11 (xmin)
(declare (single-float xmin)
(optimize (safety 0) (speed 3)))
(let ((x-ht (make-hash-table))
(input (make-array 5
:element-type 'single-float
:initial-contents '(1.0 2.0 3.0 4.0 5.0))))
(declare (hash-table x-ht)
(type (simple-array single-float 1) input))
(flet ((get-x-index (x)
(declare (single-float x))
(let ((index (gethash x x-ht nil)))
(unless index
(setf index
(setf (gethash x x-ht)
(the fixnum (truncate (- x xmin))))))
index)))
(loop for x of-type single-float across input
do (print (get-x-index x))))))
Compiling TEST11 generated the following note:
; In: LAMBDA (XMIN)
; (LOOP FOR X OF-TYPE SINGLE-FLOAT ...)
; --> BLOCK LET ANSI-LOOP::LOOP-BODY TAGBODY ANSI-LOOP::LOOP-REALLY-DESETQ
; ==>
; (SETQ X (AREF #:G4 #:G5))
; Note: Doing float to pointer coercion (cost 13) to X.
;
; Compiling Top-Level Form:
; Compilation unit finished.
; 1 note
Looks like the compiler doesn't trust my declaratioin?
Yet another try is TEST111, using MAP, instead of LOOP, at the end:
(defun test111 (xmin)
(declare (single-float xmin)
(optimize (safety 0) (speed 3)))
(let ((x-ht (make-hash-table))
(input (make-array 5
:element-type 'single-float
:initial-contents '(1.0 2.0 3.0 4.0 5.0))))
(declare (hash-table x-ht)
(type (Simple-Array Single-Float 1) input))
(flet ((get-x-index (x)
(declare (single-float x))
(let ((index (gethash x x-ht nil)))
(unless index
(setf index
(setf (gethash x x-ht)
(the fixnum (truncate (- x xmin))))))
index)))
(map nil
#'(lambda (x)
(declare (single-float x))
(print (get-x-index x)))
input))))
Compiling TEST111 does not generate any efficiency note! I have
absolutely no idea why.
Best wishes,
-cph