On 28 Dec 2006, at 00:27, Gary King wrote:
> On Dec 27, 2006, at 3:43 PM, Edi Weitz wrote:
>
>>
>>> The problem is that it's significantly faster to access slots via
>>> accessors than via SLOT-VALUE in SBCL.
>>
>> Ah! Who would have thought?
>
> Indeed?! Why is this so?
slot-value has to do a lookup of the slot in the vector that
underlies a given object. Since the mapping of slot names to indexes
into such vectors is not fixed in CLOS (it can vary according to the
class hierarchy, especially in complex cases involving multiple
inheritance, and it can vary over time), this can be an expensive
operation in the general case. The CLOS and CLOS MOP specifications
have a number of restrictions of what can happen that allows slot-
value to be replaced by faster slot accesses in some cases, but they
don't always hold.
The accessor functions have an advantage over slot-value because they
are specific to slot definitions, so they can already have some
information about slot locations procomputed. Furthermore, it is
relatively straightforward to replace the slot accessor methods with
new versions when more information about slots becomes available.
It is generally better to use accessor functions than to use direct
slot accesses, both for hiding implementation details (accessors can
more easily be replaced by plain functions in case of design
changes), for the ability to add :before/:after/:around methods in a
straightforward way, _and_ for performance reasons. To the best of my
knowledge, this is the case for all CLOS implementations.
Pascal
--
Pascal Costanza, mailto:pc@..., http://p-cos.net
Vrije Universiteit Brussel, Programming Technology Lab
Pleinlaan 2, B-1050 Brussel, Belgium
|