All Manuals > LispWorks User Guide and Reference Manual > 9 The Compiler > 9.5 Compiler control

NextPrevUpTopContentsIndex

9.5.1 Examples of compiler control

The following function, compiled with safety = 2, does not check the type of its argument because it merely reads:

(defun foo (x)
  (declare (optimize (safety 2)))
  (car x))

However the following function, also compiled with safety = 2, does check the type of its argument because it writes:

(defun set-foo (x y)
  (declare (optimize (safety 2)))
  (setf (car x) y))

As another example, interpreted code and code compiled at at low safety does not check type declarations. To make LispWorks check declarations, you need to compile your code after doing:

(declaim (optimize (safety 3) (debug 3)))

This last example shows how to copy efficiently bytes from a typed-aref vector (see make-typed-aref-vector) to an (unsigned-byte 8) array. type and safety declarations cause the compiler to inline the code that deals specifically with (unsigned-byte 8) . This code was developed after an application was found to have a bottleneck in the original version of this function:

(defun copy-typed-aref-vector-to-byte-vector
       (byte-vector typed-vector length)
  (declare (optimize (safety 0))
           (type (simple-array (unsigned-byte 8) 1) byte-vector)
           (fixnum length))
  (dotimes (index length)
    (declare (type fixnum index))
    (setf (aref byte-vector index)
          (sys:typed-aref '(unsigned-byte 8) 
                          typed-vector index))))

LispWorks User Guide and Reference Manual - 21 Dec 2011

NextPrevUpTopContentsIndex