From 13ce6127686ffc6efc6d0f43733bfd140537d05f Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 7 Jul 2016 21:42:12 -0400 Subject: [PATCH] Massive speedup comparing certain types of lists --- runtime.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/runtime.c b/runtime.c index ab60d34b..f9f54eda 100644 --- a/runtime.c +++ b/runtime.c @@ -877,11 +877,13 @@ object memqp(void *data, object x, list l) } /** + * Check two objects for deep equality */ object equalp(object x, object y) { - object slow_lis = x, fast_lis = NULL; int second_cycle = 0; + object slow_lis = x, fast_lis = NULL; + object pcar_x = &second_cycle, pcar_y = &second_cycle; // never a car value if (Cyc_is_pair(x) == boolean_t && Cyc_is_pair(cdr(x)) == boolean_t){ @@ -897,8 +899,15 @@ object equalp(object x, object y) return boolean_f; // Both objects are lists at this point, compare cars - if (boolean_f == equalp(car(x), car(y))) - return boolean_f; + if (pcar_x == car(x) && + pcar_y == car(y)) { + // do nothing, already equal + } else { + if (boolean_f == equalp(car(x), car(y))) + return boolean_f; + pcar_x = car(x); + pcar_y = car(y); + } // If there is no cycle, keep checking equality if (fast_lis == NULL ||