X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=doc%2Fmanual%2Fefficiency.texinfo;h=8ef22d2f093aac6ddf7445292cf4a9e77bcfbae0;hb=53f576d7d796e37a9c51c3c3296341458f046c44;hp=de905ae3a4faa9a7168a8adebd0baa8c5a533696;hpb=6822034325136cde4e14773c83c3769b42721306;p=sbcl.git diff --git a/doc/manual/efficiency.texinfo b/doc/manual/efficiency.texinfo index de905ae..8ef22d2 100644 --- a/doc/manual/efficiency.texinfo +++ b/doc/manual/efficiency.texinfo @@ -4,11 +4,68 @@ @cindex Efficiency @menu -* Dynamic-extent allocation:: -* Modular arithmetic:: -* Miscellaneous Efficiency Issues:: +* Slot access:: +* Dynamic-extent allocation:: +* Modular arithmetic:: +* Miscellaneous Efficiency Issues:: @end menu +@node Slot access +@comment node-name, next, previous, up +@section Slot access +@cindex Slot access + +@subsection Structure object slot access + +Structure slot accessors are efficient only if the compiler is able to +open code them: compiling a call to a structure slot accessor before +the structure is defined, declaring one @code{notinline}, or passing +it as a functional argument to another function causes severe +perfomance degradation. + +@subsection Standard object slot access + +The most efficient way to access a slot of a @code{standard-object} is +by using @code{slot-value} with a constant slot name argument inside a +@code{defmethod} body, where the variable holding the instance is a +specializer parameter of the method and is never assigned to. The cost +is roughly 1.6 times that of an open coded structure slot accessor. + +Second most efficient way is to use a CLOS slot accessor, or +@code{slot-value} with a constant slot name argument, but in +circumstances other than specified above. This may be up to 3 times as +slow as the method described above. + +Example: + +@lisp +(defclass foo () ((bar))) + +;; Fast: specializer and never assigned to +(defmethod quux ((foo foo) new) + (let ((old (slot-value foo 'bar))) + (setf (slot-value foo 'bar) new) + old)) + +;; Slow: not a specializer +(defmethod quux ((foo foo) new) + (let* ((temp foo) + (old (slot-value temp 'bar))) + (setf (slot-value temp 'bar) new) + old)) + +;; Slow: assignment to FOO +(defmethod quux ((foo foo) new) + (let ((old (slot-value foo 'bar))) + (setf (slot-value foo 'bar) new) + (setf foo new) + old)) +@end lisp + +Note that when profiling code such as this, the first few calls to the +generic function are not representative, as the dispatch mechanism is +lazily set up during those calls. + @node Dynamic-extent allocation @comment node-name, next, previous, up @section Dynamic-extent allocation