[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 462b3e6ae4e 06/11: Refactor: extract hash and index computations
From: |
Mattias Engdegård |
Subject: |
master 462b3e6ae4e 06/11: Refactor: extract hash and index computations to functions |
Date: |
Fri, 12 Jan 2024 12:04:46 -0500 (EST) |
branch: master
commit 462b3e6ae4eefeb65a2dc7b144db3e1af9a7720d
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>
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.
---
src/composite.c | 2 +-
src/fns.c | 34 ++++++++++++++++++----------------
src/lisp.h | 7 +++++++
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)
{
struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
Lisp_Object header = LGSTRING_HEADER (gstring);
- Lisp_Object hash = h->test.hashfn (header, h);
+ Lisp_Object hash = hash_from_key (h, header);
if (len < 0)
{
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)
}
+/* 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);
}
@@ -4718,8 +4725,8 @@ hash_table_rehash (Lisp_Object hash)
for (i = 0; i < count; i++)
{
Lisp_Object key = HASH_KEY (h, i);
- Lisp_Object hash_code = h->test.hashfn (key, h);
- ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index);
+ Lisp_Object hash_code = hash_from_key (h, key);
+ 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,15 +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_code = h->test.hashfn (key, h);
+ 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
@@ -4773,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);
@@ -4791,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,8 +4805,8 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key,
Lisp_Object value,
void
hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
{
- Lisp_Object hash_code = h->test.hashfn (key, h);
- ptrdiff_t start_of_bucket = XUFIXNUM (hash_code) % ASIZE (h->index);
+ Lisp_Object hash_code = hash_from_key (h, key);
+ 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);
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)
return size;
}
+/* Compute hash value for KEY in hash table H. */
+INLINE Lisp_Object
+hash_from_key (struct Lisp_Hash_Table *h, Lisp_Object key)
+{
+ return h->test.hashfn (key, h);
+}
+
void hash_table_rehash (Lisp_Object);
/* Default size for hash tables if not specified. */
- master updated (10cfbda8841 -> 8b7a6d7b6de), Mattias Engdegård, 2024/01/12
- master 22201dde773 03/11: Decouple profiler from Lisp hash table internals, Mattias Engdegård, 2024/01/12
- master 228e9000181 01/11: Add internal hash-table debug functions, Mattias Engdegård, 2024/01/12
- master 8acd89e955f 02/11: ; * src/pdumper.c (dump_hash_table): Remove unused argument., Mattias Engdegård, 2024/01/12
- master 3da324fbd3c 04/11: Refactor: less layering violation in composite.h, Mattias Engdegård, 2024/01/12
- master 0bc13945acb 05/11: ; * src/fns.c (collect_interval): Move misplaced function., Mattias Engdegård, 2024/01/12
- master 43127e5ec11 07/11: Refactor hash table vector reallocation, Mattias Engdegård, 2024/01/12
- master 484e04efa4f 08/11: ; * src/alloc.c (purecopy_hash_table): Simplify, Mattias Engdegård, 2024/01/12
- master 8b7a6d7b6de 11/11: ; * src/lisp.h (struct Lisp_Hash_Table): Add ASCII art., Mattias Engdegård, 2024/01/12
- master 29e3d1c56f0 09/11: Abstract predicate and constant for unused hash keys, Mattias Engdegård, 2024/01/12
- master 462b3e6ae4e 06/11: Refactor: extract hash and index computations to functions,
Mattias Engdegård <=
- master 4b7985db11c 10/11: ; * src/fns.c (Fmake_hash_table): ensure `test` is a bare symbol, Mattias Engdegård, 2024/01/12