From bc060b6d9962e9f9a8b0023b1fe9015a6e3b3873 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Sat, 18 May 2024 20:31:46 +0300 Subject: [PATCH] Explicit hash for strict bytestring, text and bytearray --- src/Data/Hashable/Class.hs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Data/Hashable/Class.hs b/src/Data/Hashable/Class.hs index 056a46d..717e527 100644 --- a/src/Data/Hashable/Class.hs +++ b/src/Data/Hashable/Class.hs @@ -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 @@ -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))) @@ -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))) @@ -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