[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 29e3d1c56f0 09/11: Abstract predicate and constant for unused has
From: |
Mattias Engdegård |
Subject: |
master 29e3d1c56f0 09/11: Abstract predicate and constant for unused hash keys |
Date: |
Fri, 12 Jan 2024 12:04:49 -0500 (EST) |
branch: master
commit 29e3d1c56f07a53d1955c9a71e68f70f3b901728
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>
Abstract predicate and constant for unused hash keys
Qunbound is used for many things; using a predicate and constant for
the specific purpose of unused hash entry keys allows us to locate
them and make changes much more easily.
* src/lisp.h (HASH_UNUSED_ENTRY_KEY, hash_unused_entry_key_p):
New constant and function.
* src/comp.c (compile_function, Fcomp__compile_ctxt_to_file):
* src/composite.c (composition_gstring_cache_clear_font):
* src/emacs-module.c (module_global_reference_p):
* src/fns.c (make_hash_table, maybe_resize_hash_table, hash_put)
(hash_remove_from_table, hash_clear, sweep_weak_table, Fmaphash):
* src/json.c (lisp_to_json_nonscalar_1):
* src/minibuf.c (Ftry_completion, Fall_completions, Ftest_completion):
* src/print.c (print, print_object):
Use them.
---
src/comp.c | 8 ++++----
src/composite.c | 2 +-
src/emacs-module.c | 2 +-
src/fns.c | 14 +++++++-------
src/json.c | 2 +-
src/lisp.h | 12 +++++++++++-
src/minibuf.c | 10 +++++-----
src/print.c | 4 ++--
8 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/src/comp.c b/src/comp.c
index 347f8924793..2872c28a2b1 100644
--- a/src/comp.c
+++ b/src/comp.c
@@ -4334,7 +4334,7 @@ compile_function (Lisp_Object func)
{
Lisp_Object block_name = HASH_KEY (ht, i);
if (!EQ (block_name, Qentry)
- && !BASE_EQ (block_name, Qunbound))
+ && !hash_unused_entry_key_p (block_name))
declare_block (block_name);
}
@@ -4347,7 +4347,7 @@ compile_function (Lisp_Object func)
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (ht); i++)
{
Lisp_Object block_name = HASH_KEY (ht, i);
- if (!BASE_EQ (block_name, Qunbound))
+ if (!hash_unused_entry_key_p (block_name))
{
Lisp_Object block = HASH_VALUE (ht, i);
Lisp_Object insns = CALL1I (comp-block-insns, block);
@@ -4966,12 +4966,12 @@ DEFUN ("comp--compile-ctxt-to-file",
Fcomp__compile_ctxt_to_file,
struct Lisp_Hash_Table *func_h =
XHASH_TABLE (CALL1I (comp-ctxt-funcs-h, Vcomp_ctxt));
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (func_h); i++)
- if (!BASE_EQ (HASH_KEY (func_h, i), Qunbound))
+ if (!hash_unused_entry_key_p (HASH_KEY (func_h, i)))
declare_function (HASH_VALUE (func_h, i));
/* Compile all functions. Can't be done before because the
relocation structs has to be already defined. */
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (func_h); i++)
- if (!BASE_EQ (HASH_KEY (func_h, i), Qunbound))
+ if (!hash_unused_entry_key_p (HASH_KEY (func_h, i)))
compile_function (HASH_VALUE (func_h, i));
/* Work around bug#46495 (GCC PR99126). */
diff --git a/src/composite.c b/src/composite.c
index 7c7f4720514..ed1aeb380a0 100644
--- a/src/composite.c
+++ b/src/composite.c
@@ -690,7 +690,7 @@ composition_gstring_cache_clear_font (Lisp_Object
font_object)
{
Lisp_Object k = HASH_KEY (h, i);
- if (!BASE_EQ (k, Qunbound))
+ if (!hash_unused_entry_key_p (k))
{
Lisp_Object gstring = HASH_VALUE (h, i);
diff --git a/src/emacs-module.c b/src/emacs-module.c
index 46bd732e8eb..283703b3651 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -412,7 +412,7 @@ module_global_reference_p (emacs_value v, ptrdiff_t *n)
reference that's identical to some global reference. */
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
- if (!BASE_EQ (HASH_KEY (h, i), Qunbound)
+ if (!hash_unused_entry_key_p (HASH_KEY (h, i))
&& &XMODULE_GLOBAL_REFERENCE (HASH_VALUE (h, i))->value == v)
return true;
}
diff --git a/src/fns.c b/src/fns.c
index 56b4e9a18c0..d8da8992ce9 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4575,7 +4575,7 @@ make_hash_table (struct hash_table_test test, EMACS_INT
size,
h->rehash_threshold = rehash_threshold;
h->rehash_size = rehash_size;
h->count = 0;
- h->key_and_value = make_vector (2 * size, Qunbound);
+ h->key_and_value = make_vector (2 * size, HASH_UNUSED_ENTRY_KEY);
h->hash = make_nil_vector (size);
h->next = make_vector (size, make_fixnum (-1));
h->index = make_vector (hash_index_size (h, size), make_fixnum (-1));
@@ -4678,7 +4678,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
Lisp_Object key_and_value
= alloc_larger_vector (h->key_and_value, 2 * new_size);
for (ptrdiff_t i = 2 * old_size; i < 2 * new_size; i++)
- ASET (key_and_value, i, Qunbound);
+ ASET (key_and_value, i, HASH_UNUSED_ENTRY_KEY);
Lisp_Object hash = alloc_larger_vector (h->hash, new_size);
memclear (XVECTOR (hash)->contents + old_size,
@@ -4782,7 +4782,7 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key,
Lisp_Object value,
/* Store key/value in the key_and_value vector. */
ptrdiff_t i = h->next_free;
eassert (NILP (HASH_HASH (h, i)));
- eassert (BASE_EQ (Qunbound, (HASH_KEY (h, i))));
+ eassert (hash_unused_entry_key_p (HASH_KEY (h, i)));
h->next_free = HASH_NEXT (h, i);
set_hash_key_slot (h, i, key);
set_hash_value_slot (h, i, value);
@@ -4824,7 +4824,7 @@ hash_remove_from_table (struct Lisp_Hash_Table *h,
Lisp_Object key)
/* Clear slots in key_and_value and add the slots to
the free list. */
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
set_hash_hash_slot (h, i, Qnil);
set_hash_next_slot (h, i, h->next_free);
@@ -4851,7 +4851,7 @@ hash_clear (struct Lisp_Hash_Table *h)
for (ptrdiff_t i = 0; i < size; i++)
{
set_hash_next_slot (h, i, i < size - 1 ? i + 1 : -1);
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
}
@@ -4922,7 +4922,7 @@ sweep_weak_table (struct Lisp_Hash_Table *h, bool
remove_entries_p)
h->next_free = i;
/* Clear key, value, and hash. */
- set_hash_key_slot (h, i, Qunbound);
+ set_hash_key_slot (h, i, HASH_UNUSED_ENTRY_KEY);
set_hash_value_slot (h, i, Qnil);
if (!NILP (h->hash))
set_hash_hash_slot (h, i, Qnil);
@@ -5535,7 +5535,7 @@ FUNCTION is called with two arguments, KEY and VALUE.
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object k = HASH_KEY (h, i);
- if (!BASE_EQ (k, Qunbound))
+ if (!hash_unused_entry_key_p (k))
call2 (function, k, HASH_VALUE (h, i));
}
diff --git a/src/json.c b/src/json.c
index af5f30c7275..d98b312ecc9 100644
--- a/src/json.c
+++ b/src/json.c
@@ -364,7 +364,7 @@ lisp_to_json_nonscalar_1 (Lisp_Object lisp,
for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object key = HASH_KEY (h, i);
- if (!BASE_EQ (key, Qunbound))
+ if (!hash_unused_entry_key_p (key))
{
CHECK_STRING (key);
Lisp_Object ekey = json_encode (key);
diff --git a/src/lisp.h b/src/lisp.h
index a34726adbcb..549b51d3f7f 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2462,7 +2462,7 @@ struct Lisp_Hash_Table
/* Vector of keys and values. The key of item I is found at index
2 * I, the value is found at index 2 * I + 1.
- If the key is equal to Qunbound, then this slot is unused.
+ If the key is HASH_UNUSED_ENTRY_KEY, then this slot is unused.
This is gc_marked specially if the table is weak. */
Lisp_Object key_and_value;
@@ -2478,6 +2478,16 @@ struct Lisp_Hash_Table
/* Sanity-check pseudovector layout. */
verify (offsetof (struct Lisp_Hash_Table, weak) == header_size);
+/* Key value that marks an unused hash table entry. */
+#define HASH_UNUSED_ENTRY_KEY Qunbound
+
+/* KEY is a key of an unused hash table entry. */
+INLINE bool
+hash_unused_entry_key_p (Lisp_Object key)
+{
+ return BASE_EQ (key, HASH_UNUSED_ENTRY_KEY);
+}
+
INLINE bool
HASH_TABLE_P (Lisp_Object a)
{
diff --git a/src/minibuf.c b/src/minibuf.c
index f4f9da9c3f9..22bb8fa1d75 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -1680,8 +1680,8 @@ or from one of the possible completions. */)
else /* if (type == hash_table) */
{
while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
- && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
- Qunbound))
+ && hash_unused_entry_key_p (HASH_KEY (XHASH_TABLE (collection),
+ idx)))
idx++;
if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
break;
@@ -1918,8 +1918,8 @@ with a space are ignored unless STRING itself starts with
a space. */)
else /* if (type == 3) */
{
while (idx < HASH_TABLE_SIZE (XHASH_TABLE (collection))
- && BASE_EQ (HASH_KEY (XHASH_TABLE (collection), idx),
- Qunbound))
+ && hash_unused_entry_key_p (HASH_KEY (XHASH_TABLE (collection),
+ idx)))
idx++;
if (idx >= HASH_TABLE_SIZE (XHASH_TABLE (collection)))
break;
@@ -2117,7 +2117,7 @@ the values STRING, PREDICATE and `lambda'. */)
for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
tem = HASH_KEY (h, i);
- if (BASE_EQ (tem, Qunbound)) continue;
+ if (hash_unused_entry_key_p (tem)) continue;
Lisp_Object strkey = (SYMBOLP (tem) ? Fsymbol_name (tem) : tem);
if (!STRINGP (strkey)) continue;
if (BASE_EQ (Fcompare_strings (string, Qnil, Qnil,
diff --git a/src/print.c b/src/print.c
index 26ed52b4653..e22f3b6778c 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1290,7 +1290,7 @@ print (Lisp_Object obj, Lisp_Object printcharfun, bool
escapeflag)
for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
{
Lisp_Object key = HASH_KEY (h, i);
- if (!BASE_EQ (key, Qunbound)
+ if (!hash_unused_entry_key_p (key)
&& EQ (HASH_VALUE (h, i), Qt))
Fremhash (key, Vprint_number_table);
}
@@ -2770,7 +2770,7 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun,
bool escapeflag)
{
Lisp_Object key;
ptrdiff_t idx = e->u.hash.idx;
- while (BASE_EQ ((key = HASH_KEY (h, idx)), Qunbound))
+ while (hash_unused_entry_key_p ((key = HASH_KEY (h, idx))))
idx++;
e->u.hash.idx = idx;
obj = key;
- 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 <=
- master 462b3e6ae4e 06/11: Refactor: extract hash and index computations to functions, Mattias Engdegård, 2024/01/12
- master 4b7985db11c 10/11: ; * src/fns.c (Fmake_hash_table): ensure `test` is a bare symbol, Mattias Engdegård, 2024/01/12