[pro] When to use SLOT-VALUE...

Nikodemus Siivola nikodemus at random-state.net
Tue Nov 16 21:15:20 UTC 2010


One angle that hasn't been mentioned yet is efficiency.

There's a fairly well known optimization (permutation vectors) that allows

 (defmethod foo ((x bar))
   ...
   (slot-value x 'quux)
   ...)

to be very efficient as long as X isn't assigned to (and as long as
there is not SLOT-VALUE-USING-CLASS, etc) -- not as fast as plain
structure accessors, but basically at the cost of two memory
indirections. A laugh compared to calling a generic accessor.

That said, I *think* permutation vectors can be extended to accessors
as well, but I don't know how commonly that is implemented (eg. SBCL
at least currently doesn't) -- whereas most(?) implementations,
particularly those deriving their CLOS from PCL probably implement the
basic permutation vector optimization.

Of course, readability and maintainability trump efficiency any time,
as long as the code if efficient enough for its intended purpose.

Still, something to be aware of when choosing between accessors and SLOT-VALUE.

Cheers,

 -- Nikodemus




More information about the pro mailing list