emacs.d/clones/www.sbcl.org/sbcl-internals/Basic-Implementation.html

106 lines
4.8 KiB
HTML
Raw Normal View History

2023-01-18 20:30:47 +01:00
<html lang="en">
<head>
<title>Basic Implementation - SBCL Internals</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="SBCL Internals">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Slot_002dValue.html#Slot_002dValue" title="Slot-Value">
<link rel="next" href="Compiler-Transformations.html#Compiler-Transformations" title="Compiler Transformations">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This manual is part of the SBCL software system. See the `README'
file for more information.
This manual is in the public domain and is provided with
absolutely no warranty. See the `COPYING' and `CREDITS' files for
more information.
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Basic-Implementation"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Compiler-Transformations.html#Compiler-Transformations">Compiler Transformations</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Slot_002dValue.html#Slot_002dValue">Slot-Value</a>
<hr>
</div>
<!-- node-name, next, previous, up -->
<h3 class="section">7.1 Basic Implementation</h3>
<p>All of the following, while described in terms of <code>slot-value</code>,
also applies to <code>(setf slot-value)</code> and to <code>slot-boundp</code>, and
could in principle be extended to <code>slot-makunbound</code>.
<p>The basic implementation of <code>slot-value</code>, following the suggestion
in the standards document, is shown in <a href="ex_003aslot_002dvalue.html#ex_003aslot_002dvalue">ex:slot-value</a>; the
implementation of the other slot operators is similar. The work to be
done simply to arrive at the generic function call is already
substantial: we need to look up the object's class and iterate over the
class' slots to find a slot of the right name, only then are we in a
position to call the generic function which implements the slot access
directly.
<div class="float">
<a name="ex_003aslot_002dvalue"></a>
<pre class="example"> (defun slot-value (object slot-name)
(let* ((class (class-of object))
(slot-definition (find-slot-definition class slot-name)))
(if (null slot-definition)
(values (slot-missing class object slot-name 'slot-value))
(slot-value-using-class class object slot-definition))))
</pre>
<p><strong class="float-caption">Example 7.1</strong></p></div>
<p>The basic implementation of <code>slot-value-using-class</code> specialized on
the standard metaobject classes is shown in
<a href="ex_003aslot_002dvalue_002dusing_002dclass.html#ex_003aslot_002dvalue_002dusing_002dclass">ex:slot-value-using-class</a>. First, we check for an obsolete
instance (that is, one whose class has been redefined since the object
was last accessed; if it has, the object must be updated by
<code>update-instance-for-redefined-class</code>); then, we acquire the slot's
storage location from the slot definition, the value from the instance's
slot vector, and then after checking the value against the internal unbound
marker, we return it.
<div class="float">
<a name="ex_003aslot_002dvalue_002dusing_002dclass"></a>
<pre class="example"> (defmethod slot-value-using-class
((class std-class)
(object standard-object)
(slotd standard-effective-slot-definition))
(check-obsolete-instance object)
(let* ((location (slot-definition-location slotd))
(value
(etypecase location
(fixnum (clos-slots-ref (instance-slots object) location))
(cons (cdr location)))))
(if (eq value +slot-unbound+)
(values (slot-unbound class object (slot-definition-name slotd)))
value)))
</pre>
<p><strong class="float-caption">Example 7.2</strong></p></div>
<p>Clearly, all of this activity will cause the performance of clos slot
access to compare poorly with structure slot access; while there will be
of necessity a slowdown between the slot accesses because the structure
class need not be redefineable (while redefinition of standard-object
classes is extremely common), the overhead presented in the above
implementation is excessive.
</body></html>