From 1b2ae73c28ed275db051b8492e02384a42a4e36a Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 6 Nov 2017 02:27:29 -0500 Subject: More inlining on Ord/Hashable instances for Public/Secret keys. --- src/Crypto/Tox.hs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs index 88a7372f..665a38dd 100644 --- a/src/Crypto/Tox.hs +++ b/src/Crypto/Tox.hs @@ -89,14 +89,14 @@ import qualified Data.ByteString.Internal import Control.Concurrent.STM import Crypto.Error.Types (CryptoFailable (..), throwCryptoError) import Network.Socket (SockAddr) -import GHC.Exts (Word(..)) +import GHC.Exts (Word(..),inline) import GHC.Prim import Data.Word64Map (fitsInInt) import Data.MinMaxPSQ (MinMaxPSQ') import qualified Data.MinMaxPSQ as MM import Data.Time.Clock.POSIX import Data.Hashable -import System.IO.Unsafe (unsafePerformIO) +import System.IO.Unsafe (unsafeDupablePerformIO) -- | A 16-byte mac and an arbitrary-length encrypted stream. newtype Encrypted a = Encrypted ByteString @@ -259,21 +259,26 @@ computeSharedSecret sk recipient = k `seq` \nonce -> k = hsalsa20 shared zeros24 unsafeFirstWord64 :: ByteArrayAccess ba => ba -> Word64 -unsafeFirstWord64 ba = unsafePerformIO $ BA.withByteArray ba peek +unsafeFirstWord64 ba = unsafeDupablePerformIO $ BA.withByteArray ba peek +{-# INLINE unsafeFirstWord64 #-} instance Hashable PublicKey where hashWithSalt salt pk = hashWithSalt salt (unsafeFirstWord64 pk) + {-# INLINE hashWithSalt #-} instance Hashable SecretKey where hashWithSalt salt sk = hashWithSalt salt (unsafeFirstWord64 sk) + {-# INLINE hashWithSalt #-} instance Ord PublicKey where compare = unsafeCompare32Bytes + {-# INLINE compare #-} instance Ord SecretKey where compare = unsafeCompare32Bytes + {-# INLINE compare #-} unsafeCompare32Bytes :: (ByteArrayAccess ba, ByteArrayAccess bb) => ba -> bb -> Ordering unsafeCompare32Bytes ba bb = - unsafePerformIO $ BA.withByteArray ba + unsafeDupablePerformIO $ BA.withByteArray ba $ \pa -> BA.withByteArray bb $ \pb -> unsafeCompare32Bytes' 3 pa pb @@ -281,13 +286,13 @@ unsafeCompare32Bytes' :: Int -> Ptr Word64 -> Ptr Word64 -> IO Ordering unsafeCompare32Bytes' !n !pa !pb = do a <- peek pa b <- peek pb - case compare a b of - EQ -> if n == 0 - then return EQ - else unsafeCompare32Bytes' (n - 1) - (pa `plusPtr` 8) - (pb `plusPtr` 8) - result -> return result + if n == 0 + then return $! inline compare a b + else case inline compare a b of + EQ -> unsafeCompare32Bytes' (n - 1) + (pa `plusPtr` 8) + (pb `plusPtr` 8) + neq -> return neq -- cgit v1.2.3