From dc9376feebc3108634244ff458ae4cf64e4ffe4b Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 10 Feb 2021 16:38:33 -0400 Subject: [PATCH] optimisation 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 | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Backend/Hash.hs b/Backend/Hash.hs index 0e723dea5d..53ba19c599 100644 --- a/Backend/Hash.hs +++ b/Backend/Hash.hs @@ -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) }