From 0e2920570720ce5bd270062a0a5db22251ffc4e7 Mon Sep 17 00:00:00 2001 From: mangoiv Date: Sun, 15 Sep 2024 10:56:28 +0200 Subject: [PATCH 1/5] [fix] ensure compatibility with Semigroup/Monoid change --- src/Data/Hash/SL2.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Data/Hash/SL2.hs b/src/Data/Hash/SL2.hs index 62bd9ac..9a43ca0 100644 --- a/src/Data/Hash/SL2.hs +++ b/src/Data/Hash/SL2.hs @@ -65,25 +65,30 @@ instance Eq Hash where instance Ord Hash where compare a b = unsafePerformIO $ unsafeUseAsPtr2 a b Mutable.cmp +instance Semigroup Hash where + (<>) = concat + instance Monoid Hash where mempty = unit - mappend = concat mconcat = concatAll -- | /O(n)/ Calculate the hash of the 'ByteString'. Alias for @('append' 'unit')@. hash :: ByteString -> Hash hash = append unit +{-# inline[1] hash #-} -- | /O(n)/ Append the hash of the 'ByteString' to the existing 'Hash'. -- A significantly faster equivalent of @((. 'hash') . 'concat')@. append :: Hash -> ByteString -> Hash append h s = fst $ unsafePerformIO $ Mutable.withCopy h $ Mutable.append s +{-# inline[1] append #-} {-# RULES "hash/concat" forall h s . concat h (hash s) = append h s #-} -- | /O(n)/ Prepend the hash of the 'ByteString' to the existing 'Hash'. -- A significantly faster equivalent of @('concat' . 'hash')@. prepend :: ByteString -> Hash -> Hash prepend s h = fst $ unsafePerformIO $ Mutable.withCopy h $ Mutable.prepend s +{-# inline[1] prepend #-} {-# RULES "concat/hash" forall s h . concat (hash s) h = prepend s h #-} -- | /O(n)/ Append the hash of every 'ByteString' to the existing 'Hash', from left to right. From 12700c053c55427adbc23e148b2bf288a7ed4109 Mon Sep 17 00:00:00 2001 From: mangoiv Date: Sun, 15 Sep 2024 10:56:56 +0200 Subject: [PATCH 2/5] [chore] add cabal build outputs to gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 3c6942e..e83e2d7 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,8 @@ *.x86_64 *.hex +# stack .stack-work + +# cabal +dist* From 6269de614f4c6e873bc6f62eeb8d819030e7987c Mon Sep 17 00:00:00 2001 From: mangoiv Date: Sun, 15 Sep 2024 10:57:11 +0200 Subject: [PATCH 3/5] [chore] apply some lints while going through the testsuite --- src/Data/Hash/SL2/Test.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Data/Hash/SL2/Test.hs b/src/Data/Hash/SL2/Test.hs index 0b9152c..20d4ebe 100644 --- a/src/Data/Hash/SL2/Test.hs +++ b/src/Data/Hash/SL2/Test.hs @@ -24,6 +24,7 @@ instance Arbitrary B.ByteString where instance Arbitrary Hash where arbitrary = fmap hash arbitrary +main :: IO () main = defaultMain tests tests :: TestTree @@ -41,7 +42,7 @@ tests = testGroup "Properties" , testGroup "compare" [ testProperty "eq" $ - \a -> compare (hash a) (hash a) == EQ + \a -> hash a == hash a , testProperty "gt" $ \a b c -> (a > b && b > c) ==> (a :: Hash) > c @@ -95,13 +96,13 @@ tests = testGroup "Properties" , testGroup "append" - [ testGroup "single string" $ + [ testGroup "single string" [ testProperty "equal to ((. hash) . concat)" $ \a b -> ((. hash) . concat) a b == a `append` b ] - , testGroup "multiple strings" $ + , testGroup "multiple strings" [ testProperty "equal to (foldl append)" $ \a (b :: [B.ByteString]) -> foldl append a b == a `foldAppend` b @@ -111,13 +112,13 @@ tests = testGroup "Properties" , testGroup "prepend" - [ testGroup "single string" $ + [ testGroup "single string" [ testProperty "equal to (concat . hash)" $ \a b -> (concat . hash) a b == a `prepend` b ] - , testGroup "multiple strings" $ + , testGroup "multiple strings" [ testProperty "equal to (flip (foldr prepend)" $ \(a :: [B.ByteString]) b -> foldr prepend b a == a `foldPrepend` b From cf2aa701060aaa52d095dc775fbf9197e69093f2 Mon Sep 17 00:00:00 2001 From: mangoiv Date: Sun, 15 Sep 2024 11:18:58 +0200 Subject: [PATCH 4/5] [fix] also propagate Semigroup change sthrough Chunk.hs --- src/Data/Hash/SL2/Chunk.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Data/Hash/SL2/Chunk.hs b/src/Data/Hash/SL2/Chunk.hs index 6faf0cf..a15d811 100644 --- a/src/Data/Hash/SL2/Chunk.hs +++ b/src/Data/Hash/SL2/Chunk.hs @@ -1,12 +1,11 @@ -module Data.Hash.SL2.Chunk where +module Data.Hash.SL2.Chunk (Chunk(..), fromByteString) where import Data.ByteString import Data.Hash.SL2 -import Data.Monoid data Chunk = Chunk - { getChunkHash :: Hash - , getChunkBytes :: ByteString + { getChunkHash :: {-# unpack #-} !Hash + , getChunkBytes :: {-# unpack #-} !ByteString } instance Eq Chunk where @@ -15,9 +14,11 @@ instance Eq Chunk where instance Ord Chunk where compare a b = compare (getChunkHash a) (getChunkHash b) +instance Semigroup Chunk where + a <> b = Chunk (getChunkHash a <> getChunkHash b) (getChunkBytes a <> getChunkBytes b) + instance Monoid Chunk where mempty = Chunk mempty mempty - mappend a b = Chunk (getChunkHash a <> getChunkHash b) (getChunkBytes a <> getChunkBytes b) mconcat as = Chunk (mconcat $ fmap getChunkHash as) (mconcat $ fmap getChunkBytes as) fromByteString :: ByteString -> Chunk From 051b8171fd4195dae55b564403e3045e19dbbc18 Mon Sep 17 00:00:00 2001 From: mangoiv Date: Wed, 18 Sep 2024 13:43:17 +0200 Subject: [PATCH 5/5] [feat] unsafe ffi calls --- src/Data/Hash/SL2/Internal.hs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Data/Hash/SL2/Internal.hs b/src/Data/Hash/SL2/Internal.hs index 6db0d16..b80d2d7 100644 --- a/src/Data/Hash/SL2/Internal.hs +++ b/src/Data/Hash/SL2/Internal.hs @@ -12,29 +12,29 @@ newtype Hash = H (ForeignPtr ()) hashSize = 64 :: Int hashLen = 86 :: Int -foreign import capi "sl2-inl.h sl2_valid" +foreign import capi unsafe "sl2-inl.h sl2_valid" valid :: Ptr Hash -> IO CInt -foreign import capi "sl2-inl.h sl2_eq" +foreign import capi unsafe "sl2-inl.h sl2_eq" eq :: Ptr Hash -> Ptr Hash -> IO CInt -foreign import capi "sl2-inl.h sl2_cmp" +foreign import capi unsafe "sl2-inl.h sl2_cmp" cmp :: Ptr Hash -> Ptr Hash -> IO CInt -foreign import capi "sl2-inl.h sl2_unit" +foreign import capi unsafe "sl2-inl.h sl2_unit" unit :: Ptr Hash -> IO () -foreign import capi "sl2-inl.h sl2_mul_buf_right" +foreign import capi unsafe "sl2-inl.h sl2_mul_buf_right" append :: Ptr Hash -> Ptr CChar -> CSize -> IO () -foreign import capi "sl2-inl.h sl2_mul_buf_left" +foreign import capi unsafe "sl2-inl.h sl2_mul_buf_left" prepend :: Ptr Hash -> Ptr CChar -> CSize -> IO () -foreign import capi "sl2-inl.h sl2_mul" +foreign import capi unsafe "sl2-inl.h sl2_mul" concat :: Ptr Hash -> Ptr Hash -> Ptr Hash -> IO () -foreign import capi "sl2-inl.h sl2_serialize" +foreign import capi unsafe "sl2-inl.h sl2_serialize" serialize :: Ptr Hash -> Ptr CChar -> IO () -foreign import capi "sl2-inl.h sl2_unserialize" +foreign import capi unsafe "sl2-inl.h sl2_unserialize" unserialize :: Ptr Hash -> Ptr CChar -> IO ()