Skip to content

remove unsafePerformIO #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions ghcide/src/Development/IDE/Core/Shake.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ import qualified Development.Shake as Shake
import Control.Monad.Extra
import Data.Time
import GHC.Generics
import System.IO.Unsafe
import Language.LSP.Types
import qualified Control.Monad.STM as STM
import Control.Monad.IO.Class
Expand Down Expand Up @@ -371,29 +370,24 @@ data IdeState = IdeState
,shakeSession :: MVar ShakeSession
,shakeClose :: IO ()
,shakeExtras :: ShakeExtras
,shakeProfileDir :: Maybe FilePath
,shakeDatabaseProfile :: ShakeDatabase -> IO (Maybe FilePath)
,stopProgressReporting :: IO ()
}



-- This is debugging code that generates a series of profiles, if the Boolean is true
shakeDatabaseProfile :: Maybe FilePath -> ShakeDatabase -> IO (Maybe FilePath)
shakeDatabaseProfile mbProfileDir shakeDb =
shakeDatabaseProfileIO :: Maybe FilePath -> IO(ShakeDatabase -> IO (Maybe FilePath))
shakeDatabaseProfileIO mbProfileDir = do
profileStartTime <- formatTime defaultTimeLocale "%Y%m%d-%H%M%S" <$> getCurrentTime
profileCounter <- newVar (0::Int)
return $ \shakeDb ->
for mbProfileDir $ \dir -> do
count <- modifyVar profileCounter $ \x -> let !y = x+1 in return (y,y)
let file = "ide-" ++ profileStartTime ++ "-" ++ takeEnd 5 ("0000" ++ show count) <.> "html"
shakeProfileDatabase shakeDb $ dir </> file
return (dir </> file)

{-# NOINLINE profileStartTime #-}
profileStartTime :: String
profileStartTime = unsafePerformIO $ formatTime defaultTimeLocale "%Y%m%d-%H%M%S" <$> getCurrentTime

{-# NOINLINE profileCounter #-}
profileCounter :: Var Int
profileCounter = unsafePerformIO $ newVar 0

setValues :: IdeRule k v
=> Var Values
-> k
Expand Down Expand Up @@ -502,6 +496,7 @@ shakeOpen lspEnv logger debouncer
shakeDb <- shakeDbM
initSession <- newSession shakeExtras shakeDb []
shakeSession <- newMVar initSession
shakeDatabaseProfile <- shakeDatabaseProfileIO shakeProfileDir
let ideState = IdeState{..}

IdeOptions{ optOTMemoryProfiling = IdeOTMemoryProfiling otProfilingEnabled } <- getIdeOptionsIO shakeExtras
Expand Down Expand Up @@ -628,7 +623,7 @@ shakeRestart IdeState{..} acts =
shakeSession
(\runner -> do
(stopTime,()) <- duration (cancelShakeSession runner)
res <- shakeDatabaseProfile shakeProfileDir shakeDb
res <- shakeDatabaseProfile shakeDb
let profile = case res of
Just fp -> ", profile saved at " <> fp
_ -> ""
Expand Down