Skip to content

Commit 951c6d3

Browse files
edvilmekasperk81
andauthored
Apply suggestions from code review
Co-authored-by: kasperk81 <[email protected]>
1 parent 765016a commit 951c6d3

File tree

1 file changed

+19
-8
lines changed
  • src/Cli/dotnet/commands/dotnet-sln/remove

1 file changed

+19
-8
lines changed

src/Cli/dotnet/commands/dotnet-sln/remove/Program.cs

+19-8
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@ internal class RemoveProjectFromSolutionCommand : CommandBase
1919
private readonly string _fileOrDirectory;
2020
private readonly IReadOnlyCollection<string> _projects;
2121

22-
private int CountNonFolderDescendants(SolutionModel solution, SolutionFolderModel item, Dictionary<SolutionFolderModel, int> cached)
22+
private int CountNonFolderDescendants(SolutionFolderModel item, Dictionary<SolutionFolderModel, int> cached,
23+
Dictionary<SolutionFolderModel, List<SolutionItem>> childrenByParent)
2324
{
2425
if (cached.ContainsKey(item))
2526
{
2627
return cached[item];
2728
}
29+
2830
int count = item.Files?.Count ?? 0;
29-
var children = solution.SolutionItems.Where(i => i.Parent == item);
30-
foreach (var child in children)
31+
32+
// Retrieve children directly from the precomputed index
33+
if (childrenByParent.TryGetValue(item, out var children))
3134
{
32-
count += child is SolutionFolderModel folderModel
33-
? CountNonFolderDescendants(solution, folderModel, cached)
34-
: 1;
35+
foreach (var child in children)
36+
{
37+
count += child is SolutionFolderModel folderModel
38+
? CountNonFolderDescendants(folderModel, cached, childrenByParent)
39+
: 1;
40+
}
3541
}
36-
cached.Add(item, count);
42+
43+
cached[item] = count; // Using 'cached[item] = count;' instead of 'Add' for better clarity
3744
return count;
3845
}
3946

@@ -111,10 +118,14 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<
111118
}
112119
}
113120

121+
var childrenByParent = solution.SolutionItems
122+
.GroupBy(item => item.Parent)
123+
.ToDictionary(g => g.Key, g => g.ToList());
124+
114125
Dictionary<SolutionFolderModel, int> nonFolderDescendantsCount = new();
115126
foreach (var item in solution.SolutionFolders)
116127
{
117-
CountNonFolderDescendants(solution, item, nonFolderDescendantsCount);
128+
CountNonFolderDescendants(item, nonFolderDescendantsCount, childrenByParent);
118129
}
119130

120131
var emptyFolders = nonFolderDescendantsCount.Where(i => i.Value == 0).Select(i => i.Key);

0 commit comments

Comments
 (0)