Skip to content

[ILLink] Avoid exponential walks over deep interface hierarchies - #136767

Open
ramezgerges wants to merge 1 commit into
dotnet:mainfrom
ramezgerges:illink-interface-recursion
Open

ramezgerges wants to merge 1 commit into
dotnet:mainfrom
ramezgerges:illink-interface-recursion

Conversation

@ramezgerges

Copy link
Copy Markdown

Fixes #136766

An interface's metadata lists every interface it inherits, so in a deep chain the same interface is reachable through exponentially many paths. Three walks followed every one of them:

  • TypeMapInfo.FindAndAddDefaultInterfaceImplementations now carries a visited set. Each interface is searched once per lookup. Whether an interface provides the default implementation, or has its bases searched, depends only on the interface and the method, not on the path that reached it.
  • TypeMapInfo.AddRecursiveInterfaces now carries an expanded set (same TypeReferenceEqualityComparer as the result list, so generic instantiations stay distinct). An interface's first expansion already adds everything below it, with the first chain to each, which is the one the list keeps.
  • MarkStep.MarkBaseMethods only recurses into a base method it newly marked. An already-marked base gets its own bases marked when ProcessMethod processes it.

The real-world trigger is COM interop generated by [GeneratedComInterface] for versioned interfaces. For example, WebView2Aot's ICoreWebView2_29 : ICoreWebView2_28, … makes every trimmed publish of an app that references it hang.

Test

Inheritance.Interfaces.RecursiveInterfaces.DeepInterfaceHierarchy: a 28-deep chain plus an interface deriving from the top one. Mapping the assembly's types is enough to hit all of it. It passes in about 0.5 s with this change. Without it, the Inheritance.Interfaces suite hit a 3-minute --blame-hang-timeout.

Validation

  • build.cmd -s tools.illink+tools.illinktests -c Release -restore -build -test:
    • Mono.Linker.Tests: 1307 passed, 0 failed, 30 skipped.
    • ILLink.Tasks.Tests: 296 passed.
    • ILLink.RoslynAnalyzer.Tests couldn't run locally: every case fails with DirectoryNotFoundException for artifacts/bin/ILLink.RoslynAnalyzer.Tests/Release/net11.0/live-ref-pack, which a tools-only build doesn't produce.
  • Timings (stock vs this change, ILLink run directly, same patch applied to the v10.0.10 sources):
    • plain chain + leaf interface, depth 24: 20.8 s → 2.6 s;
    • [GeneratedComInterface] chain with 10 methods per level: depth 20 > 240 s → 3.5 s, depth 29 hang → 5.4 s;
    • WebView2.dll rooted: > 300 s → 9 s.
  • Output is unchanged where stock ILLink completes. A trimmed publish of a desktop app (95 assemblies) and the depth-16 generated-COM library produced byte-identical assemblies with and without this change.

An interface's metadata lists every interface it inherits, so in a deep
chain the same interface is reachable through exponentially many paths.
Three walks followed all of them:

- TypeMapInfo.FindAndAddDefaultInterfaceImplementations, when looking
  for a default implementation of an interface method;
- TypeMapInfo.AddRecursiveInterfaces, when collecting a type's
  recursive interfaces;
- MarkStep.MarkBaseMethods, when marking the bases of a base method
  that was already marked (ProcessMethod marks those bases itself).

Each now visits an interface (or already-marked base method) once.
Trimming an app with a 29-deep [GeneratedComInterface] chain, such as
the WebView2Aot interop library, no longer hangs.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ramezgerges
ramezgerges requested a review from sbomer as a code owner October 31, 2026 23:19
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Oct 31, 2026
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Oct 31, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 31, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ILLink takes exponential time on deep interface hierarchies

1 participant