Global Compiler Policy

Tags: lisp, Date: 2009-06-30

A quick note to library implementors: the effects of DECLAIM are permitted to persist after the containing file is compiled, and it is unkind to mutate your user's settings. Personally, I find DECLAIM too blunt and prefer to add declarations within functions, even going as far as introducing LOCALLY subforms just to have a place on which to hang declarations. But if you are really set on using DECLAIM, please wrap it like this:

(eval-when (:compile-toplevel)
  (declaim (optimize speed)))

to ensure that the body is evaluated in the dynamic execution context of the compiler, which makes a practical difference on Allegro. It goes without saying that you don't want (PROCLAIM '(OPTIMIZE ...)) in a library, either.

UPDATE: The above works but is not mandated by the spec because the dynamic execution context of the compiler does not include the global declarations. Alas, by the spec the portable solution is to wrap the whole file in:

(locally (declare (optimize speed)) ...)
end-of-post