Skip to content

Commit da2ded3

Browse files
live1206Copilot
andcommitted
perf(csharp): pre-write internalize provider reference map
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4886251 commit da2ded3

6 files changed

Lines changed: 394 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: internal
3+
packages:
4+
- "@typespec/http-client-csharp"
5+
---
6+
7+
Improve C# generator post-processing performance by using provider-based reference-map construction with pre-write internalization.

‎packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/CSharpGen.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public async Task ExecuteAsync()
3737
{
3838
CodeModelGenerator.Instance.Emitter.Info("Starting code generation");
3939
CodeModelGenerator.Instance.Stopwatch.Start();
40+
ProviderReferenceMapAnalyzer.ResetPreWriteAccessibility();
4041

4142
var outputPath = CodeModelGenerator.Instance.Configuration.OutputDirectory;
4243
var generatedSourceOutputPath = CodeModelGenerator.Instance.Configuration.ProjectGeneratedDirectory;
@@ -100,7 +101,12 @@ await GeneratedCodeWorkspace.LoadBaselineContract(),
100101
{
101102
// Ensure back-compatibility processing is done after all visitors have run
102103
outputType.ProcessTypeForBackCompatibility();
104+
}
105+
106+
generatedCodeWorkspace.ApplyPreWriteAccessibility(output.TypeProviders);
103107

108+
foreach (var outputType in output.TypeProviders)
109+
{
104110
var writer = CodeModelGenerator.Instance.GetWriter(outputType);
105111
generateFilesTasks.Add(generatedCodeWorkspace.AddGeneratedFile(writer.Write()));
106112

‎packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/PostProcessing/GeneratedCodeWorkspace.cs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ internal void AnalyzeProviderReferenceMap(IReadOnlyList<TypeProvider> providers)
8989
ProviderReferenceMapAnalyzer.Analyze(providers, _project);
9090
}
9191

92+
internal void ApplyPreWriteAccessibility(IReadOnlyList<TypeProvider> providers)
93+
{
94+
ProviderReferenceMapAnalyzer.ApplyPreWriteAccessibility(providers, _project);
95+
}
96+
9297
private async Task UpdateProject(Document document)
9398
{
9499
var root = await document.GetSyntaxRootAsync();
@@ -284,10 +289,16 @@ public async Task PostProcessAsync()
284289
case Configuration.UnreferencedTypesHandlingOption.KeepAll:
285290
break;
286291
case Configuration.UnreferencedTypesHandlingOption.Internalize:
287-
_project = await postProcessor.InternalizeAsync(_project);
292+
if (!ProviderReferenceMapAnalyzer.PreWriteAccessibilityApplied)
293+
{
294+
_project = await postProcessor.InternalizeAsync(_project);
295+
}
288296
break;
289297
case Configuration.UnreferencedTypesHandlingOption.RemoveOrInternalize:
290-
_project = await postProcessor.InternalizeAsync(_project);
298+
if (!ProviderReferenceMapAnalyzer.PreWriteAccessibilityApplied)
299+
{
300+
_project = await postProcessor.InternalizeAsync(_project);
301+
}
291302
_project = await postProcessor.RemoveAsync(_project);
292303
break;
293304
}

0 commit comments

Comments
 (0)