Skip to content

Commit 9404692

Browse files
committed
avoid calling withNumCapabilities on every build
1 parent 2f7faaf commit 9404692

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

ghcide/src/Development/IDE/Main.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Development.IDE.Main
88
,defaultMain
99
) where
1010
import Control.Concurrent.Extra (newLock, readVar,
11-
withLock)
11+
withLock,
12+
withNumCapabilities)
1213
import Control.Exception.Safe (Exception (displayException),
1314
catchAny)
1415
import Control.Monad.Extra (concatMapM, unless,
@@ -68,6 +69,7 @@ import Development.IDE.Types.Options (IdeGhcSession,
6869
defaultIdeOptions,
6970
optModifyDynFlags)
7071
import Development.IDE.Types.Shake (Key (Key))
72+
import GHC.Conc (getNumProcessors)
7173
import GHC.IO.Encoding (setLocaleEncoding)
7274
import GHC.IO.Handle (hDuplicate)
7375
import HIE.Bios.Cradle (findCradle)
@@ -86,6 +88,7 @@ import Ide.Types (IdeCommand (IdeCommand),
8688
PluginId (PluginId),
8789
ipMap)
8890
import qualified Language.LSP.Server as LSP
91+
import Numeric.Natural (Natural)
8992
import Options.Applicative hiding (action)
9093
import qualified System.Directory.Extra as IO
9194
import System.Exit (ExitCode (ExitFailure),
@@ -163,6 +166,7 @@ data Arguments = Arguments
163166
, argsDebouncer :: IO (Debouncer NormalizedUri) -- ^ Debouncer used for diagnostics
164167
, argsHandleIn :: IO Handle
165168
, argsHandleOut :: IO Handle
169+
, argsThreads :: Maybe Natural
166170
}
167171

168172
instance Default Arguments where
@@ -179,6 +183,7 @@ instance Default Arguments where
179183
, argsDefaultHlsConfig = def
180184
, argsGetHieDbLoc = getHieDbLoc
181185
, argsDebouncer = newAsyncDebouncer
186+
, argsThreads = Nothing
182187
, argsHandleIn = pure stdin
183188
, argsHandleOut = do
184189
-- Move stdout to another file descriptor and duplicate stderr
@@ -221,12 +226,14 @@ defaultMain Arguments{..} = do
221226
inH <- argsHandleIn
222227
outH <- argsHandleOut
223228

229+
numProcessors <- getNumProcessors
230+
224231
case argCommand of
225232
PrintExtensionSchema ->
226233
LT.putStrLn $ decodeUtf8 $ A.encodePretty $ pluginsToVSCodeExtensionSchema argsHlsPlugins
227234
PrintDefaultConfig ->
228235
LT.putStrLn $ decodeUtf8 $ A.encodePretty $ pluginsToDefaultConfig argsHlsPlugins
229-
LSP -> do
236+
LSP -> withNumCapabilities (maybe (numProcessors `div` 2) fromIntegral argsThreads) $ do
230237
t <- offsetTime
231238
hPutStrLn stderr "Starting LSP server..."
232239
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run WITHOUT the --lsp option!"

hls-graph/src/Development/IDE/Graph/Database.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module Development.IDE.Graph.Database(
1111
shakeProfileDatabase,
1212
) where
1313

14-
import Control.Concurrent.Extra
1514
import Data.Dynamic
1615
import qualified Data.HashSet as HashSet
1716
import Data.IORef (readIORef)
@@ -24,9 +23,8 @@ import qualified Development.IDE.Graph.Internal.Intern as Intern
2423
import Development.IDE.Graph.Internal.Options
2524
import Development.IDE.Graph.Internal.Rules
2625
import Development.IDE.Graph.Internal.Types
27-
import GHC.Conc
2826

29-
data ShakeDatabase = ShakeDatabase !Int !Int [Action ()] Database
27+
data ShakeDatabase = ShakeDatabase !Int [Action ()] Database
3028

3129
-- Placeholder to be the 'extra' if the user doesn't set it
3230
data NonExportedType = NonExportedType
@@ -39,9 +37,7 @@ shakeNewDatabase opts rules = do
3937
let extra = fromMaybe (toDyn NonExportedType) $ shakeExtra opts
4038
(theRules, actions) <- runRules extra rules
4139
db <- newDatabase extra theRules
42-
let threads = shakeThreads opts
43-
threads <- if threads /= 0 then pure threads else getNumProcessors
44-
pure $ ShakeDatabase threads (length actions) actions db
40+
pure $ ShakeDatabase (length actions) actions db
4541

4642
shakeRunDatabase :: ShakeDatabase -> [Action a] -> IO ([a], [IO ()])
4743
shakeRunDatabase = shakeRunDatabaseForKeys Nothing
@@ -67,7 +63,7 @@ shakeRunDatabaseForKeys
6763
-> ShakeDatabase
6864
-> [Action a]
6965
-> IO ([a], [IO ()])
70-
shakeRunDatabaseForKeys keysChanged (ShakeDatabase threads lenAs1 as1 db) as2 = withNumCapabilities threads $ do
66+
shakeRunDatabaseForKeys keysChanged (ShakeDatabase lenAs1 as1 db) as2 = do
7167
incDatabase db
7268
flushDirty db
7369
-- record the keys changed

hls-graph/src/Development/IDE/Graph/Internal/Options.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
module Development.IDE.Graph.Internal.Options where
44

5-
import Data.Dynamic
6-
import Control.Monad.Trans.Reader
7-
import Development.IDE.Graph.Internal.Types
5+
import Control.Monad.Trans.Reader
6+
import Data.Dynamic
7+
import Development.IDE.Graph.Internal.Types
88

99
data ShakeOptions = ShakeOptions {
10+
-- | Has no effect, kept only for api compatibility with Shake
1011
shakeThreads :: Int,
1112
shakeFiles :: FilePath,
1213
shakeExtra :: Maybe Dynamic,

0 commit comments

Comments
 (0)