[commit: ghc] wip/alexbiehl: Try out another hash for FastStrings as discussed in IRC (5748f76)

git at git.haskell.org git at git.haskell.org
Thu Mar 23 19:45:39 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/alexbiehl
Link       : http://ghc.haskell.org/trac/ghc/changeset/5748f7642e406ddf232fd49edf6366a3e4de8bf0/ghc

>---------------------------------------------------------------

commit 5748f7642e406ddf232fd49edf6366a3e4de8bf0
Author: alexbiehl <alex.biehl at gmail.com>
Date:   Thu Mar 23 20:33:20 2017 +0100

    Try out another hash for FastStrings as discussed in IRC


>---------------------------------------------------------------

5748f7642e406ddf232fd49edf6366a3e4de8bf0
 compiler/utils/FastString.hs | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/compiler/utils/FastString.hs b/compiler/utils/FastString.hs
index 8d1bbb5..b094670 100644
--- a/compiler/utils/FastString.hs
+++ b/compiler/utils/FastString.hs
@@ -102,6 +102,7 @@ import FastFunctions
 import Panic
 import Util
 
+import Data.Bits ((.&.))
 import Control.DeepSeq
 import Control.Monad
 import Data.ByteString (ByteString)
@@ -129,8 +130,8 @@ import GHC.Conc.Sync    (sharedCAF)
 
 import GHC.Base         ( unpackCString# )
 
-#define hASH_TBL_SIZE          4091
-#define hASH_TBL_SIZE_UNBOXED  4091#
+#define hASH_TBL_SIZE          4096
+#define hASH_TBL_SIZE_UNBOXED  4096#
 
 
 fastStringToByteString :: FastString -> ByteString
@@ -453,16 +454,16 @@ cmpStringPrefix ptr1 ptr2 len =
  do r <- memcmp ptr1 ptr2 len
     return (r == 0)
 
+hashStr :: Ptr Word8 -> Int -> Int
+hashStr (Ptr a#) (I# len#) = loop 0# a# .&. (hASH_TBL_SIZE - 1)
+  where
+    !end = plusAddr# a# len# 
 
-hashStr  :: Ptr Word8 -> Int -> Int
- -- use the Addr to produce a hash value between 0 & m (inclusive)
-hashStr (Ptr a#) (I# len#) = loop 0# 0#
-   where
-    loop h n | isTrue# (n ==# len#) = I# h
-             | otherwise  = loop h2 (n +# 1#)
-          where !c = ord# (indexCharOffAddr# a# n)
-                !h2 = (c +# (h *# 128#)) `remInt#`
-                      hASH_TBL_SIZE#
+    loop h op | isTrue# (eqAddr# op end) = I# h
+              | otherwise = loop h' (plusAddr# op 1#)
+            where 
+                !c  = ord# (indexCharOffAddr# op 0#)
+                !h' = h *# 31# +# c
 
 -- -----------------------------------------------------------------------------
 -- Operations



More information about the ghc-commits mailing list