diff --git a/docs/plans/PERFORMANCE-PLAN.md b/docs/plans/PERFORMANCE-PLAN.md index a032d430..b2e97886 100644 --- a/docs/plans/PERFORMANCE-PLAN.md +++ b/docs/plans/PERFORMANCE-PLAN.md @@ -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 diff --git a/src/editors/vscode/src/test/suite/memory-introspection-e2e.test.ts b/src/editors/vscode/src/test/suite/memory-introspection-e2e.test.ts index cf38909a..883c47d7 100644 --- a/src/editors/vscode/src/test/suite/memory-introspection-e2e.test.ts +++ b/src/editors/vscode/src/test/suite/memory-introspection-e2e.test.ts @@ -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), }); diff --git a/src/sidecars/SharpLsp.Sidecar.Common.Tests/WorkspaceRestoreConcurrencyTests.cs b/src/sidecars/SharpLsp.Sidecar.Common.Tests/WorkspaceRestoreConcurrencyTests.cs new file mode 100644 index 00000000..ed1cf8d5 --- /dev/null +++ b/src/sidecars/SharpLsp.Sidecar.Common.Tests/WorkspaceRestoreConcurrencyTests.cs @@ -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; + +/// +/// 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]. +/// +public sealed class WorkspaceRestoreConcurrencyTests : IDisposable +{ + /// How long each project's restore deliberately takes. + 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", + "" + ); + 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)); + } + + /// When a project's slow restore step began and ended, as it recorded them. + 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) + ); + } + + /// A package-free project whose restore takes and records when. + private string Slow(string name) + { + var wait = $""" + + + + + + + """; + 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, + "net10.0" + + $"{items}{targets}" + ); + return project; + } + + private static string Assets(string project) + { + return NativePaths.Resolve(NativePaths.DirectoryOf(project), "obj", "project.assets.json"); + } +} diff --git a/src/sidecars/SharpLsp.Sidecar.Common/WorkspaceRestore.cs b/src/sidecars/SharpLsp.Sidecar.Common/WorkspaceRestore.cs index 8ad988da..e7d0ff6e 100644 --- a/src/sidecars/SharpLsp.Sidecar.Common/WorkspaceRestore.cs +++ b/src/sidecars/SharpLsp.Sidecar.Common/WorkspaceRestore.cs @@ -49,20 +49,32 @@ CancellationToken ct } } + /// Most SDK restores run at once: each is its own MSBuild process. + private static readonly int ConcurrentRestores = Math.Clamp(Environment.ProcessorCount, 2, 4); + + /// + /// Restore stale projects side by side, bounded, and report the first failure in + /// project order; one failure never abandons the rest. [PERF-PROGRESSIVE-LOAD] + /// private static async Task RestoreProjectsAsync( IEnumerable 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.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.Value); } private static async Task EnsureProjectAsync(