Skip to content

Commit 6c7cb26

Browse files
committed
Better split of code
1 parent 49dffe0 commit 6c7cb26

File tree

1 file changed

+54
-58
lines changed

1 file changed

+54
-58
lines changed

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff/MemoryOutputDiffGenerator.cs

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -90,85 +90,81 @@ public async Task RunAsync()
9090
{
9191
Stopwatch swRun = Stopwatch.StartNew();
9292

93-
// Needs to block so the _afterAssemblySymbols dictionary gets updated.
94-
await ProcessBeforeAndAfterAssembliesAsync().ConfigureAwait(false);
93+
foreach ((string beforeAssemblyName, IAssemblySymbol beforeAssemblySymbol) in _beforeAssemblySymbols)
94+
{
95+
// Needs to block so the _afterAssemblySymbols dictionary gets updated.
96+
await ProcessBeforeAndAfterAssemblyAsync(beforeAssemblyName, beforeAssemblySymbol).ConfigureAwait(false);
97+
}
9598

9699
// Needs to happen after processing the before and after assemblies and filtering out the existing ones.
97-
await ProcessNewAssembliesAsync().ConfigureAwait(false);
100+
foreach ((string afterAssemblyName, IAssemblySymbol afterAssemblySymbol) in _afterAssemblySymbols)
101+
{
102+
await ProcessNewAssemblyAsync(afterAssemblyName, afterAssemblySymbol).ConfigureAwait(false);
103+
}
98104

99105
_log.LogMessage($"FINAL TOTAL TIME: {swRun.Elapsed.TotalMilliseconds / 1000.0 / 60.0:F2} mins.");
100106
swRun.Stop();
101107
}
102108

