Jan Vroonhof <vroonhof(a)math.ethz.ch> writes:
Ben Wing <ben(a)666.com> writes:
> Could you possibly rewrite this a bit so as to use defstruct? cl is
> now a standard part of XEmacs so there's absolutely no point in
> continuing to use vectors for structs.
I thought about that. But if I interpret the defstruct macro
correctly that generates functions that cannot be inlined and do
redundant type checks.
They can and they don't.
Accessors like `person-name' normally check their arguments
(effectively using `person-p') and signal an error if the
argument is the wrong type. This check is affected by `(optimize
(safety ...))' declarations. Safety level 1, the default, uses a
somewhat optimized check that will detect all incorrect
arguments, but may use an uninformative error message (e.g.,
"expected a vector" instead of "expected a `person'").
Safety
level 0 omits all checks except as provided by the underlying
`aref' call; safety levels 2 and 3 do rigorous checking that will
always print a descriptive error message for incorrect inputs.
Test it like this. Create a file, say x.el, with the following
contents:
(proclaim '(optimize (speed 3) (safety 0)))
(defstruct (foo)
(bar nil)
(count 0))
(defun test (x)
(foo-count x))
Byte-compile it and load-file x.elc. Then try:
(disassemble 'test)
byte code for test:
args: (x)
0 varref x
1 constant 2
2 aref
3 return
So foo-count is inlined and there are no safety checks.
Note that #'foo-count is still a regular function, not a macro,
meaning you can feed it to #'mapcar etc. But when you *compile* the
stuff, the CL additions to the compiler do the right thing.
cl.el is smarter than you think.
I would love to be proven wrong though.
Good enough? :-)