From 2ab6f0183ba2fb014cc028aeb8afb6a2aba7c059 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 Feb 2019 07:44:35 -0800 Subject: Optimize hash operation with unsafe car/cdr. The associative lists that make up the chains of a hash table are guaranteed to be made of conses. We can use unsafe versions of car, cdr, rplaca and rplacd to speed up hash operations. * eval.c (op_dohash): Use unsafe operations on hash cell. * filter.c (trie_compress, regex_from_trie): Likewise. * hash.c (hash_equal_op, hash_print_op, hash_mark, hash_grow, hash_assoc, hash_assql, copy_hash_chain, gethash, inhash, gethash_n, sethash, remhash, hash_next, maphash, do_weak_tables, group_by, group_reduce, hash_keys_lazy, hash_keys, hash_values_lazy, hash_values, hash_pairs_lazy, hash_pairs, hash_alist_lazy, hash_uni, hash_diff, hash_symdiff, hash_isec, hash_subset, hash_update, hash_update_1, hash_revget): Likewise. * lib.c (us_rplaca, us_rplacd): New functions. (package_local_symbols, package_foreign_symbols, where, populate_obj_hash, obj_hash_merge): Use unsafe operations on hash cell * lib.h (us_rplaca, us_rplacd): Declared. * parser.c (circ_backpatch, get_visible_syms): Use unsafe operations on hash cell. * struct.c (method_name, get_slot_syms): Likewise. --- lib.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 5544198a..6e776bb9 100644 --- a/lib.c +++ b/lib.c @@ -568,6 +568,18 @@ val rplacd(val cons, val new_cdr) } } +val us_rplaca(val cons, val new_car) +{ + set(mkloc(cons->c.car, cons), new_car); + return cons; +} + +val us_rplacd(val cons, val new_cdr) +{ + set(mkloc(cons->c.cdr, cons), new_cdr); + return cons; +} + val sys_rplaca(val cons, val new_car) { (void) rplaca(cons, new_car); @@ -5345,7 +5357,7 @@ val package_local_symbols(val package_in) val cell; while ((cell = hash_next(hiter))) { - val sym = cdr(cell); + val sym = us_cdr(cell); if (symbol_package(sym) == package) ptail = list_collect(ptail, sym); } @@ -5361,7 +5373,7 @@ val package_foreign_symbols(val package_in) val cell; while ((cell = hash_next(hiter))) { - val sym = cdr(cell); + val sym = us_cdr(cell); if (symbol_package(sym) != package) ptail = list_collect(ptail, sym); } @@ -10653,8 +10665,8 @@ val where(val func, val seq) val cell; while ((cell = hash_next(hiter))) - if (funcall1(func, cdr(cell))) - ptail = list_collect(ptail, car(cell)); + if (funcall1(func, us_cdr(cell))) + ptail = list_collect(ptail, us_car(cell)); break; } /* fallthrough */ @@ -11646,8 +11658,8 @@ tail: val iter = hash_begin(obj); val cell; while ((cell = hash_next(iter))) { - populate_obj_hash(car(cell), ctx); - populate_obj_hash(cdr(cell), ctx); + populate_obj_hash(us_car(cell), ctx); + populate_obj_hash(us_cdr(cell), ctx); } obj = get_hash_userdata(obj); goto tail; @@ -11680,9 +11692,9 @@ static void obj_hash_merge(val parent_hash, val child_hash) for (iter = hash_begin(child_hash); (cell = hash_next(iter));) { val new_p; - loc pcdr = gethash_l(self, parent_hash, car(cell), mkcloc(new_p)); + loc pcdr = gethash_l(self, parent_hash, us_car(cell), mkcloc(new_p)); if (new_p) - set(pcdr, cdr(cell)); + set(pcdr, us_cdr(cell)); else uw_throwf(error_s, lit("~a: unexpected duplicate object " "(internal error?)"), self, nao); -- cgit v1.2.3