Re: metaclasses and performance
Christophe Rhodes via Sbcl-help <[email protected]> Sun, 22 Feb 2026 16:47:37 +0000
| Newsgroups | gmane.lisp.steel-bank.general |
|---|---|
| Message-ID | <[email protected]> |
Andrew Wolven <[email protected]> writes: > Hi, could one of the developers possibly answer a question for me? I'm answering but not in any way ex cathedra: everything below might be wrong. (But at least I hope it's plausible and gives you places to start looking for more certainty.) > Q: In SBCL, what operations are likely to be slower, even if only > slightly, when using a metaclass instead of standard-class? It depends on what your metaclass extends or customizes. Essentially, if your metaclass customizes some aspect of the protocol, you should expect that protocol to behave substantially worse. For example: if, for a given slot, there are no additional applicable methods on slot-value-using-class (and its sibling generic functions), I would expect that the permutation vector optimizations to slot-value (where `(slot-value ...)` of a constant slot-name for an instance which is a required parameter to the containing method) will continue to apply. (This is src/pcl/vector.lisp). If you *do* have such methods, then there will always be a generic function call, which the system goes to quite some lengths to avoid. Additionally, if there are no custom slot definitions at all in your metaclass, and no custom methods on the allocation protocol family of generic functions (allocate-instance and so on) then I would expect inline cached constructor optimization for call sites of make-instance with constant initargs to continue to fire and be as effective as with a standard-class. A full call to make-instance is expensive. > I have an application for classes which is particularly performance > sensitive that I want to keep optimized, and would like to use a > metaclass to add certain metadata to the mix. I suppose I could use a > class allocated slot, which actually brings me to another question > (that I could probably just try!) but I'll ask anyway: > > Q: If I have class allocated slots on a class and they have initforms, > but instead of running the make-instance machinery, I use > allocate-instance to create them instead of something that will run > shared-initialize, will the class allocated slots be filled? I'm not sure I completely understand the question. As asked, I think the answer is that calling allocate-instance will not change the state of the class-allocated slots. (A class-allocated slot is, more or less, isomorphic to a hash table where the keys are the class objects and the value is whatever: there isn't any space allocated for the class slot in the instance). If the class slot already has a value, when you call allocate-instance, that class slot will continue to have a value. If it doesn't, it won't have a value after calling allocate-instance. Christophe