In the following program, removing the line
(declare (type (simple-array bit) arr))
makes running time increase by more than a factor of 3, using SBCL. The type information given in the defclass
macro via :type
on the other hand appears to have no impact on performance.
(defclass class-1 () ((arr :type (simple-array bit))))
(defun sample (inst)
(declare (type class-1 inst))
(let ((arr (slot-value inst 'arr)))
(declare (type (simple-array bit) arr)) ;; 3x running time without
(map-into arr #'(lambda (dummy) (if (< (random 1.0) 0.5) 0 1)) arr)))
(let ((inst (make-instance 'class-1)))
(setf (slot-value inst 'arr) (make-array 10000 :element-type 'bit))
(loop for i from 1 to 10000 do (sample inst)))
How can I have the same performance benefit without having to declare the arr
slot a simple-array bit
each time I use it? The latter is particularly annoying since (as far as I have found out) is requires to introduce a binding via let
or similar each time; I cannot just write (slot-value inst 'arr)
in the place where I need it.
(declaim (ftype (function ((c)) (simple-array bit)) c-arr))
. With this in place, even the:type
can be omitted, as well as(declare (type class-1 inst))
. – Podium