Repository: dotnet/roslyn
File: src/Workspaces/Remote/ServiceHub/Host/RemoteWorkspace.cs (C#)

Code before:

        // In solution load, we might not yet have CurrentSolution updated since we haven't synchronized our primary solution over. But we might already have
        // synchronized part of it over, so let's see if we have a solution that matches what we need.
        Solution? bestSolution = null;
        int? bestSolutionProjectsMissing = null;

        void UpdateBestSolution(Solution candidateSolution)
        {
            if (candidateSolution.Id == newSolutionInfo.Id && candidateSolution.FilePath == newSolutionInfo.FilePath)
            {
                // Compute how many projects are missing from this one
                var ids = new HashSet<ProjectId>(newSolutionChecksums.Projects.Ids);
                foreach (var id in candidateSolution.ProjectIds)
                    ids.Remove(id);

                var projectsMissing = ids.Count;

                if (!bestSolutionProjectsMissing.HasValue)
                {
                    bestSolution = candidateSolution;
                    bestSolutionProjectsMissing = projectsMissing;
                }
                else if (projectsMissing < bestSolutionProjectsMissing.Value)
                {
                    bestSolution = candidateSolution;
                    bestSolutionProjectsMissing = projectsMissing;
                }
            }
        }

        // First, is our CurrentSolution already a candidate that can't be beat?
        UpdateBestSolution(this.CurrentSolution);
        if (bestSolution is not null && bestSolutionProjectsMissing == 0)
            return bestSolution;

        var solutions = new HashSet<Solution>();
        using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
        {
            _lastRequestedAnyBranchSolutions.AddAllTo(solutions);
        }

        foreach (var solution in solutions)
            UpdateBestSolution(solution);

        if (bestSolution is not null)
            return bestSolution;

        // If not, have to create a new, fresh, solution instance to update.
        var solutionInfo = await assetProvider.CreateSolutionInfoAsync(
            solutionChecksum, this.Services.SolutionServices, cancellationToken).ConfigureAwait(false);
        return CreateSolutionFromInfo(solutionInfo);

Review comment (refactor):
ok.  so i guess you are willing to return any solution...

--

tbh, i like what you're doing here, but i think you've made this calling code too complex.  i would just remove the initial special casing of:

```
        // First, is our CurrentSolution already a candidate that can't be beat?
```

instead, just do:

```
        var solutions = new HashSet<Solution>();
solutions.AddIfNotNull(this.CurrentSolution);
```

then do the rest of the logic you have here.  if this.CurrentSolution is best, it will naturally get picked.  

--

I prefer this simplicity, but i'm open to discuss :)

Code after:

        // We update CurrentSolution by periodically syncing the primary solution over from our main process. During solution load, that might not happen prior to
        // a file being opened and the user trying to interact with a project; in that case CurrentSolution might still be empty. In that window of time,
        // each operation from OOP would then synchronize over the projects or cone from scratch, and then effectively throwing away that state since the next request will
        // try to base itself from the (empty) CurrentSolution again. To avoid that, we'll see if we have some other forked solution available that has some of the projects we might
        // need, but checking how many projects are missing. This is especially important for cases where the projects may have generators, since a first time generator run
        // could be expensive.
        Solution? bestSolution = null;
        int? bestSolutionProjectsMissingCount = null;

        // First, is our CurrentSolution already a candidate that can't be beat?
        UpdateBestSolution(this.CurrentSolution);
        if (bestSolution is not null && bestSolutionProjectsMissingCount == 0)
            return bestSolution;

        using var _ = PooledHashSet<Solution>.GetInstance(out var solutions);
        using (await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false))
        {
            _lastRequestedAnyBranchSolutions.AddAllTo(solutions);
        }

        foreach (var solution in solutions)
            UpdateBestSolution(solution);

        if (bestSolution is not null)
            return bestSolution;

        // If not, have to create a new, fresh, solution instance to update.
        var solutionInfo = await assetProvider.CreateSolutionInfoAsync(
            solutionChecksum, this.Services.SolutionServices, cancellationToken).ConfigureAwait(false);
        return CreateSolutionFromInfo(solutionInfo);

        // Updates <see cref="bestSolution"/>, preferring whichever Solution will require synchronizing over the fewest new projects.
        void UpdateBestSolution(Solution candidateSolution)
        {
            if (candidateSolution.Id == newSolutionInfo.Id && candidateSolution.FilePath == newSolutionInfo.FilePath)
            {
                // Compute how many projects are missing from this one
                using var _ = PooledHashSet<ProjectId>.GetInstance(out var ids);
                ids.AddAll(newSolutionChecksums.Projects.Ids);
                foreach (var id in candidateSolution.ProjectIds)
                    ids.Remove(id);

                var projectsMissing = ids.Count;

                if (!bestSolutionProjectsMissingCount.HasValue)
                {
                    bestSolution = candidateSolution;
                    bestSolutionProjectsMissingCount = projectsMissing;
                }
                else if (projectsMissing < bestSolutionProjectsMissingCount.Value)