Skip to content

[http-client-csharp] Skip unnecessary simplifier passes#10846

Draft
live1206 wants to merge 17 commits into
microsoft:mainfrom
live1206:mtg-skip-unneeded-simplifier
Draft

[http-client-csharp] Skip unnecessary simplifier passes#10846
live1206 wants to merge 17 commits into
microsoft:mainfrom
live1206:mtg-skip-unneeded-simplifier

Conversation

@live1206
Copy link
Copy Markdown
Contributor

@live1206 live1206 commented Jun 1, 2026

Summary

Optimize generated C# post-processing by reducing provably safe qualified names before running the full Roslyn simplifier.

The new pass targets qualified names such as global::System.Uri / System.ClientModel.ClientResult and uses Roslyn speculative binding to replace them with the rightmost name only when the replacement binds to the same symbol in the same semantic model. Unsafe or unsupported cases are left for the normal Roslyn simplifier.

This keeps generated output stable while reducing the amount of semantic name-reduction work left for Simplifier.ReduceAsync.

Motivation

While profiling Azure.ResourceManager.Network generation through the Azure management generator, Roslyn post-processing dominated the direct .NET generator run time. The hotspot was semantic name simplification / global-qualified name reduction in the shared MTG GeneratedCodeWorkspace.ProcessDocument path.

The no-simplifier comparison showed that global:: / name simplification was by far the largest category: 98,190 diff blocks across 3,534 files. However, skipping or narrowing the simplifier is not a valid product fix because generated output/functionality can be missed or changed.

Latest Network direct-generator measurement

Validated against the same Network saved tspCodeModel.json / Configuration.json direct-generator path. A fresh no-reducer baseline and reducer run produced identical src/Generated output (diff -qr returned no differences).

Run Internal total Shell elapsed Notes
Fresh broad/member-span baseline without reducer 00:19:47.50 1190.835s Same saved Network inputs; output exactly matched reducer run.
This PR: targeted qualified-name reducer 00:14:04.68 847.959s Same saved Network inputs; output exactly matched fresh baseline.
Improvement ~5m43s ~343s Roughly 29% wall-clock reduction for the direct .NET generator phase.

Phase markers from the reducer run:

Phase marker Cumulative elapsed
All generated type providers built 00:00:03.94
All visitors applied 00:00:11.44
Generated types written into memory 00:00:17.66
Roslyn post-processing complete 00:14:02.90
All files written to disk 00:14:04.68

Output sanity check after the Network run:

Token Count Files
global::System 2 1
this. 31 6
<see cref="global:: 0 0

Implementation notes

  • The reducer runs after member removal and generator rewriters, then refreshes the syntax root before the existing simplifier step.
  • It considers outer QualifiedNameSyntax / AliasQualifiedNameSyntax nodes and skips unsupported contexts such as using directives and XML cref.
  • It compares the original symbol to the speculative replacement symbol using SymbolEqualityComparer.Default in the same semantic model.
  • It only replaces names that bind equivalently; everything else remains for the normal Roslyn simplifier.
  • The existing simplifier still runs over spans containing remaining reducible forms (this., parenthesized expressions, cref, qualified names, member access expressions, assignments, etc.).

Validation

cd packages/http-client-csharp
npm run build:generator
pwsh ./eng/scripts/Generate.ps1
npm run test:generator

Local validation results:

  • npm run build:generator passed.
  • pwsh ./eng/scripts/Generate.ps1 produced no generated test-project diffs.
  • npm run test:generator passed.
  • Network direct-generator profiling improved from 00:19:47.50 to 00:14:04.68 internal time on the same saved inputs, with no generated output differences versus the fresh baseline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jun 1, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jun 1, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10846

commit: 53124ea

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

No changes needing a change description found.

live1206 and others added 13 commits June 1, 2026 07:30
Run simplification for documents containing global aliases so shared-source type names and generated sample code are reduced correctly while still skipping documents that do not need simplification.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generate valid assignment statements without relying on Roslyn simplification, and keep shared-source test expectations aligned with the skip behavior so broad global-alias simplification is not reintroduced.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Refresh generated test projects and unit test expectations after skipping Roslyn simplification for documents without simplifier annotations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Avoid emitting a trailing space after array type syntax when the initializer is multiline. Regenerated outputs now match without manual generated-file whitespace edits.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use plain default for known cancellation token optional parameters so generated code remains valid when documents are not passed through Roslyn simplification.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Refresh remaining generator unit test baselines after removing trailing spaces from multiline array initializers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant