From 6aececdf6040e26fef0bb634e832883943a4f1d0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 20 Sep 2019 07:47:16 -0700 Subject: equal: reduce type checking for conses. * lib.c (equal): Since we have switched on the type of the left and right argument, we can access the object directly instead of going through car and cdr. Except that for a lazy conses, we need at least one such access to force the object first. --- lib.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 862bb1ce..ebea0d00 100644 --- a/lib.c +++ b/lib.c @@ -2681,12 +2681,31 @@ val equal(val left, val right) case NUM: break; case CONS: + switch (type(right)) { + case CONS: + if (equal(left->c.car, right->c.car) && equal(left->c.cdr, right->c.cdr)) + return t; + return nil; + case LCONS: + if (equal(left->c.car, car(right)) && equal(left->c.cdr, right->c.cdr)) + return t; + return nil; + default: + break; + } + return nil; case LCONS: - if (type(right) == CONS || type(right) == LCONS) - { - if (equal(car(left), car(right)) && equal(cdr(left), cdr(right))) + switch (type(right)) { + case CONS: + if (equal(car(left), right->c.car) && equal(left->c.cdr, right->c.cdr)) return t; return nil; + case LCONS: + if (equal(car(left), car(right)) && equal(left->c.cdr, right->c.cdr)) + return t; + return nil; + default: + break; } break; case LIT: -- cgit v1.2.3