From 039f9c10f375785c82a09cd32c4e887a5362fb8c Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 15 Sep 2011 14:23:44 -0600 Subject: [PATCH] fix calculation for hash code of a hash table --- collects/tests/racket/basic.rktl | 10 ++++++++++ src/racket/src/hash.c | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/collects/tests/racket/basic.rktl b/collects/tests/racket/basic.rktl index d43696d83b..52ce51c3cf 100644 --- a/collects/tests/racket/basic.rktl +++ b/collects/tests/racket/basic.rktl @@ -2521,6 +2521,16 @@ (list? (list* 1 2 p)) (test 1 hash-ref ht p #f)) +;; Check that hash-table cycles don't lead to an +;; explosion in the time to compute a hash code. +(let () + (define ht (make-hash)) + (hash-set! ht 'a ht) + (hash-set! ht 'b ht) + (eq-hash-code ht) + (equal-hash-code ht) + (equal-secondary-hash-code ht)) + ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Misc diff --git a/src/racket/src/hash.c b/src/racket/src/hash.c index e8db4143cb..666711a78a 100644 --- a/src/racket/src/hash.c +++ b/src/racket/src/hash.c @@ -1264,7 +1264,7 @@ static uintptr_t equal_hash_key(Scheme_Object *o, uintptr_t k, Hash_Info *hi) # include "mzhashchk.inc" k = (k << 1) + 3; - hi->depth += 2; + hi->depth *= 2; /* mult to counteract potential explosion due to old_depth reset */ old_depth = hi->depth; keys = ht->keys; @@ -1676,7 +1676,7 @@ static uintptr_t equal_hash_key2(Scheme_Object *o, Hash_Info *hi) # include "mzhashchk.inc" - hi->depth += 2; + hi->depth *= 2; /* mult to counteract potential explosion due to old_depth reset */ old_depth = hi->depth; keys = ht->keys;