| www.delorie.com/gnu/docs/emacs/cl_59.html | search |
![]() Buy the book! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Many of the advanced features of this package, such as defun*,
loop, and setf, are implemented as Lisp macros. In
byte-compiled code, these complex notations will be expanded into
equivalent Lisp code which is simple and efficient. For example,
the forms
(incf i n) (push x (car p)) |
are expanded at compile-time to the Lisp forms
(setq i (+ i n)) (setcar p (cons x (car p))) |
which are the most efficient ways of doing these respective operations
in Lisp. Thus, there is no performance penalty for using the more
readable incf and push forms in your compiled code.
Interpreted code, on the other hand, must expand these macros
every time they are executed. For this reason it is strongly
recommended that code making heavy use of macros be compiled.
(The features labeled "Special Form" instead of "Function" in
this manual are macros.) A loop using incf a hundred times
will execute considerably faster if compiled, and will also
garbage-collect less because the macro expansion will not have
to be generated, used, and thrown away a hundred times.
You can find out how a macro expands by using the
cl-prettyexpand function.
*scratch* buffer and type, say,
(cl-prettyexpand '(loop for x below 10 collect x)) |
and type C-x C-e immediately after the closing parenthesis; the expansion
(block nil
(let* ((x 0)
(G1004 nil))
(while (< x 10)
(setq G1004 (cons x G1004))
(setq x (+ x 1)))
(nreverse G1004)))
|
will be inserted into the buffer. (The block macro is
expanded differently in the interpreter and compiler, so
cl-prettyexpand just leaves it alone. The temporary
variable G1004 was created by gensym.)
If the optional argument full is true, then all
macros are expanded, including block, eval-when,
and compiler macros. Expansion is done as if form were
a top-level form in a file being compiled. For example,
(cl-prettyexpand '(pushnew 'x list))
-| (setq list (adjoin 'x list))
(cl-prettyexpand '(pushnew 'x list) t)
-| (setq list (if (memq 'x list) list (cons 'x list)))
(cl-prettyexpand '(caddr (member* 'a list)) t)
-| (car (cdr (cdr (memq 'a list))))
|
Note that adjoin, caddr, and member* all
have built-in compiler macros to optimize them in common cases.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |