From bbff294bf455a9ad4ae66edce8e70f29a40e2e6d Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Fri, 26 Jul 2019 13:29:35 -0400 Subject: [PATCH] * src/fns.c (hash_index_size): New function, extracted from make_hash_table (make_hash_table, maybe_resize_hash_table): Use it. * src/pdumper.c (check_hash_table_rehash): Use hash_rehash_needed_p. --- src/fns.c | 33 +++++++++++++++------------------ src/pdumper.c | 7 ++++--- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/fns.c b/src/fns.c index c45f4556463..49ec0a85378 100644 --- a/src/fns.c +++ b/src/fns.c @@ -4053,6 +4053,19 @@ allocate_hash_table (void) - header_size - GCALIGNMENT) \ / word_size))) +static ptrdiff_t +hash_index_size (struct Lisp_Hash_Table *h, ptrdiff_t size) +{ + double threshold = h->rehash_threshold; + double index_float = size / threshold; + ptrdiff_t index_size = (index_float < INDEX_SIZE_BOUND + 1 + ? next_almost_prime (index_float) + : INDEX_SIZE_BOUND + 1); + if (INDEX_SIZE_BOUND < index_size) + error ("Hash table too large"); + return index_size; +} + /* Create and initialize a new hash table. TEST specifies the test the hash table will use to compare keys. @@ -4086,9 +4099,7 @@ make_hash_table (struct hash_table_test test, EMACS_INT size, { struct Lisp_Hash_Table *h; Lisp_Object table; - EMACS_INT index_size; ptrdiff_t i; - double index_float; /* Preconditions. */ eassert (SYMBOLP (test.name)); @@ -4099,14 +4110,6 @@ make_hash_table (struct hash_table_test test, EMACS_INT size, if (size == 0) size = 1; - double threshold = rehash_threshold; - index_float = size / threshold; - index_size = (index_float < INDEX_SIZE_BOUND + 1 - ? next_almost_prime (index_float) - : INDEX_SIZE_BOUND + 1); - if (INDEX_SIZE_BOUND < max (index_size, 2 * size)) - error ("Hash table too large"); - /* Allocate a table and initialize it. */ h = allocate_hash_table (); @@ -4119,7 +4122,7 @@ make_hash_table (struct hash_table_test test, EMACS_INT size, h->key_and_value = make_nil_vector (2 * size); h->hash = make_nil_vector (size); h->next = make_vector (size, make_fixnum (-1)); - h->index = make_vector (index_size, make_fixnum (-1)); + h->index = make_vector (hash_index_size (h, size), make_fixnum (-1)); h->next_weak = NULL; h->purecopy = purecopy; h->mutable = true; @@ -4195,13 +4198,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h) for (ptrdiff_t i = old_size; i < next_size - 1; i++) gc_aset (next, i, make_fixnum (i + 1)); gc_aset (next, next_size - 1, make_fixnum (-1)); - double threshold = h->rehash_threshold; - double index_float = next_size / threshold; - EMACS_INT index_size = (index_float < INDEX_SIZE_BOUND + 1 - ? next_almost_prime (index_float) - : INDEX_SIZE_BOUND + 1); - if (INDEX_SIZE_BOUND < index_size) - error ("Hash table too large to resize"); + ptrdiff_t index_size = hash_index_size (h, next_size); Lisp_Object key_and_value = larger_vector (h->key_and_value, 2 * (next_size - old_size), 2 * next_size); diff --git a/src/pdumper.c b/src/pdumper.c index 1504f75c825..4ba819b4103 100644 --- a/src/pdumper.c +++ b/src/pdumper.c @@ -2662,13 +2662,14 @@ hash_table_contents (Lisp_Object table) static void check_hash_table_rehash (Lisp_Object table_orig) { + ptrdiff_t count = XHASH_TABLE (table_orig)->count; hash_rehash_if_needed (XHASH_TABLE (table_orig)); Lisp_Object table_rehashed = Fcopy_hash_table (table_orig); - eassert (XHASH_TABLE (table_rehashed)->count >= 0); + eassert (!hash_rehash_needed_p (XHASH_TABLE (table_rehashed))); XHASH_TABLE (table_rehashed)->count *= -1; - eassert (XHASH_TABLE (table_rehashed)->count <= 0); + eassert (count == 0 || hash_rehash_needed_p (XHASH_TABLE (table_rehashed))); hash_rehash_if_needed (XHASH_TABLE (table_rehashed)); - eassert (XHASH_TABLE (table_rehashed)->count >= 0); + eassert (!hash_rehash_needed_p (XHASH_TABLE (table_rehashed))); Lisp_Object expected_contents = hash_table_contents (table_orig); while (!NILP (expected_contents)) { -- 2.39.2