Skip to content

Commit

Permalink
```
Browse files Browse the repository at this point in the history
Refactor session loading to handle pending files

Introduced a queue to manage pending files during session loading, ensuring that file options are processed in batches. This change improves the efficiency and reliability of session initialization by serializing file processing and avoiding redundant lookups.
```
  • Loading branch information
soulomoon committed Oct 27, 2024
1 parent c21ce09 commit df21318
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions ghcide/session-loader/Development/IDE/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
-- you have to modify 'filesMap' as well.
filesMap <- newVar HM.empty :: IO (Var FilesMap)
-- Version of the mappings above
pendingFilesTQueue <- newTQueueIO
version <- newVar 0
biosSessionLoadingVar <- newVar Nothing :: IO (Var (Maybe SessionLoadingPreferenceConfig))
let returnWithVersion fun = IdeGhcSession fun <$> liftIO (readVar version)
Expand All @@ -474,7 +475,6 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
let invalidateShakeCache = do
void $ modifyVar' version succ
return $ toNoFileKey GhcSessionIO
OfInterestVar filesOfInterVar <- getIdeGlobalAction
IdeOptions{ optTesting = IdeTesting optTesting
, optCheckProject = getCheckProject
, optExtensions
Expand Down Expand Up @@ -728,31 +728,23 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
sessionOptsList yamlFiles



-- The main function which gets options for a file. We only want one of these running
-- at a time. Therefore the IORef contains the currently running cradle, if we try
-- to get some more options then we wait for the currently running action to finish
-- before attempting to do so.
-- let getOptions :: FilePath -> IO (IdeResult HscEnvEq, [FilePath])
-- getOptions file = do
-- let ncfp = toNormalizedFilePath' (toAbsolutePath file)
-- cachedHieYamlLocation <- HM.lookup ncfp <$> readVar filesMap
-- hieYaml <- cradleLoc file
-- sessionOpts (join cachedHieYamlLocation <|> hieYaml, file) `Safe.catch` \e ->
-- return (([renderPackageSetupException file e], Nothing), maybe [] pure hieYaml)

let getOptionsBatch :: FilePath -> IO (IdeResult HscEnvEq, [FilePath])
getOptionsBatch file' = do
pendingFiles <- atomically $ flushTQueue pendingFilesTQueue
let file = toAbsolutePath file'
hieYaml <- cradleLoc file
filesOfInterest <- HashMap.keys <$> readVar filesOfInterVar
logWith recorder Debug LogSettingInitialDynFlags
results <- getOptionsList (Set.toList $ Set.fromList $ toAbsolutePath file : map fromNormalizedFilePath filesOfInterest)
results <- getOptionsList (Set.toList $ Set.fromList $ toAbsolutePath file : map fromNormalizedFilePath pendingFiles)
return (results Map.! file) `Safe.catch` \e ->
return (([renderPackageSetupException file e], Nothing), maybe [] pure hieYaml)

returnWithVersion $ \file -> do
-- see Note [Serializing runs in separate thread]
atomically $ writeTQueue pendingFilesTQueue file
awaitRunInThread que $ getOptionsBatch file

-- cradleToType :: Cradle Void -> IO ()
Expand Down

0 comments on commit df21318

Please sign in to comment.