From: Wayne Christopher
Subject: declarations
Date: 
Message-ID: <23521@pasteur.Berkeley.EDU>
I'm trying to write Common Lisp code that compiles well, so I've
been putting (declare (type ...)) in all my functions, and it's a pain.
Are there any guidelines on when this will speed up the compiled code,
and when it won't help?  I'd guess that declaring numbers and lists
would be useful, but declaring other things like specific CLOS types
isn't necessary.

	Wayne

From: lawrence.g.mayka
Subject: Re: declarations
Date: 
Message-ID: <14748@cbnewsc.ATT.COM>
In article <·····@pasteur.Berkeley.EDU> ·······@yew.Berkeley.EDU (Wayne Christopher) writes:
>I'm trying to write Common Lisp code that compiles well, so I've
>been putting (declare (type ...)) in all my functions, and it's a pain.
>Are there any guidelines on when this will speed up the compiled code,
>and when it won't help?  I'd guess that declaring numbers and lists
>would be useful, but declaring other things like specific CLOS types
>isn't necessary.

The following general guidelines are intended to prevent the
reusability-stifling, productivity-obstructing overuse of type
declarations:

1) Don't consider type declarations until performance becomes a
major obstacle to acceptance or usage.

2) Don't add type declarations until you've attempted more global
approaches to the performance problem (e.g., better algorithms).

3) Don't add type declarations to legs of code that are not
critical to overall performance.

4) Don't add type declarations even to critical code legs until
you've identified the specific bad performers among them.

5) Don't add type declarations even to those critical, badly
performing code legs until you've considered other approaches for
improving their performance.

6) Coordinate any use of type declarations with the declared
compilation safety level, since code generation for a given
operation and object type may depend on either or both kinds of
declaration.

7) Try less restrictive declarations before resorting to more
confining ones.  For example, try VECTOR before resorting to
(SIMPLE-ARRAY DOUBLE-FLOAT (10)); try (FUNCTION (T T) T) before
(FUNCTION (FIXNUM FIXNUM) CONS).

8) Focus on type operations that are potentially executable
in-line, rather than operations that will typically require a
function call anyway.  For example, give priority to uses of
FIXNUM and + rather than HASH-TABLE and GETHASH.

Even aside from the issues of productivity, reusability, etc.,
keep in mind that:

a) Lisp machines typically ignore type declarations.

b) As Common Lisp implementations on conventional processors
continue to improve, code generated without type declarations will
approach the efficiency of code generated with them.  Processor
architectures are also improving their support for dynamic typing
as they mature.


	Lawrence G. Mayka
	AT&T Bell Laboratories
	···@ihlpf.att.com

Standard disclaimer.
From: Larry Masinter
Subject: Re: declarations
Date: 
Message-ID: <MASINTER.90Apr3225242@recycle.parc.xerox.com>
Unfortunately, treatment of declarations varies widely across
implementations -- this is the case primarily because the utility of
declarations to the compiler optimizer differs quite a bit depending
on the context of use, the specific machine architecture, and a lot of
other choices that differ among implementations.  For the most part,
declarations only matter for character, small number ranges, and
vector types; while optimizers might be able to take some small
advantage of other declarations, it is hard to find examples where it
has a measurable effect on performance.

* The best way to use declarations safely is within macros where either 

(a) the types are really known, and the compiler just can't do well
enough at type inference (few Common Lisp compilers do cross-procedural
type inference, for example)

(b) declarations can be selectively turned off or tuned to particular
implementations (while the declarations have portable semantics, they
don't have portable effect on your system's runtime)

(c) you can enforce run-time type-checking by modifying the macro.

* Try to avoid scattering non-portable declarations in your code. For
example, FIXNUM ranges can differ. If you know a number is between 0
and 2^18, it still may not be a FIXNUM on some systems.

Unfortunately, (type (unsigned-byte 18) x) may not be recognized by
some compilers which recognize (type fixnum x). 

* Declarations are a necessary evil in Lisp, not "good coding
practice" as they are in other languages. If you want type-comments or
type-checking in your code, use CHECK-TYPE or a macro that expands
into it.








--
Larry Masinter (········@parc.xerox.com)
Xerox Palo Alto Research Center (PARC)
3333 Coyote Hill Road; Palo Alto, CA USA 94304