Previous: Capabilities and Internals, Up: Grids

4.7 Efficiency

Access to elements of foreign arrays, for getting or setting, can be very slow if they are not declared. These declarations can take several forms: declare, declaim, proclaim, or the. With the exception of the the form, these declarations will only be effective in SBCL and CCL. Users of other implementations that support the environment function variable-information should contact the Antik maintainer to have support added. If declarations are present and the compiler honors the compiler macros that foreign-array defines, the aref and related forms are macro-expanded directly into foreign array calls, considerably increasing the speed of execution.

When using matrices, declarations with explicit dimensions are also helpful for speed, e.g.

     (declare (type (grid:matrix-double-float 100 100) my-matrix new-matrix))

For setting array elements, it is better to use the macro grid:gsetf than setf when declarations are present. It is hoped that eventually this macro can be eliminated.

For example, the declarations in the following function, and the use of grid:gsetf instead of setf, help decrease the execution time when running this function:

     (defun foreign-array-test (dim)
       (let ((input (grid:make-foreign-array
     		:dimensions dim
     		:initial-element 1.0d0))
     	(output (grid:make-foreign-array 'double-float :dimensions dim)))
         (declare (type grid:vector-double-float input output))   ; declaration of foreign arrays
         (let ((tv0 0.0d0) (tv1 0.0d0))
           (declare (type double-float tv0 tv1))
           (iter (for i from 0 below dim)
     	    (setf tv0 0.0d0)
     	    (iter (for m from 0 to i)
     		  (iter (for n from i below dim)
     			(setf tv1 0.0d0)
     			(iter (for k from m to n)
     			      (incf tv1 (grid:aref input k)))
     			(incf tv0 (expt tv1 -2))))
     	    (grid:gsetf (grid:aref* output i) (- (grid:aref input i) tv0))))))