diff --git a/src/Development/Shake/Internal/Core/Build.hs b/src/Development/Shake/Internal/Core/Build.hs index dbbadcc8..f9e0a871 100644 --- a/src/Development/Shake/Internal/Core/Build.hs +++ b/src/Development/Shake/Internal/Core/Build.hs @@ -38,6 +38,12 @@ import Data.Either.Extra import System.Time.Extra +-- All of General.Wait is used at Locked here; its functions are INLINABLE. +{-# SPECIALISE runWait :: Wait Locked a -> Locked (Wait Locked a) #-} +{-# SPECIALISE fromLater :: Wait Locked a -> (a -> Locked ()) -> Locked () #-} +{-# SPECIALISE firstJustWaitUnordered :: (a -> Wait Locked (Maybe b)) -> [a] -> Wait Locked (Maybe b) #-} + + --------------------------------------------------------------------- -- LOW-LEVEL OPERATIONS ON THE DATABASE diff --git a/src/Development/Shake/Internal/Core/Database.hs b/src/Development/Shake/Internal/Core/Database.hs index 500c67e2..dba7f4da 100644 --- a/src/Development/Shake/Internal/Core/Database.hs +++ b/src/Development/Shake/Internal/Core/Database.hs @@ -59,6 +59,7 @@ createDatabase status journal vDefault = do -- SAFE READ-ONLY getValueFromKey :: (Eq k, Hashable k) => DatabasePoly k v -> k -> IO (Maybe v) +{-# INLINABLE getValueFromKey #-} getValueFromKey Database{..} k = do is <- readIORef intern case Intern.lookup k is of @@ -76,6 +77,7 @@ getKeyValuesFromId :: DatabasePoly k v -> IO (Map.HashMap Id (k, v)) getKeyValuesFromId Database{..} = Ids.toMap status getIdFromKey :: (Eq k, Hashable k) => DatabasePoly k v -> IO (k -> Maybe Id) +{-# INLINABLE getIdFromKey #-} getIdFromKey Database{..} = do is <- readIORef intern pure $ flip Intern.lookup is @@ -86,6 +88,7 @@ getIdFromKey Database{..} = do -- | Ensure that a Key has a given Id, creating an Id if there is not one already mkId :: (Eq k, Hashable k) => DatabasePoly k v -> k -> Locked Id +{-# INLINABLE mkId #-} mkId Database{..} k = liftIO $ do is <- readIORef intern case Intern.lookup k is of diff --git a/src/Development/Shake/Internal/Core/Types.hs b/src/Development/Shake/Internal/Core/Types.hs index 13712578..0b298e8b 100755 --- a/src/Development/Shake/Internal/Core/Types.hs +++ b/src/Development/Shake/Internal/Core/Types.hs @@ -439,6 +439,12 @@ userRuleSize (Versioned _ x) = userRuleSize x type Database = DatabasePoly Key Status +-- The only instantiation of the Core.Database lookups; every caller imports this module. +-- These rules are orphans (the functions live in Core.Database), which is harmless here. +{-# SPECIALISE getValueFromKey :: Database -> Key -> IO (Maybe Status) #-} +{-# SPECIALISE getIdFromKey :: Database -> IO (Key -> Maybe Id) #-} +{-# SPECIALISE mkId :: Database -> Key -> Locked Id #-} + -- global constants of Action data Global = Global {globalBuild :: [String] -> [Key] -> Action [Value] diff --git a/src/General/Binary.hs b/src/General/Binary.hs index 11e8efb7..9657dd6c 100644 --- a/src/General/Binary.hs +++ b/src/General/Binary.hs @@ -44,19 +44,23 @@ binaryOpMap mp = BinaryOp binarySplit :: forall a . Storable a => BS.ByteString -> (a, BS.ByteString) +{-# INLINABLE binarySplit #-} binarySplit bs | BS.length bs < sizeOf (undefined :: a) = error "Reading from ByteString, insufficient left" | otherwise = unsafeBinarySplit bs binarySplit2 :: forall a b . (Storable a, Storable b) => BS.ByteString -> (a, b, BS.ByteString) +{-# INLINABLE binarySplit2 #-} binarySplit2 bs | BS.length bs < sizeOf (undefined :: a) + sizeOf (undefined :: b) = error "Reading from ByteString, insufficient left" | (a,bs) <- unsafeBinarySplit bs, (b,bs) <- unsafeBinarySplit bs = (a,b,bs) binarySplit3 :: forall a b c . (Storable a, Storable b, Storable c) => BS.ByteString -> (a, b, c, BS.ByteString) +{-# INLINABLE binarySplit3 #-} binarySplit3 bs | BS.length bs < sizeOf (undefined :: a) + sizeOf (undefined :: b) + sizeOf (undefined :: c) = error "Reading from ByteString, insufficient left" | (a,bs) <- unsafeBinarySplit bs, (b,bs) <- unsafeBinarySplit bs, (c,bs) <- unsafeBinarySplit bs = (a,b,c,bs) unsafeBinarySplit :: Storable a => BS.ByteString -> (a, BS.ByteString) +{-# INLINABLE unsafeBinarySplit #-} unsafeBinarySplit bs = (v, BS.unsafeDrop (sizeOf v) bs) where v = unsafePerformIO $ BS.unsafeUseAsCString bs $ \ptr -> peek (castPtr ptr) @@ -173,20 +177,24 @@ instance BinaryEx Float where putExStorable :: forall a . Storable a => a -> Builder +{-# INLINABLE putExStorable #-} putExStorable x = Builder (sizeOf x) $ \p i -> pokeByteOff p i x getExStorable :: forall a . Storable a => BS.ByteString -> a +{-# INLINABLE getExStorable #-} getExStorable = \bs -> unsafePerformIO $ BS.unsafeUseAsCStringLen bs $ \(p, size) -> if size /= n then error "size mismatch" else peek (castPtr p) where n = sizeOf (undefined :: a) putExStorableList :: forall a . Storable a => [a] -> Builder +{-# INLINABLE putExStorableList #-} putExStorableList xs = Builder (n * length xs) $ \ptr i -> for2M_ [i,i+n..] xs $ \i x -> pokeByteOff ptr i x where n = sizeOf (undefined :: a) getExStorableList :: forall a . Storable a => BS.ByteString -> [a] +{-# INLINABLE getExStorableList #-} getExStorableList = \bs -> unsafePerformIO $ BS.unsafeUseAsCStringLen bs $ \(p, size) -> let (d,m) = size `divMod` n in if m /= 0 then error "size mismatch" else forM [0..d-1] $ \i -> peekElemOff (castPtr p) i diff --git a/src/General/Fence.hs b/src/General/Fence.hs index 025bf4cd..975e7b8a 100644 --- a/src/General/Fence.hs +++ b/src/General/Fence.hs @@ -22,14 +22,17 @@ newtype Fence m a = Fence (IORef (Either (a -> m ()) a)) instance Show (Fence m a) where show _ = "Fence" newFence :: MonadIO m => IO (Fence m a) +{-# SPECIALISE newFence :: IO (Fence IO a) #-} newFence = Fence <$> newIORef (Left $ const $ pure ()) signalFence :: (Partial, MonadIO m) => Fence m a -> a -> m () +{-# SPECIALISE signalFence :: Partial => Fence IO a -> a -> IO () #-} signalFence (Fence ref) v = join $ liftIO $ atomicModifyIORef' ref $ \case Left queue -> (Right v, queue v) Right _ -> throwImpure $ errorInternal "signalFence called twice on one Fence" waitFence :: MonadIO m => Fence m a -> (a -> m ()) -> m () +{-# SPECIALISE waitFence :: Fence IO a -> (a -> IO ()) -> IO () #-} waitFence (Fence ref) call = join $ liftIO $ atomicModifyIORef' ref $ \case Left queue -> (Left (\a -> queue a >> call a), pure ()) Right v -> (Right v, call v) @@ -42,6 +45,7 @@ testFence (Fence x) = eitherToMaybe <$> readIORef x -- FENCE COMPOSITES exceptFence :: MonadIO m => [Fence m (Either e r)] -> m (Fence m (Either e [r])) +{-# SPECIALISE exceptFence :: [Fence IO (Either e r)] -> IO (Fence IO (Either e [r])) #-} exceptFence xs = do -- number of items still to complete, becomes negative after it has triggered todo <- liftIO $ newIORef $ length xs diff --git a/src/General/Wait.hs b/src/General/Wait.hs index ea99ff0d..4af673e8 100644 --- a/src/General/Wait.hs +++ b/src/General/Wait.hs @@ -1,5 +1,4 @@ {-# LANGUAGE BangPatterns #-} -{-# LANGUAGE DeriveFunctor #-} -- | A bit like 'Fence', but not thread safe and optimised for avoiding taking the fence module General.Wait( @@ -18,10 +17,12 @@ import Prelude runWait :: Monad m => Wait m a -> m (Wait m a) +{-# INLINABLE runWait #-} runWait (Lift x) = runWait =<< x runWait x = pure x fromLater :: Monad m => Wait m a -> (a -> m ()) -> m () +{-# INLINABLE fromLater #-} fromLater (Lift x) f = do x <- x; fromLater x f fromLater (Now x) f = f x fromLater (Later x) f = x f @@ -32,9 +33,15 @@ quickly = Lift . fmap Now data Wait m a = Now a | Lift (m (Wait m a)) | Later ((a -> m ()) -> m ()) - deriving Functor + +instance Functor m => Functor (Wait m) where + {-# INLINABLE fmap #-} + fmap f (Now x) = Now $ f x + fmap f (Lift x) = Lift $ fmap (fmap f) x + fmap f (Later x) = Later $ \c -> x $ \a -> c $ f a instance (Monad m, Applicative m) => Applicative (Wait m) where + {-# INLINABLE (<*>) #-} pure = Now Now x <*> y = x <$> y Lift x <*> y = Lift $ (<*> y) <$> x @@ -44,6 +51,7 @@ instance (Monad m, Applicative m) => Applicative (Wait m) where Later x <*> Later y = Later $ \c -> x $ \x -> y $ \y -> c $ x y instance (Monad m, Applicative m) => Monad (Wait m) where + {-# INLINABLE (>>=) #-} return = pure (>>) = (*>) Now x >>= f = f x @@ -55,12 +63,14 @@ instance (Monad m, Applicative m) => Monad (Wait m) where _ -> fromLater x c instance (MonadIO m, Applicative m) => MonadIO (Wait m) where + {-# INLINABLE liftIO #-} liftIO = Lift . liftIO . fmap Now instance MonadFail m => MonadFail (Wait m) where fail = Lift . Control.Monad.Fail.fail firstJustWaitUnordered :: MonadIO m => (a -> Wait m (Maybe b)) -> [a] -> Wait m (Maybe b) +{-# INLINABLE firstJustWaitUnordered #-} firstJustWaitUnordered f = go 0 [] . map f where -- keep a list of those things we might visit later, and ask for each we see in turn @@ -88,6 +98,7 @@ firstJustWaitUnordered f = go 0 [] . map f firstLeftWaitUnordered :: MonadIO m => (a -> Wait m (Either e b)) -> [a] -> Wait m (Either e [b]) +{-# INLINABLE firstLeftWaitUnordered #-} firstLeftWaitUnordered f xs = do let n = length xs mut <- liftIO $ newArray n undefined