Skip to content

Commit

Permalink
🚧 Use SocketWaitAll instead of SocketReadyQ
Browse files Browse the repository at this point in the history
- This saves CPU load in idle state on Unix.
  • Loading branch information
kenkangxgwe committed Jul 6, 2021
1 parent cdf0beb commit 934f931
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions src/WolframLanguageServer/Server.wl
Expand Up @@ -150,18 +150,14 @@ ServerConfig = <|
"textDocument/documentColor"
(* "textDocument/codeLens" *)
},
"delayedRequests" -> {
"textDocument/documentHighlight",
"textDocument/documentColor"
},
(* default delays (seconds) *)
"requestDelays" -> <|
"textDocument/publishDiagnostics" -> 2.5,
(* "textDocument/publishDiagnostics" -> 2.5,
"textDocument/signatureHelp" -> 0.5,
"textDocument/documentSymbol" -> 4.0,
"textDocument/documentHighlight" -> 0.5,
(* "textDocument/documentLink" -> 0.0, *)
"textDocument/documentColor" -> 5.0
"textDocument/documentLink" -> 4.0,
"textDocument/documentColor" -> 5.0 *)
|>
|>

Expand Down Expand Up @@ -427,6 +423,24 @@ TcpSocketHandler[state_WorkState] := With[
debugSession = state["debugSession"]
},

TimeConstrained[
SocketWaitNext[{client,debugSession["client"] // Replace[Null -> Nothing]}],
state
// getNextTaskTime
// Replace[{
_?MissingQ :> (
0.5
),
_?(GreaterThan[DatePlus[Now, {0.5, "Second"}]]) :> (
0.5
),
nextTime_?(GreaterThan[Now]) :> (
nextTime - Now
),
_ :> $TimeUnit
}]
];

Which[
SocketReadyQ[client],
handleMessageList[ReadMessages[client], state],
Expand Down Expand Up @@ -781,7 +795,7 @@ handleMessage[msg_Association, state_WorkState] := With[
cacheAvailableQ[method, msg, state],
LogInfo["Sending cached results of " <> ToString[method]];
sendCachedResult[method, msg, state],
MemberQ[ServerConfig["delayedRequests"], method],
KeyExistsQ[ServerConfig["requestDelays"], method],
scheduleDelayedRequest[method, msg, state],
True,
handleRequest[method, msg, state]
Expand Down Expand Up @@ -2607,27 +2621,34 @@ DeclareType[ServerTask, <|


addScheduledTask[state_WorkState, task_ServerTask] := (
state["scheduledTasks"]
// Map[Key["scheduledTime"]]
// FirstPosition[_DateObject?(GreaterThan[task["scheduledTime"]])]
// Replace[{
_?MissingQ :> (
state
// ReplaceKeyBy["scheduledTasks" -> Append[task]]
),
{pos_Integer} :> (
state
// ReplaceKeyBy["scheduledTasks" -> Insert[task, pos]]
state
// ReplaceKeyBy["scheduledTasks" -> (
tasklist \[Function] (
tasklist
// (
FirstPosition[tasklist, _ServerTask?((
"scheduledTime"
// (# > task)
// Through
)&), {-1}]
// Insert[task, #]&
)
)
}]
)]
)


getNextTaskTime[state_WorkState] := (
state["scheduledTasks"]
// First[#, <||>]&
// Key["scheduledTime"]
)

doNextScheduledTask[state_WorkState] := (
SelectFirst[state["scheduledTasks"], Key["scheduledTime"] /* LessThan[Now]]
// Replace[{
_?MissingQ :> (
Pause[0.001];
(* Pause[0.01]; *)
{"Continue", state}
),
task_ServerTask :> With[
Expand Down Expand Up @@ -2686,7 +2707,7 @@ doNextScheduledTask[state_WorkState] := (
_?MissingQ :> If[!MissingQ[task["callback"]],
(* If the function is time constrained, than the there should not be a lot of lags. *)
(* TimeConstrained[task["callback"][newState, task["params"]], 0.1, sendMessage[state["client"], ResponseMessage[<|"id" -> task["params"]["id"], "result" -> <||>|>]]], *)
(task["callback"] // LogDebug)[newState, task["params"] // LogDebug]
task["callback"][newState, task["params"]]
// AbsoluteTiming
// Apply[(LogInfo[{task["type"], #1}]; #2)&],
sendMessage[newState["client"], ResponseMessage[<|
Expand Down

0 comments on commit 934f931

Please sign in to comment.