emacs-devel
[Top][All Lists]
Advanced

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

Re: Redisplay slower in Emacs 28 than Emacs 27


From: Stefan Monnier
Subject: Re: Redisplay slower in Emacs 28 than Emacs 27
Date: Tue, 08 Dec 2020 17:46:51 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>> :data image, current sxhash, Fequal: 6.4s.
>> :data image, NOOP sxhash, Fequal: 3.4s.
>
> Maybe it's time we speed up `hash_string`.
> There's a lot of room for improvement there, AFAICT, e.g.:
> - work on `EMACS_UINT` chunks instead of `char` chunks.
> - Bound the amount of work (e.g. do a max of, say, 10 chunks, equally
>   spaced throughout the string).

E.g.


        Stefan


diff --git a/src/fns.c b/src/fns.c
index e4c9acc316..23d24ef4db 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4525,18 +4526,40 @@ #define SXHASH_MAX_LEN   7
 EMACS_UINT
 hash_string (char const *ptr, ptrdiff_t len)
 {
-  char const *p = ptr;
-  char const *end = p + len;
-  unsigned char c;
-  EMACS_UINT hash = 0;
-
-  while (p != end)
+  if (len < 16)
     {
-      c = *p++;
-      hash = sxhash_combine (hash, c);
+      char const *p = ptr;
+      char const *end = p + len;
+      EMACS_UINT hash = len;
+
+      while (p < end)
+        {
+          unsigned char c = *p++;
+          hash = sxhash_combine (hash, c);
+        }
+
+      return hash;
     }
+  else
+    {
+      EMACS_UINT const *p   = (EMACS_UINT const *) ptr;
+      EMACS_UINT const *end = (EMACS_UINT const *) (ptr + len);
+      EMACS_UINT hash = len;
+      /* At most 8 steps.  We could reuse SXHASH_MAX_LEN, of course,
+       * but dividing by 8 is cheaper.  */
+      ptrdiff_t step = max (1, (end - p) >> 3);
+
+      /* Beware: `end` might be unaligned, so `p < end` is not always the same
+       * as `p <= end - 1`.  */
+      while (p <= end - 1)
+        {
+          EMACS_UINT c = *p;
+          p += step;
+          hash = sxhash_combine (hash, c);
+        }
 
-  return hash;
+      return hash;
+    }
 }
 
 /* Return a hash for string PTR which has length LEN.  The hash




reply via email to

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