@@ -19,21 +19,28 @@ internal class RemoveProjectFromSolutionCommand : CommandBase
19
19
private readonly string _fileOrDirectory ;
20
20
private readonly IReadOnlyCollection < string > _projects ;
21
21
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 )
23
24
{
24
25
if ( cached . ContainsKey ( item ) )
25
26
{
26
27
return cached [ item ] ;
27
28
}
29
+
28
30
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 ) )
31
34
{
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
+ }
35
41
}
36
- cached . Add ( item , count ) ;
42
+
43
+ cached [ item ] = count ; // Using 'cached[item] = count;' instead of 'Add' for better clarity
37
44
return count ;
38
45
}
39
46
@@ -111,10 +118,14 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<
111
118
}
112
119
}
113
120
121
+ var childrenByParent = solution . SolutionItems
122
+ . GroupBy ( item => item . Parent )
123
+ . ToDictionary ( g => g . Key , g => g . ToList ( ) ) ;
124
+
114
125
Dictionary < SolutionFolderModel , int > nonFolderDescendantsCount = new ( ) ;
115
126
foreach ( var item in solution . SolutionFolders )
116
127
{
117
- CountNonFolderDescendants ( solution , item , nonFolderDescendantsCount ) ;
128
+ CountNonFolderDescendants ( item , nonFolderDescendantsCount , childrenByParent ) ;
118
129
}
119
130
120
131
var emptyFolders = nonFolderDescendantsCount . Where ( i => i . Value == 0 ) . Select ( i => i . Key ) ;
0 commit comments