103-
private async Task ProcessBeforeAndAfterAssembliesAsync()
109+
private async Task ProcessBeforeAndAfterAssemblyAsync(string beforeAssemblyName, IAssemblySymbol beforeAssemblySymbol)
104110
{
105-
foreach ((string beforeAssemblyName, IAssemblySymbol beforeAssemblySymbol) in _beforeAssemblySymbols)
106-
{
107-
StringBuilder sb = new();
108-
109-
_log.LogMessage($"Visiting assembly {beforeAssemblySymbol.Name} (before vs after)...");
110-
111-
Stopwatch swAssembly = Stopwatch.StartNew();
112-
(SyntaxNode beforeAssemblyNode, SemanticModel beforeModel) = await GetAssemblyRootNodeAndModelAsync(_beforeLoader, beforeAssemblySymbol).ConfigureAwait(false);
113-
string finalMessage = $"Finished visiting {beforeAssemblySymbol.Name} (before) in {swAssembly.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
111+
StringBuilder sb = new();
114112

115-
// See if an assembly with the same name can be found in the diff folder.
116-
if (_afterAssemblySymbols.TryGetValue(beforeAssemblyName, out IAssemblySymbol? afterAssemblySymbol))
117-
{
118-
swAssembly = Stopwatch.StartNew();
119-
(SyntaxNode afterAssemblyNode, SemanticModel afterModel) = await GetAssemblyRootNodeAndModelAsync(_afterLoader, afterAssemblySymbol).ConfigureAwait(false);
120-
finalMessage += $"Finished visiting {afterAssemblySymbol.Name} (after) in {swAssembly.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
113+
_log.LogMessage($"Visiting assembly {beforeAssemblySymbol.Name} (before vs after)...");
121114

122-
// We don't care about changed assembly attributes (for now).
123-
swAssembly = Stopwatch.StartNew();
124-
sb.Append(VisitChildren(beforeAssemblyNode, afterAssemblyNode, beforeModel, afterModel, ChangeType.Unchanged, wereParentAttributesChanged: false));
125-
finalMessage += $"Finished generating diff for {afterAssemblySymbol.Name} (before vs after) in {swAssembly.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
115+
Stopwatch swAssembly = Stopwatch.StartNew();
116+
(SyntaxNode beforeAssemblyNode, SemanticModel beforeModel) = await GetAssemblyRootNodeAndModelAsync(_beforeLoader, beforeAssemblySymbol).ConfigureAwait(false);
117+
string finalMessage = $"Finished visiting {beforeAssemblySymbol.Name} (before) in {swAssembly.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
126118

127-
_log.LogMessage(finalMessage);
128-
swAssembly.Stop();
119+
// See if an assembly with the same name can be found in the diff folder.
120+
if (_afterAssemblySymbols.TryGetValue(beforeAssemblyName, out IAssemblySymbol? afterAssemblySymbol))
121+
{
122+
string afterAssemblyString = await ProcessAfterAssemblyAsync(afterAssemblySymbol, beforeAssemblyNode, beforeModel).ConfigureAwait(false);
123+
sb.Append(afterAssemblyString);
129124

130-
// Remove the found ones. The remaining ones will be processed at the end because they're new.
131-
_afterAssemblySymbols.Remove(beforeAssemblyName, out _);
132-
}
133-
else
134-
{
135-
// The assembly was removed in the diff.
136-
//sb.Append(GenerateDeletedDiff(beforeAssemblyNode));
137-
sb.Append(VisitChildren(beforeAssemblyNode, afterParentNode: null, beforeModel, afterModel: null, ChangeType.Deleted, wereParentAttributesChanged: false));
138-
}
125+
// Remove the found ones. The remaining ones will be processed at the end because they're new.
126+
_afterAssemblySymbols.Remove(beforeAssemblyName, out _);
127+
}
128+
else
129+
{
130+
// The assembly was removed in the diff.
131+
//sb.Append(GenerateDeletedDiff(beforeAssemblyNode));
132+
sb.Append(VisitChildren(beforeAssemblyNode, afterParentNode: null, beforeModel, afterModel: null, ChangeType.Deleted, wereParentAttributesChanged: false));
133+
}
139134

140-
if (sb.Length > 0)
141-
{
142-
_results.TryAdd(beforeAssemblyName, GetFinalAssemblyDiff(beforeAssemblyName, sb.ToString()));
143-
}
135+
if (sb.Length > 0)
136+
{
137+
_results.TryAdd(beforeAssemblyName, GetFinalAssemblyDiff(beforeAssemblyName, sb.ToString()));
144138
}
145139
}
146140

147-
private async Task ProcessNewAssembliesAsync()
141+
private async Task ProcessNewAssemblyAsync(string afterAssemblyName, IAssemblySymbol afterAssemblySymbol)
148142
{
149-
// Process the remaining assemblies in the diff folder.
150-
foreach ((string afterAssemblyName, IAssemblySymbol afterAssemblySymbol) in _afterAssemblySymbols)
143+
string afterAssemblyString = await ProcessAfterAssemblyAsync(afterAssemblySymbol).ConfigureAwait(false);
144+
if (afterAssemblyString.Length > 0)
151145
{
152-
StringBuilder sb = new();
153-
_log.LogMessage($"Visiting *new* assembly {afterAssemblySymbol.Name}...");
146+
_results.TryAdd(afterAssemblyName, GetFinalAssemblyDiff(afterAssemblyName, afterAssemblyString));
147+
}
148+
}
154149

155-
var swNew = Stopwatch.StartNew();
156-
(SyntaxNode afterAssemblyNode, SemanticModel afterModel) = await GetAssemblyRootNodeAndModelAsync(_afterLoader, afterAssemblySymbol).ConfigureAwait(false);
157-
string finalMessage = $"Finished visiting *new* {afterAssemblySymbol.Name} in {swNew.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
150+
private async Task<string> ProcessAfterAssemblyAsync(IAssemblySymbol afterAssemblySymbol, SyntaxNode? beforeParentNode = null, SemanticModel? beforeModel = null)
151+
{
152+
StringBuilder sb = new();
153+
var swNew = Stopwatch.StartNew();
154+
(SyntaxNode afterAssemblyNode, SemanticModel afterModel) = await GetAssemblyRootNodeAndModelAsync(_afterLoader, afterAssemblySymbol).ConfigureAwait(false);
155+
string assemblyDescriptor = beforeParentNode == null ? "new" : "existing";
156+
string finalMessage = $"Finished visiting *{assemblyDescriptor}* assembly {afterAssemblySymbol.Name} in {swNew.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
158157

159-
// We don't care about changed assembly attributes (for now).
160-
swNew = Stopwatch.StartNew();
161-
sb.Append(VisitChildren(beforeParentNode: null, afterAssemblyNode, beforeModel: null, afterModel, ChangeType.Inserted, wereParentAttributesChanged: false));
162-
finalMessage += $"Finished generating diff for *new* assembly {afterAssemblySymbol.Name} in {swNew.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
158+
swNew = Stopwatch.StartNew();
159+
ChangeType parentChangeType = beforeParentNode == null ? ChangeType.Inserted : ChangeType.Unchanged;
160+
// We don't care about changed assembly attributes (for now).
161+
sb.Append(VisitChildren(beforeParentNode, afterAssemblyNode, beforeModel, afterModel, parentChangeType, wereParentAttributesChanged: false));
162+
finalMessage += $"Finished generating diff for *{assemblyDescriptor}* assembly {afterAssemblySymbol.Name} in {swNew.Elapsed.TotalMilliseconds / 1000.0:F2}s{Environment.NewLine}";
163163

164-
_log.LogMessage(finalMessage);
165-
swNew.Stop();
164+
_log.LogMessage(finalMessage);
165+
swNew.Stop();
166166

167-
if (sb.Length > 0)
168-
{
169-
_results.TryAdd(afterAssemblyName, GetFinalAssemblyDiff(afterAssemblyName, sb.ToString()));
170-
}
171-
}
167+
return sb.ToString();
172168
}
173169

174170
private async Task<(SyntaxNode rootNode, SemanticModel model)> GetAssemblyRootNodeAndModelAsync(IAssemblySymbolLoader loader, IAssemblySymbol assemblySymbol)
@@ -317,7 +313,7 @@ private static ConcurrentDictionary<string, MemberDeclarationSyntax> CollectChil
317313

318314
if (parentNode is RecordDeclarationSyntax record && record.Members.Any())
319315
{
320-
foreach (MemberDeclarationSyntax memberNode in record.ChildNodes().Where(n => n is MemberDeclarationSyntax m && IsPublicOrProtectedOrDestructor(m)).Cast<MemberDeclarationSyntax>())
316+
foreach (MemberDeclarationSyntax memberNode in record.ChildNodes().Where(n => n is MemberDeclarationSyntax m && IsPublicOrProtectedOrDestructor(m)).Cast<MemberDeclarationSyntax>())
321317
{
322318
// Note that these could also be nested types
323319
dictionary.TryAdd(GetDocId(memberNode, model), memberNode);

0 commit comments

Comments
 (0)