[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/hash-table-perf fdc390f8dc0 10/35: Abstract predicate and consta
From: |
Mattias Engdegård |
Subject: |
scratch/hash-table-perf fdc390f8dc0 10/35: Abstract predicate and constant for unused hash keys |
Date: |
Thu, 4 Jan 2024 10:56:41 -0500 (EST) |
branch: scratch/hash-table-perf
commit fdc390f8dc040f7e604455f30e3dbf83ea8e7c54
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 b81a80b00f8..d2a44c81b83 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_VALUE (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_VALUE (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 142259acadf..f9cf17770dc 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 86360a0f225..ac3d244d395 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 42ff7930621..2ba29f42b75 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 f7979fb446e..de9bac41e52 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 863a0fe4a3e..55decdcf5da 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 58adde1bf66..a3a534f5738 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 96c4d0a5d1e..5d94d3fd4de 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;
- branch scratch/hash-table-perf created (now 681a2877cc2), Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 594152bf667 01/35: Add internal hash-table debug functions, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 9f94796b657 05/35: ; * src/fns.c (collect_interval): Move misplaced function., Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 31950946290 04/35: Refactor: less egregious layering violation in composite.h, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf e2a6ce36d83 03/35: Decouple profiler from Lisp hash table internals, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 9141966be51 09/35: ; * src/alloc.c (purecopy_hash_table): Simplify, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf fdc390f8dc0 10/35: Abstract predicate and constant for unused hash keys,
Mattias Engdegård <=
- scratch/hash-table-perf c4df6041de8 12/35: * src/print.c (print_object): Don't print hash table test if `eql`., Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf f3e985a16ba 14/35: Don't print or read the hash table size parameter, Mattias Engdegård, 2024/01/04
- scratch/hash-table-perf 1ebd00f6d0a 21/35: Retype hash interfaces to use EMACS_UINT instead of Lisp fixnum, Mattias Engdegård, 2024/01/04
- 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