[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/hash-table-perf 3d2042c48a6 07/35: Refactor: extract hash index
From: |
Mattias Engdegård |
Subject: |
scratch/hash-table-perf 3d2042c48a6 07/35: Refactor: extract hash index computation to a function |
Date: |
Thu, 4 Jan 2024 10:56:41 -0500 (EST) |
branch: scratch/hash-table-perf
commit 3d2042c48a60c3c4e43fcad06990c5de9e949878
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>
Refactor: extract hash index computation to a function
This way we have it in one place instead of five.
* src/fns.c (hash_index_index): New.
(maybe_resize_hash_table, hash_table_rehash, hash_lookup)
(hash_put, hash_remove_from_table): Use it.
---
src/fns.c | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/src/fns.c b/src/fns.c
index 1c6bd4fd526..aeaeea7375f 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4631,6 +4631,13 @@ copy_hash_table (struct Lisp_Hash_Table *h1)
}
+/* Compute index into the index vector from a hash value. */
+static inline ptrdiff_t
+hash_index_index (struct Lisp_Hash_Table *h, Lisp_Object hash_code)
+{
+ return XUFIXNUM (hash_code) % ASIZE (h->index);
+}
+
/* Resize hash table H if it's too full. If H cannot be resized
because it's already too large, throw an error. */
@@ -4689,8 +4696,8 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
for (ptrdiff_t i = 0; i < old_size; i++)
if (!NILP (HASH_HASH (h, i)))
{
- EMACS_UINT hash_code = XUFIXNUM (HASH_HASH (h, i));
- ptrdiff_t start_of_bucket = hash_code % ASIZE (h->index);
+ Lisp_Object hash_code = HASH_HASH (h, i);
+ ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
set_hash_index_slot (h, start_of_bucket, i);
}
@@ -4719,7 +4726,7 @@ hash_table_rehash (Lisp_Object hash)
{
Lisp_Object key = HASH_KEY (h, i);
Lisp_Object hash_code = hash_from_key (h, key);
- ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index);
+ ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
set_hash_hash_slot (h, i, hash_code);
set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
set_hash_index_slot (h, start_of_bucket, i);
@@ -4738,14 +4745,12 @@ hash_table_rehash (Lisp_Object hash)
ptrdiff_t
hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object *hash)
{
- ptrdiff_t start_of_bucket, i;
-
Lisp_Object hash_code = hash_from_key (h, key);
if (hash)
*hash = hash_code;
- start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index);
-
+ ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
+ ptrdiff_t i;
for (i = HASH_INDEX (h, start_of_bucket); 0 <= i; i = HASH_NEXT (h, i))
if (EQ (key, HASH_KEY (h, i))
|| (h->test.cmpfn
@@ -4772,14 +4777,12 @@ ptrdiff_t
hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
Lisp_Object hash)
{
- ptrdiff_t start_of_bucket, i;
-
/* Increment count after resizing because resizing may fail. */
maybe_resize_hash_table (h);
h->count++;
/* Store key/value in the key_and_value vector. */
- i = h->next_free;
+ ptrdiff_t i = h->next_free;
eassert (NILP (HASH_HASH (h, i)));
eassert (BASE_EQ (Qunbound, (HASH_KEY (h, i))));
h->next_free = HASH_NEXT (h, i);
@@ -4790,7 +4793,7 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key,
Lisp_Object value,
set_hash_hash_slot (h, i, hash);
/* Add new entry to its collision chain. */
- start_of_bucket = XUFIXNUM (hash) % ASIZE (h->index);
+ ptrdiff_t start_of_bucket = hash_index_index (h, hash);
set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
set_hash_index_slot (h, start_of_bucket, i);
return i;
@@ -4803,7 +4806,7 @@ void
hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
{
Lisp_Object hash_code = hash_from_key (h, key);
- ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index);
+ ptrdiff_t start_of_bucket = hash_index_index (h, hash_code);
ptrdiff_t prev = -1;
for (ptrdiff_t i = HASH_INDEX (h, start_of_bucket);
- scratch/hash-table-perf fc68176120f 24/35: Use hash_hash_t for storing hash values, (continued)
- scratch/hash-table-perf fc68176120f 24/35: Use hash_hash_t for storing hash values, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 4b5d9f92abe 13/35: * src/print.c (print_object): Don't print empty hash-table data, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 310f6584ccb 18/35: Allow zero hash table size, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf e53398ab698 26/35: ; Reorder structs (hash and test), Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 4e8d7725fd4 11/35: ; * src/fns.c (Fmake_hash_table): ensure `test` is a bare symbol, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 8335891387a 22/35: Inlined and specialised hash table look-up, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 54807fee4d0 23/35: Use hash_idx_t for storing hash indices, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 8cd35079f4c 34/35: Don't pretend that hash-table-size is useful, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf e6defe82569 27/35: Change default hash table size to 8 (from 65), Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 422c91a822a 02/35: ; * src/pdumper.c (dump_hash_table): Remove unused argument., Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 3d2042c48a6 07/35: Refactor: extract hash index computation to a function,
Mattias Engdegård <=
- scratch/hash-table-perf c188b9f2bf5 08/35: Refactor hash table vector reallocation, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 6ffbccbf1dd 15/35: Represent hash table weakness as an enum internally, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 3e9e68333ae 16/35: Remove rehash-threshold and rehash-size struct members, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 8e80d1930e3 20/35: Store hash values as EMACS_UINT instead of Lisp_Object, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 05297736aa6 33/35: Adapt hash functions to produce a hash_hash_t eventually, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 830838eb5f3 31/35: Use KEY=Qunbound instead of HASH=hash_unused for unused entries, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 8b1b140bed9 32/35: * src/lisp.h (hash_hash_t): Change to uint32_t., Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 681a2877cc2 35/35: Hash-table documentation updates, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf d77c9540363 06/35: Refactor: extract hash computation to a function, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf e69035c6ef5 17/35: Leaner hash table dumping and thawing, Mattias Engdegård, 2024/01/04