on master help sign in
Optimize uuid V3 & V5 performance
3be19b1Herbert Valerio Riedel
uuid/Data/UUID/Named.hs+2−4
@@ -33,21 +33,19 @@ import Data.Binary.Get (runGet, getWord32be)
33import Data.Maybe
33import Data.Maybe
34import Data.Word (Word8)
34import Data.Word (Word8)
35
35
36−import qualified Data.ByteArray as BA
37import qualified Data.ByteString as B
36import qualified Data.ByteString as B
38import qualified Data.ByteString.Lazy as BL
37import qualified Data.ByteString.Lazy as BL
39
38
40-- |Generate a 'UUID' within the specified namespace out of the given
39-- |Generate a 'UUID' within the specified namespace out of the given
41-- object.
40-- object.
42−generateNamed :: BA.ByteArrayAccess bs
43− => (B.ByteString -> bs) -- ^Hash
41+generateNamed :: (B.ByteString -> B.ByteString) -- ^Hash
44 -> Word8 -- ^Version
42 -> Word8 -- ^Version
45 -> UUID -- ^Namespace
43 -> UUID -- ^Namespace
46 -> [Word8] -- ^Object
44 -> [Word8] -- ^Object
47 -> UUID
45 -> UUID
48generateNamed hash version namespace object =
46generateNamed hash version namespace object =
49 let chunk = B.pack $ toList namespace ++ object
47 let chunk = B.pack $ toList namespace ++ object
50− bytes = BL.fromStrict . BA.convert $ hash chunk
48+ bytes = BL.fromChunks . (:[]) $ hash chunk
51 w = getWord32be
49 w = getWord32be
52 unpackBytes = runGet $
50 unpackBytes = runGet $
53 buildFromWords version <$> w <*> w <*> w <*> w
51 buildFromWords version <$> w <*> w <*> w <*> w
uuid/Data/UUID/V3.hs+2−2
@@ -35,7 +35,7 @@ import Data.Word
35
35
36import Data.UUID.Types.Internal
36import Data.UUID.Types.Internal
37import qualified Data.UUID.Named as Shared
37import qualified Data.UUID.Named as Shared
38−import qualified Crypto.Hash as H
38+import qualified Crypto.Hash.MD5 as MD5
39
39
40
40
41-- |Generate a 'UUID' within the specified namespace out of the given
41-- |Generate a 'UUID' within the specified namespace out of the given
@@ -46,4 +46,4 @@ import qualified Crypto.Hash as H
46generateNamed :: UUID -- ^Namespace
46generateNamed :: UUID -- ^Namespace
47 -> [Word8] -- ^Object
47 -> [Word8] -- ^Object
48 -> UUID
48 -> UUID
49−generateNamed = Shared.generateNamed (H.hashWith H.MD5) 3
49+generateNamed = Shared.generateNamed MD5.hash 3
uuid/Data/UUID/V5.hs+2−2
@@ -30,7 +30,7 @@ import Data.Word
30
30
31import Data.UUID.Types.Internal
31import Data.UUID.Types.Internal
32import qualified Data.UUID.Named as Shared
32import qualified Data.UUID.Named as Shared
33−import qualified Crypto.Hash as H
33+import qualified Crypto.Hash.SHA1 as SHA1
34
34
35
35
36-- |Generate a 'UUID' within the specified namespace out of the given
36-- |Generate a 'UUID' within the specified namespace out of the given
@@ -41,4 +41,4 @@ import qualified Crypto.Hash as H
41generateNamed :: UUID -- ^Namespace
41generateNamed :: UUID -- ^Namespace
42 -> [Word8] -- ^Object
42 -> [Word8] -- ^Object
43 -> UUID
43 -> UUID
44−generateNamed = Shared.generateNamed (H.hashWith H.SHA1) 5
44+generateNamed = Shared.generateNamed SHA1.hash 5
uuid/uuid.cabal+2
@@ -32,6 +32,8 @@ Library
32 Build-Depends: base >= 3 && < 5,
32 Build-Depends: base >= 3 && < 5,
33 binary >= 0.4 && < 0.9,
33 binary >= 0.4 && < 0.9,
34 bytestring >= 0.10 && < 0.11,
34 bytestring >= 0.10 && < 0.11,
35+ cryptohash-sha1 >= 0.11.100 && < 0.12,
36+ cryptohash-md5 >= 0.11.100 && < 0.12,
35 cryptonite >= 0.1 && < 0.21,
37 cryptonite >= 0.1 && < 0.21,
36 memory >= 0.2 && < 0.14,
38 memory >= 0.2 && < 0.14,
37 network-info == 0.2.*,
39 network-info == 0.2.*,