aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-10-29 12:27:04 +0100
committerMattias EngdegÄrd2024-01-12 18:02:15 +0100
commit462b3e6ae4eefeb65a2dc7b144db3e1af9a7720d (patch)
tree57c5f0aa6c081602b517bd0c7f987277fe8fa063 /src
parent0bc13945acb8d18bc18b5abc5c5cf9adebc46ca6 (diff)
downloademacs-462b3e6ae4eefeb65a2dc7b144db3e1af9a7720d.tar.gz
emacs-462b3e6ae4eefeb65a2dc7b144db3e1af9a7720d.zip
Refactor: extract hash and index computations to functions
* src/lisp.h (hash_from_key): * src/fns.c (hash_index_index): New. (hash_table_rehash, hash_lookup, hash_remove_from_table): (maybe_resize_hash_table, hash_put): * src/composite.c (composition_gstring_put_cache): Use them.
Diffstat (limited to 'src')
-rw-r--r--src/composite.c2
-rw-r--r--src/fns.c34
-rw-r--r--src/lisp.h7
3 files changed, 26 insertions, 17 deletions
diff --git a/src/composite.c b/src/composite.c
index 91836fa2a8f..7c7f4720514 100644
--- a/src/composite.c
+++ b/src/composite.c
@@ -653,7 +653,7 @@ composition_gstring_put_cache (Lisp_Object gstring, ptrdiff_t len)
653{ 653{
654 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table); 654 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
655 Lisp_Object header = LGSTRING_HEADER (gstring); 655 Lisp_Object header = LGSTRING_HEADER (gstring);
656 Lisp_Object hash = h->test.hashfn (header, h); 656 Lisp_Object hash = hash_from_key (h, header);
657 if (len < 0) 657 if (len < 0)
658 { 658 {
659 ptrdiff_t glyph_len = LGSTRING_GLYPH_LEN (gstring); 659 ptrdiff_t glyph_len = LGSTRING_GLYPH_LEN (gstring);
diff --git a/src/fns.c b/src/fns.c
index 33ee7c3d36e..207094909f4 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4631,6 +4631,13 @@ copy_hash_table (struct Lisp_Hash_Table *h1)
4631} 4631}
4632 4632
4633 4633
4634/* Compute index into the index vector from a hash value. */
4635static inline ptrdiff_t
4636hash_index_index (struct Lisp_Hash_Table *h, Lisp_Object hash_code)
4637{
4638 return XUFIXNUM (hash_code) % ASIZE (h->index);
4639}
4640
4634/* Resize hash table H if it's too full. If H cannot be resized 4641/* Resize hash table H if it's too full. If H cannot be resized
4635 because it's already too large, throw an error. */ 4642 because it's already too large, throw an error. */
4636 4643
@@ -4689,8 +4696,8 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
4689 for (ptrdiff_t i = 0; i < old_size; i++) 4696 for (ptrdiff_t i = 0; i < old_size; i++)
4690 if (!NILP (HASH_HASH (h, i))) 4697 if (!NILP (HASH_HASH (h, i)))
4691 { 4698 {
4692 EMACS_UINT hash_code = XUFIXNUM (HASH_HASH (h, i)); 4699 Lisp_Object hash_code = HASH_HASH (h, i);
4693 ptrdiff_t start_of_bucket = hash_code % ASIZE (h->index); 4700 ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
4694 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket)); 4701 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
4695 set_hash_index_slot (h, start_of_bucket, i); 4702 set_hash_index_slot (h, start_of_bucket, i);
4696 } 4703 }
@@ -4718,8 +4725,8 @@ hash_table_rehash (Lisp_Object hash)
4718 for (i = 0; i < count; i++) 4725 for (i = 0; i < count; i++)
4719 { 4726 {
4720 Lisp_Object key = HASH_KEY (h, i); 4727 Lisp_Object key = HASH_KEY (h, i);
4721 Lisp_Object hash_code = h->test.hashfn (key, h); 4728 Lisp_Object hash_code = hash_from_key (h, key);
4722 ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index); 4729 ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
4723 set_hash_hash_slot (h, i, hash_code); 4730 set_hash_hash_slot (h, i, hash_code);
4724 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket)); 4731 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
4725 set_hash_index_slot (h, start_of_bucket, i); 4732 set_hash_index_slot (h, start_of_bucket, i);
@@ -4738,15 +4745,12 @@ hash_table_rehash (Lisp_Object hash)
4738ptrdiff_t 4745ptrdiff_t
4739hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object *hash) 4746hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object *hash)
4740{ 4747{
4741 ptrdiff_t start_of_bucket, i; 4748 Lisp_Object hash_code = hash_from_key (h, key);
4742
4743 Lisp_Object hash_code;
4744 hash_code = h->test.hashfn (key, h);
4745 if (hash) 4749 if (hash)
4746 *hash = hash_code; 4750 *hash = hash_code;
4747 4751
4748 start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index); 4752 ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
4749 4753 ptrdiff_t i;
4750 for (i = HASH_INDEX (h, start_of_bucket); 0 <= i; i = HASH_NEXT (h, i)) 4754 for (i = HASH_INDEX (h, start_of_bucket); 0 <= i; i = HASH_NEXT (h, i))
4751 if (EQ (key, HASH_KEY (h, i)) 4755 if (EQ (key, HASH_KEY (h, i))
4752 || (h->test.cmpfn 4756 || (h->test.cmpfn
@@ -4773,14 +4777,12 @@ ptrdiff_t
4773hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value, 4777hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
4774 Lisp_Object hash) 4778 Lisp_Object hash)
4775{ 4779{
4776 ptrdiff_t start_of_bucket, i;
4777
4778 /* Increment count after resizing because resizing may fail. */ 4780 /* Increment count after resizing because resizing may fail. */
4779 maybe_resize_hash_table (h); 4781 maybe_resize_hash_table (h);
4780 h->count++; 4782 h->count++;
4781 4783
4782 /* Store key/value in the key_and_value vector. */ 4784 /* Store key/value in the key_and_value vector. */
4783 i = h->next_free; 4785 ptrdiff_t i = h->next_free;
4784 eassert (NILP (HASH_HASH (h, i))); 4786 eassert (NILP (HASH_HASH (h, i)));
4785 eassert (BASE_EQ (Qunbound, (HASH_KEY (h, i)))); 4787 eassert (BASE_EQ (Qunbound, (HASH_KEY (h, i))));
4786 h->next_free = HASH_NEXT (h, i); 4788 h->next_free = HASH_NEXT (h, i);
@@ -4791,7 +4793,7 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
4791 set_hash_hash_slot (h, i, hash); 4793 set_hash_hash_slot (h, i, hash);
4792 4794
4793 /* Add new entry to its collision chain. */ 4795 /* Add new entry to its collision chain. */
4794 start_of_bucket = XUFIXNUM (hash) % ASIZE (h->index); 4796 ptrdiff_t start_of_bucket = hash_index_index (h, hash);
4795 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket)); 4797 set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
4796 set_hash_index_slot (h, start_of_bucket, i); 4798 set_hash_index_slot (h, start_of_bucket, i);
4797 return i; 4799 return i;
@@ -4803,8 +4805,8 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
4803void 4805void
4804hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key) 4806hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
4805{ 4807{
4806 Lisp_Object hash_code = h->test.hashfn (key, h); 4808 Lisp_Object hash_code = hash_from_key (h, key);
4807 ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index); 4809 ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
4808 ptrdiff_t prev = -1; 4810 ptrdiff_t prev = -1;
4809 4811
4810 for (ptrdiff_t i = HASH_INDEX (h, start_of_bucket); 4812 for (ptrdiff_t i = HASH_INDEX (h, start_of_bucket);
diff --git a/src/lisp.h b/src/lisp.h
index 5ec895ecc81..a34726adbcb 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2524,6 +2524,13 @@ HASH_TABLE_SIZE (const struct Lisp_Hash_Table *h)
2524 return size; 2524 return size;
2525} 2525}
2526 2526
2527/* Compute hash value for KEY in hash table H. */
2528INLINE Lisp_Object
2529hash_from_key (struct Lisp_Hash_Table *h, Lisp_Object key)
2530{
2531 return h->test.hashfn (key, h);
2532}
2533
2527void hash_table_rehash (Lisp_Object); 2534void hash_table_rehash (Lisp_Object);
2528 2535
2529/* Default size for hash tables if not specified. */ 2536/* Default size for hash tables if not specified. */