From 49fd4d120deb0b878ad262aea7d849c7275bc12c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sat, 4 Nov 2023 15:16:38 +0100 Subject: [PATCH] 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 17ed711a318..636b4972c84 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -7242,7 +7242,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 74fdf29417e..a1659884b5e 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 d9b828b0328..f863df6bca0 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -2560,9 +2560,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. */ -- 2.39.2