emacs-diffs
[Top][All Lists]
Advanced

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

scratch/hash-table-perf fc68176120f 24/35: Use hash_hash_t for storing h


From: Mattias Engdegård
Subject: scratch/hash-table-perf fc68176120f 24/35: Use hash_hash_t for storing hash values
Date: Thu, 4 Jan 2024 10:56:43 -0500 (EST)

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

    Use hash_hash_t for storing hash values
    
    Now hash_hash_t is a typedef for EMACS_UINT so there is no actual code
    change, but this allows us to decouple the hash value width from the
    Lisp word size.
    
    * src/lisp.h (hash_hash_t): New typedef for EMACS_UINT.
    (struct hash_table_test, struct Lisp_Hash_Table):
    Use it for all hash values: hashfn and the hash vector.
    All uses adapted.
    (HASH_HASH, hash_from_key):
    * src/fns.c (set_hash_index_slot, hash_index_index)
    (hash_lookup_with_hash, hash_lookup_get_hash, hash_put):
    Retype hash arguments and return values to hash_hash_t.
    All callers adapted.
---
 src/category.c     |  2 +-
 src/charset.c      |  2 +-
 src/composite.c    |  2 +-
 src/emacs-module.c |  2 +-
 src/fns.c          | 32 ++++++++++++++++----------------
 src/image.c        |  2 +-
 src/json.c         |  2 +-
 src/lisp.h         | 26 +++++++++++++++-----------
 src/lread.c        |  8 ++++----
 src/macfont.m      |  2 +-
 10 files changed, 42 insertions(+), 38 deletions(-)

diff --git a/src/category.c b/src/category.c
index 3230844fce6..be94b6b1417 100644
--- a/src/category.c
+++ b/src/category.c
@@ -53,7 +53,7 @@ hash_get_category_set (Lisp_Object table, Lisp_Object 
category_set)
       (table, 1,
        make_hash_table (hashtest_equal, DEFAULT_HASH_SIZE, Weak_None, false));
   struct Lisp_Hash_Table *h = XHASH_TABLE (XCHAR_TABLE (table)->extras[1]);
-  EMACS_UINT hash;
+  hash_hash_t hash;
   ptrdiff_t i = hash_lookup_get_hash (h, category_set, &hash);
   if (i >= 0)
     return HASH_KEY (h, i);
diff --git a/src/charset.c b/src/charset.c
index d68c30353c1..2033a944146 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -1107,7 +1107,7 @@ usage: (define-charset-internal ...)  */)
   CHECK_LIST (args[charset_arg_plist]);
   ASET (attrs, charset_plist, args[charset_arg_plist]);
 
-  EMACS_UINT hash_code;
+  hash_hash_t hash_code;
   charset.hash_index = hash_lookup_get_hash (hash_table, 
args[charset_arg_name],
                                             &hash_code);
   if (charset.hash_index >= 0)
diff --git a/src/composite.c b/src/composite.c
index fb1d607e3d6..be7c03377ba 100644
--- a/src/composite.c
+++ b/src/composite.c
@@ -240,7 +240,7 @@ get_composition_id (ptrdiff_t charpos, ptrdiff_t bytepos, 
ptrdiff_t nchars,
   else
     goto invalid_composition;
 
-  EMACS_UINT hash_code;
+  hash_hash_t hash_code;
   hash_index = hash_lookup_get_hash (hash_table, key, &hash_code);
   if (hash_index >= 0)
     {
diff --git a/src/emacs-module.c b/src/emacs-module.c
index 17e7829b32d..59980e364b1 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -428,7 +428,7 @@ module_make_global_ref (emacs_env *env, emacs_value value)
   MODULE_FUNCTION_BEGIN (NULL);
   struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash);
   Lisp_Object new_obj = value_to_lisp (value);
-  EMACS_UINT hashcode;
+  hash_hash_t hashcode;
   ptrdiff_t i = hash_lookup_get_hash (h, new_obj, &hashcode);
 
   /* Note: This approach requires the garbage collector to never move
diff --git a/src/fns.c b/src/fns.c
index ccd2ade1215..b2a0d5b3be4 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -2732,7 +2732,7 @@ equal_no_quit (Lisp_Object o1, Lisp_Object o2)
 }
 
 static ptrdiff_t hash_lookup_with_hash (struct Lisp_Hash_Table *h,
-                                       Lisp_Object key, EMACS_UINT hash);
+                                       Lisp_Object key, hash_hash_t hash);
 
 
 /* Return true if O1 and O2 are equal.  EQUAL_KIND specifies what kind
@@ -2763,7 +2763,7 @@ internal_equal (Lisp_Object o1, Lisp_Object o2, enum 
equal_kind equal_kind,
        case Lisp_Cons: case Lisp_Vectorlike:
          {
            struct Lisp_Hash_Table *h = XHASH_TABLE (ht);
-           EMACS_UINT hash = hash_from_key (h, o1);
+           hash_hash_t hash = hash_from_key (h, o1);
            ptrdiff_t i = hash_lookup_with_hash (h, o1, hash);
            if (i >= 0)
              { /* `o1' was seen already.  */
@@ -4283,7 +4283,7 @@ set_hash_next_slot (struct Lisp_Hash_Table *h, ptrdiff_t 
idx, ptrdiff_t val)
   h->next[idx] = val;
 }
 static void
