aboutsummaryrefslogtreecommitdiffstats
path: root/src/fns.c
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-10-26 17:17:01 +0200
committerMattias EngdegÄrd2024-01-13 20:50:37 +0100
commitc3d0cc50faf588479db62e20ceabe044dd89e244 (patch)
tree32333d6369a1cddf1799a00ba46bd22d26d473af /src/fns.c
parentc6bdc1ea1dc7f9a0b6d92d443f34c42affde73d1 (diff)
downloademacs-c3d0cc50faf588479db62e20ceabe044dd89e244.tar.gz
emacs-c3d0cc50faf588479db62e20ceabe044dd89e244.zip
Remove rehash-threshold and rehash-size struct members
These parameters have no visible semantics and are hardly ever used, so just use the default values for all hash tables. This saves memory, shrinks the external representation, and will improve performance. * src/fns.c (std_rehash_size, std_rehash_threshold): New. (hash_index_size): Use std_rehash_threshold. Remove table argument. All callers updated. (make_hash_table): Remove rehash_size and rehash_threshold args. All callers updated. (maybe_resize_hash_table) (Fhash_table_rehash_size, Fhash_table_rehash_threshold): Use std_rehash_size and std_rehash_threshold. (Fmake_hash_table): Ignore :rehash-size and :rehash-threshold args. * src/lisp.h (struct Lisp_Hash_Table): Remove rehash_size and rehash_threshold fields. (DEFAULT_REHASH_THRESHOLD, DEFAULT_REHASH_SIZE): Remove. * src/lread.c (hash_table_from_plist): Don't read rehash-size or rehash-threshold. (syms_of_lread): Remove unused symbols. * src/print.c (print_object): Don't print rehash-size or rehash-threshold. * src/pdumper.c (dump_hash_table): Don't dump removed fields.
Diffstat (limited to 'src/fns.c')
-rw-r--r--src/fns.c101
1 files changed, 30 insertions, 71 deletions
diff --git a/src/fns.c b/src/fns.c
index 5837795f838..efec74d4959 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4509,11 +4509,17 @@ allocate_hash_table (void)
4509 - header_size - GCALIGNMENT) \ 4509 - header_size - GCALIGNMENT) \
4510 / word_size))) 4510 / word_size)))
4511 4511
4512/* Default factor by which to increase the size of a hash table. */
4513static const double std_rehash_size = 1.5;
4514
4515/* Resize hash table when number of entries / table size is >= this
4516 ratio. */
4517static const double std_rehash_threshold = 0.8125;
4518
4512static ptrdiff_t 4519static ptrdiff_t
4513hash_index_size (struct Lisp_Hash_Table *h, ptrdiff_t size) 4520hash_index_size (ptrdiff_t size)
4514{ 4521{
4515 double threshold = h->rehash_threshold; 4522 double index_float = size * (1.0 / std_rehash_threshold);
4516 double index_float = size / threshold;
4517 ptrdiff_t index_size = (index_float < INDEX_SIZE_BOUND + 1 4523 ptrdiff_t index_size = (index_float < INDEX_SIZE_BOUND + 1
4518 ? next_almost_prime (index_float) 4524 ? next_almost_prime (index_float)
4519 : INDEX_SIZE_BOUND + 1); 4525 : INDEX_SIZE_BOUND + 1);
@@ -4531,16 +4537,6 @@ hash_index_size (struct Lisp_Hash_Table *h, ptrdiff_t size)
4531 4537
4532 Give the table initial capacity SIZE, 0 <= SIZE <= MOST_POSITIVE_FIXNUM. 4538 Give the table initial capacity SIZE, 0 <= SIZE <= MOST_POSITIVE_FIXNUM.
4533 4539
4534 If REHASH_SIZE is equal to a negative integer, this hash table's
4535 new size when it becomes full is computed by subtracting
4536 REHASH_SIZE from its old size. Otherwise it must be positive, and
4537 the table's new size is computed by multiplying its old size by
4538 REHASH_SIZE + 1.
4539
4540 REHASH_THRESHOLD must be a float <= 1.0, and > 0. The table will
4541 be resized when the approximate ratio of table entries to table
4542 size exceeds REHASH_THRESHOLD.
4543
4544 WEAK specifies the weakness of the table. 4540 WEAK specifies the weakness of the table.
4545 4541
4546 If PURECOPY is non-nil, the table can be copied to pure storage via 4542 If PURECOPY is non-nil, the table can be copied to pure storage via
@@ -4549,7 +4545,6 @@ hash_index_size (struct Lisp_Hash_Table *h, ptrdiff_t size)
4549 4545
4550Lisp_Object 4546Lisp_Object
4551make_hash_table (struct hash_table_test test, EMACS_INT size, 4547make_hash_table (struct hash_table_test test, EMACS_INT size,
4552 float rehash_size, float rehash_threshold,
4553 hash_table_weakness_t weak, bool purecopy) 4548 hash_table_weakness_t weak, bool purecopy)
4554{ 4549{
4555 struct Lisp_Hash_Table *h; 4550 struct Lisp_Hash_Table *h;
@@ -4559,8 +4554,6 @@ make_hash_table (struct hash_table_test test, EMACS_INT size,
4559 /* Preconditions. */ 4554 /* Preconditions. */
4560 eassert (SYMBOLP (test.name)); 4555 eassert (SYMBOLP (test.name));
4561 eassert (0 <= size && size <= MOST_POSITIVE_FIXNUM); 4556 eassert (0 <= size && size <= MOST_POSITIVE_FIXNUM);
4562 eassert (rehash_size <= -1 || 0 < rehash_size);
4563 eassert (0 < rehash_threshold && rehash_threshold <= 1);
4564 4557
4565 if (size == 0) 4558 if (size == 0)
4566 size = 1; 4559 size = 1;
@@ -4571,13 +4564,11 @@ make_hash_table (struct hash_table_test test, EMACS_INT size,
4571 /* Initialize hash table slots. */ 4564 /* Initialize hash table slots. */
4572 h->test = test; 4565 h->test = test;
4573 h->weakness = weak; 4566 h->weakness = weak;
4574 h->rehash_threshold = rehash_threshold;
4575 h->rehash_size = rehash_size;
4576 h->count = 0; 4567 h->count = 0;
4577 h->key_and_value = make_vector (2 * size, HASH_UNUSED_ENTRY_KEY); 4568 h->key_and_value = make_vector (2 * size, HASH_UNUSED_ENTRY_KEY);
4578 h->hash = make_nil_vector (size); 4569 h->hash = make_nil_vector (size);
4579 h->next = make_vector (size, make_fixnum (-1)); 4570 h->next = make_vector (size, make_fixnum (-1));
4580 h->index = make_vector (hash_index_size (h, size), make_fixnum (-1)); 4571 h->index = make_vector (hash_index_size (size), make_fixnum (-1));
4581 h->next_weak = NULL; 4572 h->next_weak = NULL;
4582 h->purecopy = purecopy; 4573 h->purecopy = purecopy;
4583 h->mutable = true; 4574 h->mutable = true;
@@ -4648,18 +4639,12 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
4648 { 4639 {
4649 ptrdiff_t old_size = HASH_TABLE_SIZE (h); 4640 ptrdiff_t old_size = HASH_TABLE_SIZE (h);
4650 EMACS_INT new_size; 4641 EMACS_INT new_size;
4651 double rehash_size = h->rehash_size;
4652 4642
4653 if (rehash_size < 0) 4643 double float_new_size = old_size * std_rehash_size;
4654 new_size = old_size - rehash_size; 4644 if (float_new_size < EMACS_INT_MAX)
4645 new_size = float_new_size;
4655 else 4646 else
4656 { 4647 new_size = EMACS_INT_MAX;
4657 double float_new_size = old_size * (rehash_size + 1);
4658 if (float_new_size < EMACS_INT_MAX)
4659 new_size = float_new_size;
4660 else
4661 new_size = EMACS_INT_MAX;
4662 }
4663 if (PTRDIFF_MAX < new_size) 4648 if (PTRDIFF_MAX < new_size)
4664 new_size = PTRDIFF_MAX; 4649 new_size = PTRDIFF_MAX;
4665 if (new_size <= old_size) 4650 if (new_size <= old_size)
@@ -4682,7 +4667,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
4682 Lisp_Object hash = alloc_larger_vector (h->hash, new_size); 4667 Lisp_Object hash = alloc_larger_vector (h->hash, new_size);
4683 memclear (XVECTOR (hash)->contents + old_size, 4668 memclear (XVECTOR (hash)->contents + old_size,
4684 (new_size - old_size) * word_size); 4669 (new_size - old_size) * word_size);
4685 ptrdiff_t index_size = hash_index_size (h, new_size); 4670 ptrdiff_t index_size = hash_index_size (new_size);
4686 h->index = make_vector (index_size, make_fixnum (-1)); 4671 h->index = make_vector (index_size, make_fixnum (-1));
4687 h->key_and_value = key_and_value; 4672 h->key_and_value = key_and_value;
4688 h->hash = hash; 4673 h->hash = hash;
@@ -5281,15 +5266,6 @@ keys. Default is `eql'. Predefined are the tests `eq', `eql', and
5281:size SIZE -- A hint as to how many elements will be put in the table. 5266:size SIZE -- A hint as to how many elements will be put in the table.
5282Default is 65. 5267Default is 65.
5283 5268
5284:rehash-size REHASH-SIZE - Indicates how to expand the table when it
5285fills up. If REHASH-SIZE is an integer, increase the size by that
5286amount. If it is a float, it must be > 1.0, and the new size is the
5287old size multiplied by that factor. Default is 1.5.
5288
5289:rehash-threshold THRESHOLD -- THRESHOLD must a float > 0, and <= 1.0.
5290Resize the hash table when the ratio (table entries / table size)
5291exceeds an approximation to THRESHOLD. Default is 0.8125.
5292
5293:weakness WEAK -- WEAK must be one of nil, t, `key', `value', 5269:weakness WEAK -- WEAK must be one of nil, t, `key', `value',
5294`key-or-value', or `key-and-value'. If WEAK is not nil, the table 5270`key-or-value', or `key-and-value'. If WEAK is not nil, the table
5295returned is a weak table. Key/value pairs are removed from a weak 5271returned is a weak table. Key/value pairs are removed from a weak
@@ -5303,6 +5279,9 @@ to pure storage when Emacs is being dumped, making the contents of the
5303table read only. Any further changes to purified tables will result 5279table read only. Any further changes to purified tables will result
5304in an error. 5280in an error.
5305 5281
5282The keywords arguments :rehash-threshold and :rehash-size are obsolete
5283and ignored.
5284
5306usage: (make-hash-table &rest KEYWORD-ARGS) */) 5285usage: (make-hash-table &rest KEYWORD-ARGS) */)
5307 (ptrdiff_t nargs, Lisp_Object *args) 5286 (ptrdiff_t nargs, Lisp_Object *args)
5308{ 5287{
@@ -5352,26 +5331,6 @@ usage: (make-hash-table &rest KEYWORD-ARGS) */)
5352 else 5331 else
5353 signal_error ("Invalid hash table size", size_arg); 5332 signal_error ("Invalid hash table size", size_arg);
5354 5333
5355 /* Look for `:rehash-size SIZE'. */
5356 float rehash_size;
5357 i = get_key_arg (QCrehash_size, nargs, args, used);
5358 if (!i)
5359 rehash_size = DEFAULT_REHASH_SIZE;
5360 else if (FIXNUMP (args[i]) && 0 < XFIXNUM (args[i]))
5361 rehash_size = - XFIXNUM (args[i]);
5362 else if (FLOATP (args[i]) && 0 < (float) (XFLOAT_DATA (args[i]) - 1))
5363 rehash_size = (float) (XFLOAT_DATA (args[i]) - 1);
5364 else
5365 signal_error ("Invalid hash table rehash size", args[i]);
5366
5367 /* Look for `:rehash-threshold THRESHOLD'. */
5368 i = get_key_arg (QCrehash_threshold, nargs, args, used);
5369 float rehash_threshold = (!i ? DEFAULT_REHASH_THRESHOLD
5370 : !FLOATP (args[i]) ? 0
5371 : (float) XFLOAT_DATA (args[i]));
5372 if (! (0 < rehash_threshold && rehash_threshold <= 1))
5373 signal_error ("Invalid hash table rehash threshold", args[i]);
5374
5375 /* Look for `:weakness WEAK'. */ 5334 /* Look for `:weakness WEAK'. */
5376 i = get_key_arg (QCweakness, nargs, args, used); 5335 i = get_key_arg (QCweakness, nargs, args, used);
5377 Lisp_Object weakness = i ? args[i] : Qnil; 5336 Lisp_Object weakness = i ? args[i] : Qnil;
@@ -5392,11 +5351,16 @@ usage: (make-hash-table &rest KEYWORD-ARGS) */)
5392 /* Now, all args should have been used up, or there's a problem. */ 5351 /* Now, all args should have been used up, or there's a problem. */
5393 for (i = 0; i < nargs; ++i) 5352 for (i = 0; i < nargs; ++i)
5394 if (!used[i]) 5353 if (!used[i])
5395 signal_error ("Invalid argument list", args[i]); 5354 {
5355 /* Ignore obsolete arguments. */
5356 if (EQ (args[i], QCrehash_threshold) || EQ (args[i], QCrehash_size))
5357 i++;
5358 else
5359 signal_error ("Invalid argument list", args[i]);
5360 }
5396 5361
5397 SAFE_FREE (); 5362 SAFE_FREE ();
5398 return make_hash_table (testdesc, size, rehash_size, rehash_threshold, weak, 5363 return make_hash_table (testdesc, size, weak, purecopy);
5399 purecopy);
5400} 5364}
5401 5365
5402 5366
@@ -5422,14 +5386,8 @@ DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size,
5422 doc: /* Return the current rehash size of TABLE. */) 5386 doc: /* Return the current rehash size of TABLE. */)
5423 (Lisp_Object table) 5387 (Lisp_Object table)
5424{ 5388{
5425 double rehash_size = check_hash_table (table)->rehash_size; 5389 CHECK_HASH_TABLE (table);
5426 if (rehash_size < 0) 5390 return make_float (std_rehash_size);
5427 {
5428 EMACS_INT s = -rehash_size;
5429 return make_fixnum (min (s, MOST_POSITIVE_FIXNUM));
5430 }
5431 else
5432 return make_float (rehash_size + 1);
5433} 5391}
5434 5392
5435 5393
@@ -5438,7 +5396,8 @@ DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold,
5438 doc: /* Return the current rehash threshold of TABLE. */) 5396 doc: /* Return the current rehash threshold of TABLE. */)
5439 (Lisp_Object table) 5397 (Lisp_Object table)
5440{ 5398{
5441 return make_float (check_hash_table (table)->rehash_threshold); 5399 CHECK_HASH_TABLE (table);
5400 return make_float (std_rehash_threshold);
5442} 5401}
5443 5402
5444 5403