emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/hash-table-perf 310f6584ccb 18/37: Allow zero hash table size


From: Mattias Engdegård
Subject: scratch/hash-table-perf 310f6584ccb 18/37: Allow zero hash table size
Date: Sun, 7 Jan 2024 12:41:14 -0500 (EST)

branch: scratch/hash-table-perf
commit 310f6584ccb37b6d1aa8f2fc9a06f41157cf5160
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    Allow zero hash table size
    
    This avoids any extra allocation for such vectors, including empty
    tables read by the Lisp reader, and provides extra safety essentially
    for free.
    
    * src/fns.c (make_hash_table): Allow tables to be 0-sized.  The index
    will always have at least one entry, to avoid extra look-up costs.
    * src/alloc.c (process_mark_stack): Don't mark pure objects,
    because empty vectors are pure.
---
 src/alloc.c | 3 ++-
 src/fns.c   | 7 +++----
 src/lisp.h  | 4 +---
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index be20dc6e5e7..0107e8dd925 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -7272,7 +7272,8 @@ process_mark_stack (ptrdiff_t base_sp)
                      eassert (h->next_weak == NULL);
                      h->next_weak = weak_hash_tables;
                      weak_hash_tables = h;
-                     set_vector_marked (XVECTOR (h->key_and_value));
+                     if (!PURE_P (h->key_and_value))
+                       set_vector_marked (XVECTOR (h->key_and_value));
                    }
                  break;
                }
diff --git a/src/fns.c b/src/fns.c
index db2b5fdb5cc..be7def86c62 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4555,9 +4555,6 @@ make_hash_table (struct hash_table_test test, EMACS_INT 
size,
   eassert (SYMBOLP (test.name));
   eassert (0 <= size && size <= MOST_POSITIVE_FIXNUM);
 
-  if (size == 0)
-    size = 1;
-
   /* Allocate a table and initialize it.  */
   h = allocate_hash_table ();
 
@@ -4576,7 +4573,9 @@ make_hash_table (struct hash_table_test test, EMACS_INT 
size,
   /* Set up the free list.  */
   for (i = 0; i < size - 1; ++i)
     set_hash_next_slot (h, i, i + 1);
-  h->next_free = 0;
+  if (size > 0)
+    set_hash_next_slot (h, size - 1, -1);
+  h->next_free = size > 0 ? 0 : -1;
 
   XSET_HASH_TABLE (table, h);
   eassert (HASH_TABLE_P (table));
diff --git a/src/lisp.h b/src/lisp.h
index 1f3220c0179..215e72ca2c9 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2538,9 +2538,7 @@ HASH_HASH (const struct Lisp_Hash_Table *h, ptrdiff_t idx)
 INLINE ptrdiff_t
 HASH_TABLE_SIZE (const struct Lisp_Hash_Table *h)
 {
-  ptrdiff_t size = ASIZE (h->next);
-  eassume (0 < size);
-  return size;
+  return ASIZE (h->next);
 }
 
 /* Compute hash value for KEY in hash table H.  */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]