-set_hash_hash_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, EMACS_UINT val)
+set_hash_hash_slot (struct Lisp_Hash_Table *h, ptrdiff_t idx, hash_hash_t val)
 {
   eassert (idx >= 0 && idx < h->table_size);
   h->hash[idx] = val;
@@ -4454,7 +4454,7 @@ cmpfn_user_defined (Lisp_Object key1, Lisp_Object key2,
 
 /* Ignore H and return a hash code for KEY which uses 'eq' to compare keys.  */
 
-static EMACS_UINT
+static hash_hash_t
 hashfn_eq (Lisp_Object key, struct Lisp_Hash_Table *h)
 {
   if (symbols_with_pos_enabled && SYMBOL_WITH_POS_P (key))
@@ -4465,7 +4465,7 @@ hashfn_eq (Lisp_Object key, struct Lisp_Hash_Table *h)
 /* Ignore H and return a hash code for KEY which uses 'equal' to compare keys.
    The hash code is at most INTMASK.  */
 
-static EMACS_UINT
+static hash_hash_t
 hashfn_equal (Lisp_Object key, struct Lisp_Hash_Table *h)
 {
   return sxhash (key);
@@ -4474,7 +4474,7 @@ hashfn_equal (Lisp_Object key, struct Lisp_Hash_Table *h)
 /* Ignore H and return a hash code for KEY which uses 'eql' to compare keys.
    The hash code is at most INTMASK.  */
 
-static EMACS_UINT
+static hash_hash_t
 hashfn_eql (Lisp_Object key, struct Lisp_Hash_Table *h)
 {
   return (FLOATP (key) || BIGNUMP (key)
@@ -4484,7 +4484,7 @@ hashfn_eql (Lisp_Object key, struct Lisp_Hash_Table *h)
 /* Given H, return a hash code for KEY which uses a user-defined
    function to compare keys.  */
 
-static EMACS_UINT
+static hash_hash_t
 hashfn_user_defined (Lisp_Object key, struct Lisp_Hash_Table *h)
 {
   Lisp_Object args[] = { h->test.user_hash_function, key };
@@ -4638,7 +4638,7 @@ 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, EMACS_UINT hash)
+hash_index_index (struct Lisp_Hash_Table *h, hash_hash_t hash)
 {
   eassert (h->index_size > 0);
   return hash % h->index_size;
@@ -4679,7 +4679,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
       for (ptrdiff_t i = 2 * old_size; i < 2 * new_size; i++)
         key_and_value[i] = HASH_UNUSED_ENTRY_KEY;
 
-      EMACS_UINT *hash = hash_table_alloc_bytes (new_size * sizeof *hash);
+      hash_hash_t *hash = hash_table_alloc_bytes (new_size * sizeof *hash);
       memcpy (hash, h->hash, old_size * sizeof *hash);
       for (ptrdiff_t i = old_size; i < new_size; i++)
        hash[i] = hash_unused;
@@ -4713,7 +4713,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
       for (ptrdiff_t i = 0; i < old_size; i++)
        if (HASH_HASH (h, i) != hash_unused)
          {
-           EMACS_UINT hash_code = HASH_HASH (h, i);
+           hash_hash_t 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);
@@ -4770,7 +4770,7 @@ hash_table_thaw (Lisp_Object hash_table)
   for (ptrdiff_t i = 0; i < size; i++)
     {
       Lisp_Object key = HASH_KEY (h, i);
-      EMACS_UINT hash_code = hash_from_key (h, key);
+      hash_hash_t 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));
@@ -4782,7 +4782,7 @@ hash_table_thaw (Lisp_Object hash_table)
    Return entry index or -1 if none.  */
 static ptrdiff_t
 hash_lookup_with_hash (struct Lisp_Hash_Table *h,
-                      Lisp_Object key, EMACS_UINT hash)
+                      Lisp_Object key, hash_hash_t hash)
 {
   ptrdiff_t start_of_bucket = hash_index_index (h, hash);
   for (ptrdiff_t i = HASH_INDEX (h, start_of_bucket);
@@ -4807,9 +4807,9 @@ hash_lookup (struct Lisp_Hash_Table *h, Lisp_Object key)
    Value is the index of the entry in H matching KEY, or -1 if not found.  */
 ptrdiff_t
 hash_lookup_get_hash (struct Lisp_Hash_Table *h, Lisp_Object key,
-                     EMACS_UINT *phash)
+                     hash_hash_t *phash)
 {
-  EMACS_UINT hash = hash_from_key (h, key);
+  hash_hash_t hash = hash_from_key (h, key);
   *phash = hash;
   return hash_lookup_with_hash (h, key, hash);
 }
@@ -4828,7 +4828,7 @@ check_mutable_hash_table (Lisp_Object obj, struct 
Lisp_Hash_Table *h)
 
 ptrdiff_t
 hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
-         EMACS_UINT hash)
+         hash_hash_t hash)
 {
   /* Increment count after resizing because resizing may fail.  */
   maybe_resize_hash_table (h);
@@ -4858,7 +4858,7 @@ 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)
 {
-  EMACS_UINT hashval = hash_from_key (h, key);
+  hash_hash_t hashval = hash_from_key (h, key);
   ptrdiff_t start_of_bucket = hash_index_index (h, hashval);
   ptrdiff_t prev = -1;
 
diff --git a/src/image.c b/src/image.c
index cd5f1c4a6b7..5a1dd92a87a 100644
--- a/src/image.c
+++ b/src/image.c
@@ -6081,7 +6081,7 @@ xpm_put_color_table_h (Lisp_Object color_table,
   struct Lisp_Hash_Table *table = XHASH_TABLE (color_table);
   Lisp_Object chars = make_unibyte_string (chars_start, chars_len);
 
-  EMACS_UINT hash_code;
+  hash_hash_t hash_code;
   hash_lookup_get_hash (table, chars, &hash_code);
   hash_put (table, chars, color, hash_code);
 }
diff --git a/src/json.c b/src/json.c
index 9a6d25335bb..66c3c63bfc1 100644
--- a/src/json.c
+++ b/src/json.c
@@ -880,7 +880,7 @@ json_to_lisp (json_t *json, const struct json_configuration 
*conf)
               json_object_foreach (json, key_str, value)
                 {
                  Lisp_Object key = build_string_from_utf8 (key_str);
-                 EMACS_UINT hash;
+                 hash_hash_t hash;
                   ptrdiff_t i = hash_lookup_get_hash (h, key, &hash);
                   /* Keys in JSON objects are unique, so the key can't
                      be present yet.  */
diff --git a/src/lisp.h b/src/lisp.h
index 6ea7ded0fd5..92e36b71392 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2385,6 +2385,10 @@ INLINE int
 
 struct Lisp_Hash_Table;
 
+/* The type of a hash value stored in the table.
+   It's unsigned and a subtype of EMACS_UINT.  */
+typedef EMACS_UINT hash_hash_t;
+
 typedef enum {
   Test_eql,
   Test_eq,
@@ -2406,7 +2410,7 @@ struct hash_table_test
   Lisp_Object (*cmpfn) (Lisp_Object, Lisp_Object, struct Lisp_Hash_Table *);
 
   /* C function to compute hash code.  */
-  EMACS_UINT (*hashfn) (Lisp_Object, struct Lisp_Hash_Table *);
+  hash_hash_t (*hashfn) (Lisp_Object, struct Lisp_Hash_Table *);
 };
 
 typedef enum {
@@ -2422,8 +2426,8 @@ typedef enum {
 } hash_table_weakness_t;
 
 /* An value that marks an unused hash entry.
-   Any EMACS_UINT value that is not a valid fixnum will do here.  */
-enum { hash_unused = (EMACS_UINT)MOST_POSITIVE_FIXNUM + 1 };
+   Any hash_hash_t value that is not a valid fixnum will do here.  */
+enum { hash_unused = (hash_hash_t)MOST_POSITIVE_FIXNUM + 1 };
 verify (FIXNUM_OVERFLOW_P (hash_unused));
 
 /* The type of a hash table index, both for table indices and index
@@ -2442,13 +2446,13 @@ struct Lisp_Hash_Table
 
   /* Bucket vector.  An entry of -1 indicates no item is present,
      and a nonnegative entry is the index of the first item in
-     a collision chain.  This vector's size can be larger than the
-     hash table size to reduce collisions.  */
+     a collision chain.
+     This vector is index_size entries long.  */
   hash_idx_t *index;
 
   /* Vector of hash codes.  The value hash_unused marks an unused table entry.
      This vector is table_size entries long.  */
-  EMACS_UINT *hash;
+  hash_hash_t *hash;
 
   /* Vector used to chain entries.  If entry I is free, next[I] is the
      entry number of the next free item.  If entry I is non-free,
@@ -2537,7 +2541,7 @@ HASH_VALUE (const struct Lisp_Hash_Table *h, ptrdiff_t 
idx)
 }
 
 /* Value is the hash code computed for entry IDX in hash table H.  */
-INLINE EMACS_UINT
+INLINE hash_hash_t
 HASH_HASH (const struct Lisp_Hash_Table *h, ptrdiff_t idx)
 {
   eassert (idx >= 0 && idx < h->table_size);
@@ -2551,8 +2555,8 @@ HASH_TABLE_SIZE (const struct Lisp_Hash_Table *h)
   return h->table_size;
 }
 
-/* Compute hash value for KEY in hash table H.  */
-INLINE EMACS_UINT
+/* Hash value for KEY in hash table H.  */
+INLINE hash_hash_t
 hash_from_key (struct Lisp_Hash_Table *h, Lisp_Object key)
 {
   return h->test.hashfn (key, h);
@@ -4007,9 +4011,9 @@ Lisp_Object make_hash_table (struct hash_table_test, 
EMACS_INT,
 Lisp_Object hash_table_weakness_symbol (hash_table_weakness_t weak);
 ptrdiff_t hash_lookup (struct Lisp_Hash_Table *, Lisp_Object);
 ptrdiff_t hash_lookup_get_hash (struct Lisp_Hash_Table *h, Lisp_Object key,
-                               EMACS_UINT *phash);
+                               hash_hash_t *phash);
 ptrdiff_t hash_put (struct Lisp_Hash_Table *, Lisp_Object, Lisp_Object,
-                   EMACS_UINT);
+                   hash_hash_t);
 void hash_remove_from_table (struct Lisp_Hash_Table *, Lisp_Object);
 extern struct hash_table_test const hashtest_eq, hashtest_eql, hashtest_equal;
 extern void validate_subarray (Lisp_Object, Lisp_Object, Lisp_Object,
diff --git a/src/lread.c b/src/lread.c
index 039b700e169..6f5a237b714 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -4255,7 +4255,7 @@ read0 (Lisp_Object readcharfun, bool locate_syms)
                        struct Lisp_Hash_Table *h
                          = XHASH_TABLE (read_objects_map);
                        Lisp_Object number = make_fixnum (n);
-                       EMACS_UINT hash;
+                       hash_hash_t hash;
                        ptrdiff_t i = hash_lookup_get_hash (h, number, &hash);
                        if (i >= 0)
                          /* Not normal, but input could be malformed.  */
@@ -4571,7 +4571,7 @@ read0 (Lisp_Object readcharfun, bool locate_syms)
 
                struct Lisp_Hash_Table *h2
                  = XHASH_TABLE (read_objects_completed);
-               EMACS_UINT hash;
+               hash_hash_t hash;
                ptrdiff_t i = hash_lookup_get_hash (h2, placeholder, &hash);
                eassert (i < 0);
                hash_put (h2, placeholder, Qnil, hash);
@@ -4586,7 +4586,7 @@ read0 (Lisp_Object readcharfun, bool locate_syms)
                  {
                    struct Lisp_Hash_Table *h2
                      = XHASH_TABLE (read_objects_completed);
-                   EMACS_UINT hash;
+                   hash_hash_t hash;
                    ptrdiff_t i = hash_lookup_get_hash (h2, obj, &hash);
                    eassert (i < 0);
                    hash_put (h2, obj, Qnil, hash);
@@ -4598,7 +4598,7 @@ read0 (Lisp_Object readcharfun, bool locate_syms)
 
                /* ...and #n# will use the real value from now on.  */
                struct Lisp_Hash_Table *h = XHASH_TABLE (read_objects_map);
-               EMACS_UINT hash;
+               hash_hash_t hash;
                ptrdiff_t i = hash_lookup_get_hash (h, e->u.numbered.number,
                                                    &hash);
                eassert (i >= 0);
diff --git a/src/macfont.m b/src/macfont.m
index 32083a7b0db..43b57914700 100644
--- a/src/macfont.m
+++ b/src/macfont.m
@@ -1023,7 +1023,7 @@ macfont_set_family_cache (Lisp_Object symbol, CFStringRef 
string)
     macfont_family_cache = CALLN (Fmake_hash_table, QCtest, Qeq);
 
   h = XHASH_TABLE (macfont_family_cache);
-  EMACS_UINT hash;
+  hash_hash_t hash;
   i = hash_lookup_get_hash (h, symbol, &hash);
   value = string ? make_mint_ptr ((void *) CFRetain (string)) : Qnil;
   if (i >= 0)



reply via email to

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