Skip to content
Merged
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
5 changes: 4 additions & 1 deletion docs/plans/PERFORMANCE-PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ user never waits on projects they are not looking at.
current ([DIAG-RESTORE]). This repo's own projects were restored on every open because
the root `Directory.Build.props` imports through `$(MSBuildThisFileDirectory)` —
`WorkspaceRestoreDigestTests`
- [ ] Restore and evaluate projects concurrently (bounded), not one after another
- [x] Restore projects concurrently (bounded: 2-4 SDK restores at once), not one after another;
the first failure is reported in project order and never abandons the rest —
`WorkspaceRestoreConcurrencyTests`. Evaluation: F# already loads projects with
`Task.WhenAll`; Roslyn's `MSBuildWorkspace` evaluates its own solution
- [ ] Order the load: the project owning the active editor first, then its
references, then the rest of the solution
- [ ] Sidecars stream per-project progress to the host (`$/progress`-style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ suite('Memory introspection — real end-to-end', () => {
const debuggee = harness.debuggee();
vscode.debug.removeBreakpoints([...vscode.debug.breakpoints]);
const session = await launch(debuggee, launchConfig(debuggee, 'Memory introspection'));
// The heap holds the cached Widgets only once the program has started its loop.
await harness.recorder().waitForOutput('READY');
const started = await execute<{ memory_session_id: string }>('memory.start', {
pid: await debuggeePid(session),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System.Globalization;

// RS1035: this end-to-end test deliberately writes real project files for the real SDK.
#pragma warning disable RS1035

namespace SharpLsp.Sidecar.Common.Tests;

/// <summary>
/// A workspace's stale projects restore side by side, bounded, never one after another,
/// and one project's failed restore neither hides nor blocks the others.
/// [PERF-PROGRESSIVE-LOAD] (GitHub #563), [DIAG-RESTORE].
/// </summary>
public sealed class WorkspaceRestoreConcurrencyTests : IDisposable
{
/// <summary>How long each project's restore deliberately takes.</summary>
private const int SlowSeconds = 6;

private readonly string _root = NativePaths.Temp($"slsp-restore-par-{Guid.NewGuid():N}");

public void Dispose()
{
try
{
Directory.Delete(_root, true);
}
catch (IOException) { }
}

[Fact]
public async Task Stale_projects_restore_side_by_side_and_a_second_open_restores_none()
{
string[] projects = [Slow("A"), Slow("B")];

var result = await WorkspaceRestore.EnsureProjectsAsync(projects, CancellationToken.None);

Assert.True(result.IsOk, result.Match(_ => string.Empty, error => error));
Assert.All(projects, project => Assert.Equal(1, WorkspaceRestore.RestoresOf(project)));
Assert.All(projects, project => Assert.True(File.Exists(Assets(project)), project));
// Side by side: each restore's slow step began before the other's ended. One
// after another, the second would begin only after the first ended.
var windows = projects.Select(Window).ToArray();
Assert.All(windows, window => Assert.True(window.Start < window.End, "a real wait"));
Assert.True(
windows.Max(window => window.Start) < windows.Min(window => window.End),
$"the restores ran in series: {string.Join(", ", windows)}"
);

var again = await WorkspaceRestore.EnsureProjectsAsync(projects, CancellationToken.None);
Assert.True(again.IsOk, "a current workspace stays restored");
Assert.All(projects, project => Assert.Equal(1, WorkspaceRestore.RestoresOf(project)));
}

[Fact]
public async Task A_failed_restore_is_reported_in_project_order_and_the_others_still_restore()
{
var good = Slow("Good");
var broken = Project(
"Broken",
"<PackageReference Include=\"SharpLsp.No.Such.Package.Ever\" Version=\"1.0.0\" />"
);
var after = Slow("After");

var result = await WorkspaceRestore.EnsureProjectsAsync(
[good, broken, after],
CancellationToken.None
);

var error = result.Match(_ => string.Empty, failure => failure);
Assert.True(result.IsError, "a failed restore fails the workspace restore");
Assert.Contains(broken, error, StringComparison.Ordinal);
Assert.Contains("NU1101", error, StringComparison.Ordinal);
Assert.True(File.Exists(Assets(good)), "the project before the failure restored");
Assert.True(File.Exists(Assets(after)), "and the one after it was not abandoned");
Assert.Equal(1, WorkspaceRestore.RestoresOf(after));
}

/// <summary>When a project's slow restore step began and ended, as it recorded them.</summary>
private static (long Start, long End) Window(string project)
{
var ticks = File.ReadAllLines(
NativePaths.Resolve(NativePaths.DirectoryOf(project), "window.txt")
);
return (
long.Parse(ticks[0], CultureInfo.InvariantCulture),
long.Parse(ticks[1], CultureInfo.InvariantCulture)
);
}

/// <summary>A package-free project whose restore takes <see cref="SlowSeconds"/> and records when.</summary>
private string Slow(string name)
{
var wait = $"""
<Target Name="SlowRestore" BeforeTargets="Restore">
<WriteLinesToFile File="$(MSBuildProjectDirectory)/window.txt" Lines="$([System.DateTime]::UtcNow.Ticks)" Overwrite="true" />
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="powershell -NoProfile -Command Start-Sleep {SlowSeconds}" />
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="sleep {SlowSeconds}" />
<WriteLinesToFile File="$(MSBuildProjectDirectory)/window.txt" Lines="$([System.DateTime]::UtcNow.Ticks)" />
</Target>
""";
return Project(name, "", wait);
}

private string Project(string name, string items, string targets = "")
{
var directory = NativePaths.Resolve(_root, name);
_ = Directory.CreateDirectory(directory);
var project = NativePaths.Resolve(directory, $"{name}.csproj");
File.WriteAllText(
project,
"<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup>"
+ $"<ItemGroup>{items}</ItemGroup>{targets}</Project>"
);
return project;
}

private static string Assets(string project)
{
return NativePaths.Resolve(NativePaths.DirectoryOf(project), "obj", "project.assets.json");
}
}
28 changes: 20 additions & 8 deletions src/sidecars/SharpLsp.Sidecar.Common/WorkspaceRestore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,32 @@ CancellationToken ct
}
}

/// <summary>Most SDK restores run at once: each is its own MSBuild process.</summary>
private static readonly int ConcurrentRestores = Math.Clamp(Environment.ProcessorCount, 2, 4);

/// <summary>
/// Restore stale projects side by side, bounded, and report the first failure in
/// project order; one failure never abandons the rest. [PERF-PROGRESSIVE-LOAD]
/// </summary>
private static async Task<RestoreResult> RestoreProjectsAsync(
IEnumerable<string> projects,
CancellationToken ct
)
{
foreach (var project in projects)
var distinct = projects.Distinct(NativePaths.Comparer).ToArray();
var results = new RestoreResult[distinct.Length];
var bound = new ParallelOptions
{
var result = await EnsureProjectAsync(project, ct).ConfigureAwait(false);
if (result.IsError)
{
return result;
}
}
return new RestoreResult.Ok<Outcome.Unit, string>(Outcome.Unit.Value);
MaxDegreeOfParallelism = ConcurrentRestores,
CancellationToken = ct,
};
await Parallel
.ForEachAsync(Enumerable.Range(0, distinct.Length), bound, async (at, token) =>
results[at] = await EnsureProjectAsync(distinct[at], token).ConfigureAwait(false)
)
.ConfigureAwait(false);
return results.FirstOrDefault(result => result.IsError)
?? new RestoreResult.Ok<Outcome.Unit, string>(Outcome.Unit.Value);
}

private static async Task<RestoreResult> EnsureProjectAsync(
Expand Down