aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorStefan Monnier2019-07-26 13:29:35 -0400
committerStefan Monnier2019-07-26 15:03:03 -0400
commitbbff294bf455a9ad4ae66edce8e70f29a40e2e6d (patch)
treed0285a4db6957074c22d074cc2a98eba36c3b6ea /src
parentaabb844e45642d6bf80673159dae18f4ea3693e4 (diff)
downloademacs-bbff294bf455a9ad4ae66edce8e70f29a40e2e6d.tar.gz
emacs-bbff294bf455a9ad4ae66edce8e70f29a40e2e6d.zip
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c33
-rw-r--r--src/pdumper.c7
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)
4053 - header_size - GCALIGNMENT) \ 4053 - header_size - GCALIGNMENT) \
4054 / word_size))) 4054 / word_size)))
4055 4055
4056static ptrdiff_t
4057hash_index_size (struct Lisp_Hash_Table *h, ptrdiff_t size)
4058{
4059 double threshold = h->rehash_threshold;
4060 double index_float = size / threshold;
4061 ptrdiff_t index_size = (index_float < INDEX_SIZE_BOUND + 1
4062 ? next_almost_prime (index_float)
4063 : INDEX_SIZE_BOUND + 1);
4064 if (INDEX_SIZE_BOUND < index_size)
4065 error ("Hash table too large");
4066 return index_size;
4067}
4068
4056/* Create and initialize a new hash table. 4069/* Create and initialize a new hash table.
4057 4070
4058 TEST specifies the test the hash table will use to compare keys. 4071 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,
4086{ 4099{
4087 struct Lisp_Hash_Table *h; 4100 struct Lisp_Hash_Table *h;
4088 Lisp_Object table; 4101 Lisp_Object table;
4089 EMACS_INT index_size;
4090 ptrdiff_t i; 4102 ptrdiff_t i;
4091 double index_float;
4092 4103
4093 /* Preconditions. */ 4104 /* Preconditions. */
4094 eassert (SYMBOLP (test.name)); 4105 eassert (SYMBOLP (test.name));
@@ -4099,14 +4110,6 @@ make_hash_table (struct hash_table_test test, EMACS_INT size,
4099 if (size == 0) 4110 if (size == 0)
4100 size = 1; 4111 size = 1;
4101 4112
4102 double threshold = rehash_threshold;
4103 index_float = size / threshold;
4104 index_size = (index_float < INDEX_SIZE_BOUND + 1
4105 ? next_almost_prime (index_float)
4106 : INDEX_SIZE_BOUND + 1);
4107 if (INDEX_SIZE_BOUND < max (index_size, 2 * size))
4108 error ("Hash table too large");
4109
4110 /* Allocate a table and initialize it. */ 4113 /* Allocate a table and initialize it. */
4111 h = allocate_hash_table (); 4114 h = allocate_hash_table ();
4112 4115
@@ -4119,7 +4122,7 @@ make_hash_table (struct hash_table_test test, EMACS_INT size,
4119 h->key_and_value = make_nil_vector (2 * size); 4122 h->key_and_value = make_nil_vector (2 * size);
4120 h->hash = make_nil_vector (size); 4123 h->hash = make_nil_vector (size);
4121 h->next = make_vector (size, make_fixnum (-1)); 4124 h->next = make_vector (size, make_fixnum (-1));
4122 h->index = make_vector (index_size, make_fixnum (-1)); 4125 h->index = make_vector (hash_index_size (h, size), make_fixnum (-1));
4123 h->next_weak = NULL; 4126 h->next_weak = NULL;
4124 h->purecopy = purecopy; 4127 h->purecopy = purecopy;
4125 h->mutable = true; 4128 h->mutable = true;
@@ -4195,13 +4198,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
4195 for (ptrdiff_t i = old_size; i < next_size - 1; i++) 4198 for (ptrdiff_t i = old_size; i < next_size - 1; i++)
4196 gc_aset (next, i, make_fixnum (i + 1)); 4199 gc_aset (next, i, make_fixnum (i + 1));
4197 gc_aset (next, next_size - 1, make_fixnum (-1)); 4200 gc_aset (next, next_size - 1, make_fixnum (-1));
4198 double threshold = h->rehash_threshold; 4201 ptrdiff_t index_size = hash_index_size (h, next_size);
4199 double index_float = next_size / threshold;
4200 EMACS_INT index_size = (index_float < INDEX_SIZE_BOUND + 1
4201 ? next_almost_prime (index_float)
4202 : INDEX_SIZE_BOUND + 1);
4203 if (INDEX_SIZE_BOUND < index_size)
4204 error ("Hash table too large to resize");
4205 Lisp_Object key_and_value 4202 Lisp_Object key_and_value
4206 = larger_vector (h->key_and_value, 2 * (next_size - old_size), 4203 = larger_vector (h->key_and_value, 2 * (next_size - old_size),
4207 2 * next_size); 4204 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)
2662static void 2662static void
2663check_hash_table_rehash (Lisp_Object table_orig) 2663check_hash_table_rehash (Lisp_Object table_orig)
2664{ 2664{
2665 ptrdiff_t count = XHASH_TABLE (table_orig)->count;
2665 hash_rehash_if_needed (XHASH_TABLE (table_orig)); 2666 hash_rehash_if_needed (XHASH_TABLE (table_orig));
2666 Lisp_Object table_rehashed = Fcopy_hash_table (table_orig); 2667 Lisp_Object table_rehashed = Fcopy_hash_table (table_orig);
2667 eassert (XHASH_TABLE (table_rehashed)->count >= 0); 2668 eassert (!hash_rehash_needed_p (XHASH_TABLE (table_rehashed)));
2668 XHASH_TABLE (table_rehashed)->count *= -1; 2669 XHASH_TABLE (table_rehashed)->count *= -1;
2669 eassert (XHASH_TABLE (table_rehashed)->count <= 0); 2670 eassert (count == 0 || hash_rehash_needed_p (XHASH_TABLE (table_rehashed)));
2670 hash_rehash_if_needed (XHASH_TABLE (table_rehashed)); 2671 hash_rehash_if_needed (XHASH_TABLE (table_rehashed));
2671 eassert (XHASH_TABLE (table_rehashed)->count >= 0); 2672 eassert (!hash_rehash_needed_p (XHASH_TABLE (table_rehashed)));
2672 Lisp_Object expected_contents = hash_table_contents (table_orig); 2673 Lisp_Object expected_contents = hash_table_contents (table_orig);
2673 while (!NILP (expected_contents)) 2674 while (!NILP (expected_contents))
2674 { 2675 {