Skip to content

Commit

Permalink
Faster compiled scripts
Browse files Browse the repository at this point in the history
Note the changelog entry, which explains a downside to this approach.
  • Loading branch information
snoyberg committed Feb 19, 2019
1 parent bc0bc3e commit 3ef899b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Other enhancements:
[#4535](https://github.com/commercialhaskell/stack/issues/4535)/
* Show snapshot being used when `stack ghci` is invoked outside of a project directory. See
[#3651](https://github.com/commercialhaskell/stack/issues/3651)
* When using the script interpreter with `--optimize` or `--compile`,
Stack will perform an optimization of checking whether a newer
executable exists, making reruns significantly faster. There's a
downside to this, however: if you have a multifile script, and
change one of the dependency modules, Stack will not automatically
detect and recompile.

Bug fixes:

Expand Down
1 change: 1 addition & 0 deletions src/Stack/Runners.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Stack.Runners
, loadCompilerVersion
, withUserFileLock
, munlockFile
, withRunnerGlobal
) where

import Stack.Prelude
Expand Down
36 changes: 28 additions & 8 deletions src/Stack/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Stack.Types.Compiler
import Stack.Types.Config
import Stack.Types.SourceMap
import System.FilePath (dropExtension, replaceExtension)
import qualified RIO.Directory as Dir
import RIO.Process
import qualified RIO.Text as T

Expand All @@ -42,6 +43,25 @@ scriptCmd opts go' = do
}
, globalStackYaml = SYLNoConfig scriptDir
}

-- Optimization: if we're compiling, and the executable is newer
-- than the source file, run it immediately.
case soCompile opts of
SEInterpret -> longWay file scriptDir go
SECompile -> shortCut file scriptDir go
SEOptimize -> shortCut file scriptDir go

where
shortCut file scriptDir go = handleIO (const $ longWay file scriptDir go) $ do
srcMod <- getModificationTime file
exeMod <- Dir.getModificationTime $ toExeName $ toFilePath file
if srcMod < exeMod
then withRunnerGlobal go' $ \runner ->
runRIO runner $
exec (toExeName $ toFilePath file) (soArgs opts)
else longWay file scriptDir go

longWay file scriptDir go = do
withDefaultBuildConfigAndLock go $ \lk -> do
-- Some warnings in case the user somehow tries to set a
-- stack.yaml location. Note that in this functions we use
Expand Down Expand Up @@ -121,16 +141,16 @@ scriptCmd opts go' = do
(ghcArgs ++ [toFilePath file])
(void . readProcessStdout_)
exec (toExeName $ toFilePath file) (soArgs opts)
where
toPackageName = reverse . drop 1 . dropWhile (/= '-') . reverse

-- Like words, but splits on both commas and spaces
wordsComma = splitWhen (\c -> c == ' ' || c == ',')
toPackageName = reverse . drop 1 . dropWhile (/= '-') . reverse

-- Like words, but splits on both commas and spaces
wordsComma = splitWhen (\c -> c == ' ' || c == ',')

toExeName fp =
if osIsWindows
then replaceExtension fp "exe"
else dropExtension fp
toExeName fp =
if osIsWindows
then replaceExtension fp "exe"
else dropExtension fp

getPackagesFromModuleInfo
:: ModuleInfo
Expand Down

0 comments on commit 3ef899b

Please sign in to comment.