From 6fd179dc790d2296e7b05ec3aedb4fdeee587ecb Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 30 Jan 2025 06:04:13 -0800 Subject: 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. --- lib.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib.c b/lib.c index 44f2b1ef..e23c2ad3 100644 --- a/lib.c +++ b/lib.c @@ -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; } -- cgit v1.2.3