Skip to content

Commit 58ec7ea

Browse files
committed
re-inline the old file instead of loading it twice
1 parent 0913d2e commit 58ec7ea

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,8 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
772772
-- at a time. Therefore the IORef contains the currently running cradle, if we try
773773
-- to get some more options then we wait for the currently running action to finish
774774
-- before attempting to do so.
775-
let getOptionsWorker :: FilePath -> IO ()
776-
getOptionsWorker file = do
777-
logWith recorder Debug (LogGetOptionsLoop file)
775+
let getOptions :: FilePath -> IO ()
776+
getOptions file = do
778777
let ncfp = toNormalizedFilePath' file
779778
cachedHieYamlLocation <- atomically $ STM.lookup ncfp filesMap
780779
hieYaml <- cradleLoc file
@@ -788,6 +787,14 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
788787
-- delete file from pending files
789788
S.delete file pendingFileSet
790789

790+
let getOptionsLoop :: IO ()
791+
getOptionsLoop = do
792+
-- Get the next file to load
793+
absFile <- atomically $ S.readQueue pendingFileSet
794+
logWith recorder Debug (LogGetOptionsLoop absFile)
795+
getOptions absFile
796+
getOptionsLoop
797+
791798
-- | Given a file, this function will return the HscEnv and the dependencies
792799
-- it would look up the cache first, if the cache is not available, it would
793800
-- submit a request to the getOptionsLoop to get the options for the file
@@ -813,12 +820,11 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
813820
Nothing -> do
814821
-- if not ok, we need to reload the session
815822
atomically $ S.insert absFile pendingFileSet
816-
-- line up the session to load
817-
atomically $ writeTQueue que (getOptionsWorker absFile)
818823
lookupOrWaitCache absFile
819824

820825
-- see Note [Serializing runs in separate thread]
821826
-- Start the getOptionsLoop if the queue is empty
827+
liftIO $ atomically $ Extra.whenM (isEmptyTQueue que) $ writeTQueue que getOptionsLoop
822828
returnWithVersion $ \file -> do
823829
let absFile = toAbsolutePath file
824830
second Map.keys <$> lookupOrWaitCache absFile

ghcide/session-loader/Development/IDE/Session/OrderedSet.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Development.IDE.Session.OrderedSet where
22

3-
import Control.Concurrent.STM (STM, TQueue, newTQueueIO)
3+
import Control.Concurrent.STM (STM, TQueue, flushTQueue,
4+
newTQueueIO)
45
import Control.Concurrent.STM.TQueue (readTQueue, writeTQueue)
56
import Control.Monad (when)
67
import Data.Hashable (Hashable)
@@ -15,7 +16,14 @@ type OrderedSet a = (TQueue a, Set a)
1516
insert :: Hashable a => a -> OrderedSet a -> STM ()
1617
insert a (que, s) = do
1718
(_, inserted) <- S.focus (Focus.testingIfInserts $ Focus.insert ()) a s
18-
when inserted $ writeTQueue que a
19+
-- if already in the set
20+
-- update the position of the element in the queue
21+
when (not inserted) $ do
22+
items <- filter (==a) <$> flushTQueue que
23+
mapM_ (writeTQueue que) items
24+
return ()
25+
writeTQueue que a
26+
-- when que $ writeTQueue que a
1927

2028
newIO :: Hashable a => IO (OrderedSet a)
2129
newIO = do
@@ -27,7 +35,7 @@ readQueue :: Hashable a => OrderedSet a -> STM a
2735
readQueue rs@(que, s) = do
2836
f <- readTQueue que
2937
b <- S.lookup f s
30-
-- retry if the file is already in done
38+
-- retry if no files are left in the queue
3139
if b then return f else readQueue rs
3240

3341
lookup :: Hashable a => a -> OrderedSet a -> STM Bool

0 commit comments

Comments
 (0)