diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2025-01-30 06:04:13 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2025-01-30 06:04:13 -0800 |
commit | 81b5e08190cbb76a8bf02a4204d538800e1a9f1a (patch) | |
tree | 7e6160be9c7d84b43794505dd09b84df42a7e787 /lib.c | |
parent | 707ab6c2626575cd090a2ba02db17306a25d49ed (diff) | |
download | txr-81b5e08190cbb76a8bf02a4204d538800e1a9f1a.tar.gz txr-81b5e08190cbb76a8bf02a4204d538800e1a9f1a.tar.bz2 txr-81b5e08190cbb76a8bf02a4204d538800e1a9f1a.zip |
cobj: optimize subclass checks based on depth 1 assumption
Nowhere in the image do we have cobj_class inheritance
deeper than one. No class has a superclass which itself
has a superclass. Based on this, we can eliminate loops
coded to handle the general case.
* lib.c (sutypep, cobjclassp): Do not iterate to chase
the chain of super pointers. Do the subclass check
based on the assumption that there is at most a super
pointer to class which itself then has no super.
(cobj_register_super): Assert if the situation occurs
that a class is registered with a super that is not
a root. All these calls take place on startup, so if
the assumption is wrong, the assert will be 100%
reproducible.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 16 |
1 files changed, 6 insertions, 10 deletions
@@ -315,12 +315,8 @@ val subtypep(val sub, val sup) struct cobj_class *sup_cls = class_from_sym(sup); if (sub_cls && sup_cls) { - struct cobj_class *pcls = sub_cls; - do { - if (pcls == sup_cls) - return t; - pcls = pcls->super; - } while (pcls); + if (sub_cls->super == sup_cls) + return t; } } @@ -10547,6 +10543,7 @@ struct cobj_class *cobj_register_super(val cls_sym, struct cobj_class *super) { struct cobj_class *cls = cobj_register(cls_sym); cls->super = super; + bug_unless (cls->super->super == 0); return cls; } @@ -10578,10 +10575,9 @@ val cobjp(val obj) val cobjclassp(val obj, struct cobj_class *cls) { if (is_ptr(obj) && obj->t.type == COBJ) { - struct cobj_class *pcls; - for (pcls = obj->co.cls; pcls != 0; pcls = pcls->super) - if (pcls == cls) - return t; + struct cobj_class *ocls = obj->co.cls; + if (ocls == cls || ocls->super == cls) + return t; } return nil; } |