Skip to content
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

Improve startup performance in VS Code #4634

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion Python/Product/Analysis/LanguageServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public void TraceMessage(IFormattable message) {

internal async Task<InitializeResult> Initialize(InitializeParams @params, CancellationToken cancellationToken) {
ThrowIfDisposed();
await DoInitializeAsync(@params, cancellationToken);
if (@params.initializationOptions.asyncStartup) {
CompleteInitialization = DoInitializeAsync(@params, cancellationToken);
} else {
await DoInitializeAsync(@params, cancellationToken);
}

return new InitializeResult {
capabilities = new ServerCapabilities {
Expand All @@ -153,6 +157,9 @@ internal async Task<InitializeResult> Initialize(InitializeParams @params, Cance
}
};
}

internal Task CompleteInitialization { get; private set; } = Task.CompletedTask;
Copy link
Contributor

@AlexanderSher AlexanderSher Aug 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't safe. Awaiter may reach CompleteInitialization before it is replaced. I'd rather go with TaskCompetionSource:

internal async Task<InitializeResult> Initialize(InitializeParams @params, CancellationToken cancellationToken) { 
    ThrowIfDisposed();
    if (!@params.initializationOptions.asyncStartup) { 
        _completeInitializationTcs.TrySetCompleted(null);
    }

    using (_completeInitializationTcs.RegisterForCancellation(cancellationToken)) {
        await DoInitializeAsync(@params, cancellationToken); 
        _completeInitializationTcs.TrySetCompleted(null);
    }
internal Task CompleteInitialization => _completeInitializationTcs.Task

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although point here is NOT to await on DoInitializeAsync and just let it run. Awaiting blocks VSC startup.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, nothing ever called until initialized returns. So I don't think the task can be replaced easily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do I get it right that you want to wait for initialization to complete on the first request after initialization, rather than during initialization request itself?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Those are 'open file' and 'change config' - they come after the init. Actually we need to debug why sync context does not wait for completions - or, rather, the StreamJsonRpc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might need to use alternative implementation (from R/vsc) instead of sync context which actually makes sure requests do complete sequentially. See https://github.com/MikhailArkhipov/PTVS/blob/sync/Python/Product/VSCode/AnalysisVsc/LanguageServerProxy.cs


public override Task Shutdown() {
ThrowIfDisposed();
ProjectFiles.Clear();
Expand Down
5 changes: 5 additions & 0 deletions Python/Product/Analysis/LanguageServer/Structures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public struct Interpreter {
/// This will likely have a performance impact.
/// </summary>
public bool traceLogging;

/// <summary>
/// If true, analyzer will be created asynchronously. Used in VS Code.
/// </summary>
public bool asyncStartup;
}

[Serializable]
Expand Down
12 changes: 11 additions & 1 deletion Python/Product/VSCode/AnalysisVsc/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public sealed partial class LanguageServer : IDisposable {
private readonly CancellationTokenSource _sessionTokenSource = new CancellationTokenSource();
private readonly RestTextConverter _textConverter = new RestTextConverter();
private readonly Dictionary<Uri, Diagnostic[]> _pendingDiagnostic = new Dictionary<Uri, Diagnostic[]>();
private ManualResetEventSlim _initComplete = new ManualResetEventSlim(false);
private readonly object _lock = new object();

private IUIService _ui;
Expand Down Expand Up @@ -153,6 +154,9 @@ private void OnUnregisterCapability(object sender, UnregisterCapabilityEventArgs
#region Workspace
[JsonRpcMethod("workspace/didChangeConfiguration")]
public async Task DidChangeConfiguration(JToken token, CancellationToken cancellationToken) {
// Change configuration comes right after the initialization
WaitForServerInitComplete(cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why await server.CompleteInitialization isn't enough?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It yields, we get next request. So we end up receiving requests without having initialization complete. One option to add await CompleteInitialization to sync context and call it before any request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But DidChangeConfiguration is called through sync context already. We have it in JsonRPC, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It called sequentially but I do see the yield. Perhaps our sync context is incorrect.


var settings = new LanguageServerSettings();

var rootSection = token["settings"];
Expand Down Expand Up @@ -212,8 +216,9 @@ public Task<object> ExecuteCommand(JToken token)

#region TextDocument
[JsonRpcMethod("textDocument/didOpen")]
public Task DidOpenTextDocument(JToken token) {
public Task DidOpenTextDocument(JToken token, CancellationToken cancellationToken) {
_idleTimeTracker?.NotifyUserActivity();
WaitForServerInitComplete(cancellationToken);
return _server.DidOpenTextDocument(ToObject<DidOpenTextDocumentParams>(token));
}

Expand Down Expand Up @@ -426,6 +431,11 @@ private void PublishPendingDiagnostics() {
}
}

private void WaitForServerInitComplete(CancellationToken token) {
_server.CompleteInitialization.ContinueWith(t => _initComplete.Set()).DoNotWait();
_initComplete.Wait(token);
}

private sealed class IdleTimeTracker : IDisposable {
private readonly int _delay;
private readonly Action _action;
Expand Down