Skip to content

Commit

Permalink
Remove local INLINEABLEs
Browse files Browse the repository at this point in the history
We annotate monad-polymorphic functions with INLINEABLE, so that
these functions can potentially be specialized, allowing optimizations.
Is there any evidence that we need these annotations for funtions that
are defined __within__ top-level INLINEABLE functions? I don't know!
The benchmarks do not appear to show any difference.
  • Loading branch information
tbidne committed Nov 5, 2024
1 parent b0f9422 commit f0abd42
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 40 deletions.
2 changes: 0 additions & 2 deletions src/Shrun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ shrun = displayRegions $ do
hFlush h
where
MkFileLogOpened h fileQueue = fileLogging ^. #file
{-# INLINEABLE runWithFileLogging #-}

runCommands :: (HasCallStack) => m ()
runCommands = do
Expand All @@ -145,7 +144,6 @@ shrun = displayRegions $ do

(totalTime, result) <- withTiming $ trySync actionsWithTimer
printFinalResult totalTime result
{-# INLINEABLE runCommands #-}
{-# INLINEABLE shrun #-}

runCommand ::
Expand Down
7 changes: 0 additions & 7 deletions src/Shrun/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ tryCommandLogging command = do
(ConsoleLogCmdOn, Nothing) -> \cmd ->
withRegion Linear $ \region -> do
let logFn = logConsole keyHide consoleLogQueue region consoleLogging
{-# INLINEABLE logFn #-}

logFn hello

Expand All @@ -179,7 +178,6 @@ tryCommandLogging command = do
(ConsoleLogCmdOff, Just fileLogging) -> \cmd -> do
let logFn :: Log -> m ()
logFn = logFile keyHide fileLogging
{-# INLINEABLE logFn #-}

logFn hello

Expand All @@ -191,12 +189,10 @@ tryCommandLogging command = do
let logFn log = do
logConsole keyHide consoleLogQueue region consoleLogging log
logFile keyHide fileLogging log
{-# INLINEABLE logFn #-}

logFn hello

tryCommandStream logFn cmd
{-# INLINEABLE cmdFn #-}

withTiming (cmdFn command) >>= \case
(rt, Nothing) -> do
Expand All @@ -216,12 +212,10 @@ tryCommandLogging command = do
logConsole keyHide consoleQueue region consoleLogging log = do
let formatted = formatConsoleLog keyHide consoleLogging log
writeTBQueueA consoleQueue (LogRegion (log ^. #mode) region formatted)
{-# INLINEABLE logConsole #-}

logFile keyHide fileLogging log = do
formatted <- formatFileLog keyHide fileLogging log
writeTBQueueA (fileLogging ^. #file % #queue) formatted
{-# INLINEABLE logFile #-}

hello =
MkLog
Expand Down Expand Up @@ -319,7 +313,6 @@ streamOutput logFn cmd p = do

sleepFn :: m ()
sleepFn = when (pollInterval /= 0) (microsleep pollInterval)
{-# INLINEABLE sleepFn #-}

blockSize :: Int
blockSize = commandLogging ^. (#readSize % #unReadSize % _MkBytes)
Expand Down
16 changes: 1 addition & 15 deletions src/Shrun/IO/Handle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ readHandleRaw blockSize handle = do
-- should be large enough that we are not likely to cut off a line
-- prematurely, but obviously this is best-effort.
Right <$> hGetNonBlocking handle blockSize
{-# INLINEABLE readHandle' #-}


nothingIfReady = do
-- NOTE: This somewhat torturous logic exists for a reason. We want to
Expand All @@ -204,7 +204,6 @@ readHandleRaw blockSize handle = do
if not isReadable
then pure $ Just "Handle is not readable"
else pure Nothing
{-# INLINEABLE nothingIfReady #-}
{-# INLINEABLE readHandleRaw #-}

-- NOTE: [EOF / blocking error] We would like to check hIsEOF (definitely
Expand Down Expand Up @@ -241,7 +240,6 @@ readAndUpdateRef (prevReadRef, bufferLength, bufferTimeout, bufferWriteTimeRef)
Just prevRead ->
maybeToReadHandleResult
<$> prepareSendIfExceedsThresholds (const (pure ())) prevRead
{-# INLINEABLE onNoData #-}

-- 2. Partial read: Send the data if it breaks the thresholds, prepending
-- prevRead if it exists.
Expand All @@ -255,7 +253,6 @@ readAndUpdateRef (prevReadRef, bufferLength, bufferTimeout, bufferWriteTimeRef)
let combinedRead = prevRead <> finalPartialRead
maybeToReadHandleResult
<$> prepareSendIfExceedsThresholds updateRef combinedRead
{-# INLINEABLE onPartialRead #-}

-- 3. Completed reads and partial read.
onCompletedAndPartialRead :: NonEmpty UnlinedText -> UnlinedText -> m ReadHandleResult
Expand All @@ -271,7 +268,6 @@ readAndUpdateRef (prevReadRef, bufferLength, bufferTimeout, bufferWriteTimeRef)
Nothing -> completedReads'
Just finalRead -> completedReads' <> ne finalRead
pure $ ReadSuccess totalRead
{-# INLINEABLE onCompletedAndPartialRead #-}

-- Turns this text into Just text iff the buffer thresholds are
-- exceeded.
Expand All @@ -294,14 +290,12 @@ readAndUpdateRef (prevReadRef, bufferLength, bufferTimeout, bufferWriteTimeRef)
else do
onNoSend readData
pure Nothing
{-# INLINEABLE prepareSendIfExceedsThresholds #-}

exceedsThreshold :: UnlinedText -> m Bool
exceedsThreshold t =
if bufferExceedsLength t
then pure True
else bufferExceedsTime
{-# INLINEABLE exceedsThreshold #-}

bufferExceedsLength :: UnlinedText -> Bool
bufferExceedsLength t = tLen > bufLen
Expand All @@ -319,17 +313,13 @@ readAndUpdateRef (prevReadRef, bufferLength, bufferTimeout, bufferWriteTimeRef)
pure $ diffTime > bufTimeout
where
bufTimeout = bufferTimeout ^. #unBufferTimeout % #unTimeout
{-# INLINEABLE bufferExceedsTime #-}

resetPrevReadRef' = resetPrevReadRef prevReadRef
{-# INLINEABLE resetPrevReadRef' #-}

updateRef = writeIORef prevReadRef . Just
{-# INLINEABLE updateRef #-}

maybeToReadHandleResult Nothing = ReadNoData
maybeToReadHandleResult (Just read) = ReadSuccess (ne read)
{-# INLINEABLE maybeToReadHandleResult #-}
{-# INLINEABLE readAndUpdateRef #-}

-- | Intended for a final read that handles previous read data.
Expand Down Expand Up @@ -357,26 +347,22 @@ readAndUpdateRefFinal prevReadRef =
readIORef prevReadRef >>= \case
Nothing -> resetPrevReadRef' $> ReadNoData
Just prevRead -> resetPrevReadRef' $> ReadSuccess (ne prevRead)
{-# INLINEABLE onNoData #-}

-- 2. Partial read: Combine if prevRead exists, send off result.
onPartialRead :: UnlinedText -> m ReadHandleResult
onPartialRead finalPartialRead = do
readIORef prevReadRef >>= \case
Nothing -> resetPrevReadRef' $> ReadSuccess (ne finalPartialRead)
Just prevRead -> resetPrevReadRef' $> ReadSuccess (ne $ prevRead <> finalPartialRead)
{-# INLINEABLE onPartialRead #-}

-- 3. Completed and partial reads: Combine, send off result.
onCompletedAndPartialRead :: NonEmpty UnlinedText -> UnlinedText -> m ReadHandleResult
onCompletedAndPartialRead completedReads finalPartialRead = do
completedReads' <- mPrependPrevRead prevReadRef completedReads
resetPrevReadRef'
pure $ ReadSuccess $ completedReads' <> ne finalPartialRead
{-# INLINEABLE onCompletedAndPartialRead #-}

resetPrevReadRef' = resetPrevReadRef prevReadRef
{-# INLINEABLE resetPrevReadRef' #-}
{-# INLINEABLE readAndUpdateRefFinal #-}

mPrependPrevRead ::
Expand Down
16 changes: 8 additions & 8 deletions src/Shrun/Logging/MonadRegionLogger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ instance MonadRegionLogger IO where
type Region IO = ConsoleRegion

logGlobal = putTextLn
{-# INLINEABLE logGlobal #-}


logRegion LogModeSet cr = Regions.setConsoleRegion cr
logRegion LogModeAppend cr = Regions.appendConsoleRegion cr
logRegion LogModeFinish cr = Regions.finishConsoleRegion cr
{-# INLINEABLE logRegion #-}


withRegion = Regions.withConsoleRegion
{-# INLINEABLE withRegion #-}


displayRegions = Regions.displayConsoleRegions
{-# INLINEABLE displayRegions #-}


instance (MonadRegionLogger m) => MonadRegionLogger (ReaderT env m) where
type Region (ReaderT env m) = Region m

logGlobal = lift . logGlobal
{-# INLINEABLE logGlobal #-}


logRegion m r = lift . logRegion m r
{-# INLINEABLE logRegion #-}


withRegion l f =
ask >>= \e -> lift (withRegion l (\r -> runReaderT (f r) e))
{-# INLINEABLE withRegion #-}


displayRegions m = ask >>= \e -> lift (displayRegions $ runReaderT m e)
{-# INLINEABLE displayRegions #-}

6 changes: 3 additions & 3 deletions src/Shrun/Notify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ sendNotif summary body urgency = do
notify (mkNote timeout) >>= \case
Nothing -> pure ()
Just notifyEx -> withRegion Linear (logEx notifyEx)
{-# INLINEABLE notifyWithErrorLogging #-}


logEx ex r = do
-- set exit code
Expand All @@ -72,7 +72,7 @@ sendNotif summary body urgency = do
lvl = LevelError,
mode = LogModeFinish
}
{-# INLINEABLE logEx #-}


mkNote timeout =
MkShrunNote
Expand All @@ -81,4 +81,4 @@ sendNotif summary body urgency = do
urgency,
timeout
}
{-# INLINEABLE mkNote #-}

8 changes: 4 additions & 4 deletions src/Shrun/Notify/DBus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ class (Monad m) => MonadDBus m where

instance MonadDBus IO where
connectSession = DBusC.connectSession
{-# INLINEABLE connectSession #-}


notify client note =
trySync (DBusN.notify client note) <&> \case
Left err -> Just err
Right _ -> Nothing
{-# INLINEABLE notify #-}


instance (MonadDBus m) => MonadDBus (ReaderT env m) where
connectSession = lift connectSession
{-# INLINEABLE connectSession #-}

notify c = lift . notify c
{-# INLINEABLE notify #-}


notifyDBus ::
( HasCallStack,
Expand Down
2 changes: 1 addition & 1 deletion src/Shrun/ShellT.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ instance
sendNote (DBus client) = DBus.notifyDBus client note
sendNote NotifySend = NotifySend.notifyNotifySend note
sendNote AppleScript = AppleScript.notifyAppleScript note
{-# INLINEABLE notify #-}

0 comments on commit f0abd42

Please sign in to comment.