optimisation
authorJoey Hess <joeyh@joeyh.name>
Wed, 10 Feb 2021 20:38:33 +0000 (16:38 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 10 Feb 2021 20:39:41 +0000 (16:39 -0400)
IORef rather than MVar sped up benchmark mentioned in last commit to
13.0s.

This makes me wonder if changing the interface to not need the IORef
either would improve speed further.

Backend/Hash.hs

index 0e723dea5d7a92103eac9783e01c6c6a56e46c09..53ba19c599493fd2ff5350faed1e1ca563cb8bc1 100644 (file)
@@ -28,7 +28,7 @@ import qualified Data.ByteString.Char8 as S8
 import qualified Data.ByteString.Lazy as L
 import Control.DeepSeq
 import Control.Exception (evaluate)
-import Control.Concurrent.MVar
+import Data.IORef
 
 data Hash
        = MD5Hash
@@ -280,15 +280,11 @@ md5Hasher = mkHasher md5 md5_context
 
 mkIncrementalVerifier :: HashAlgorithm h => Context h -> Key -> IO IncrementalVerifier
 mkIncrementalVerifier ctx key = do
-       v <- newMVar ctx
+       v <- newIORef ctx
        return $ IncrementalVerifier
-               { updateIncremental = \b -> do
-                       ctx' <- takeMVar v
-                       let ctx'' = hashUpdate ctx' b
-                       evaluate $ rnf ctx''
-                       putMVar v ctx''
+               { updateIncremental = modifyIORef' v . flip hashUpdate
                , finalizeIncremental = do
-                       ctx' <- takeMVar v
+                       ctx' <- readIORef v
                        let digest = hashFinalize ctx'
                        return $ sameCheckSum key (show digest)
                }