Skip to content

Commit

Permalink
Explicit hash for strict bytestring, text and bytearray
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed May 19, 2024
1 parent 8d9b544 commit 03f2c0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/Data/Hashable/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ instance Hashable1 [] where
step (SP s l) x = SP (h s x) (l + 1)

instance Hashable B.ByteString where
hash bs = fromIntegral (xxh3_64bit_withSeed_bs bs 0)

hashWithSalt salt bs =
fromIntegral (xxh3_64bit_withSeed_bs bs (fromIntegral (hashWithSalt salt len)))
where
Expand All @@ -645,36 +647,45 @@ instance Hashable BL.ByteString where
next (acc + fromIntegral (B.length bs))

instance Hashable BSI.ShortByteString where
hash (BSI.SBS ba) = hash (ByteArray ba)
hashWithSalt salt (BSI.SBS ba) = hashWithSalt salt (ByteArray ba)

#if HAS_OS_STRING_filepath || HAS_OS_STRING_os_string
-- | @since 1.4.2.0
instance Hashable PosixString where
hash (PosixString s) = hash s
hashWithSalt salt (PosixString s) = hashWithSalt salt s

-- | @since 1.4.2.0
instance Hashable WindowsString where
hash (WindowsString s) = hash s
hashWithSalt salt (WindowsString s) = hashWithSalt salt s

-- | @since 1.4.2.0
instance Hashable OsString where
hash (OsString s) = hash s
hashWithSalt salt (OsString s) = hashWithSalt salt s
#endif

#if HAS_OS_STRING_filepath && HAS_OS_STRING_os_string
instance Hashable FP.PosixString where
hash (FP.PosixString s) = hash s
hashWithSalt salt (FP.PosixString s) = hashWithSalt salt s

instance Hashable FP.WindowsString where
hash (FP.WindowsString s) = hash s
hashWithSalt salt (FP.WindowsString s) = hashWithSalt salt s

instance Hashable FP.OsString where
hash (FP.OsString s) = hash s
hashWithSalt salt (FP.OsString s) = hashWithSalt salt s
#endif

#if MIN_VERSION_text(2,0,0)

instance Hashable T.Text where
hash (T.Text (TA.ByteArray arr) off len) =
fromIntegral (xxh3_64bit_withSeed_ba (ByteArray arr) off len 0)
hashWithSalt salt (T.Text (TA.ByteArray arr) off len) =
fromIntegral (xxh3_64bit_withSeed_ba (ByteArray arr) off len (fromIntegral (hashWithSalt salt len)))

Expand All @@ -694,6 +705,8 @@ instance Hashable TL.Text where
#else

instance Hashable T.Text where
hash (T.Text arr off len) =
fromIntegral (xxh3_64bit_withSeed_ba (ByteArray (TA.aBA arr)) (unsafeShiftL off 1) (unsafeShiftL len 1) 0)
hashWithSalt salt (T.Text arr off len) =
fromIntegral (xxh3_64bit_withSeed_ba (ByteArray (TA.aBA arr)) (unsafeShiftL off 1) (unsafeShiftL len 1) (fromIntegral (hashWithSalt salt len)))

Expand Down Expand Up @@ -909,10 +922,15 @@ instance (Hashable1 f, Hashable1 g, Hashable a) => Hashable (FS.Sum f g a) where
-- @since 1.4.2.0
--
instance Hashable AB.ByteArray where
hash ba@(AB.ByteArray ba') =
fromIntegral (xxh3_64bit_withSeed_ba ba 0 len 0)
where
!len = I# (sizeofByteArray# ba')

hashWithSalt salt ba@(AB.ByteArray ba') =
fromIntegral (xxh3_64bit_withSeed_ba ba 0 len (fromIntegral (hashWithSalt salt len)))
where
len = I# (sizeofByteArray# ba')
!len = I# (sizeofByteArray# ba')

-------------------------------------------------------------------------------
-- Hashed
Expand Down
4 changes: 2 additions & 2 deletions tests/Regress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ regressions = [] ++
, testCase "64 bit Text" $ do
let expected =
#if MIN_VERSION_text(2,0,0)
899316382784853347
-3150353794653054837
#else
-1883629154121491777
660667291861873677
#endif
hash ("hello world" :: Text) @?= expected
#endif
Expand Down

0 comments on commit 03f2c0f

Please sign in to comment.