Skip to content

[wasm][mono][AOT] Polymorphic recursion over struct type arguments makes the aot-instances pass take 1-2 hours (gsharedvt wrapper normalization); request 10.0 backport of #135334 if it covers this #137919

Open

Description

@LostBeard

Description

A browser-wasm AOT publish (RunAOTCompilation=true, default WasmDedup=true) spends one to two hours in the single-threaded aot-instances.dll pass of mono-aot-cross when a generic method recurses into itself with its struct type arguments wrapped one level deeper:

static void MapInstance<TSource, TTarget>(in TSource source, in TTarget target)   // TSource, TTarget : struct
{
    foreach (var f in ...)
        MapInstance(new StructureSource<TSource>(source, f), new StructureTarget<TTarget>(target, f));
}

At run time the depth is bounded by real data (2 in the repro). AOT cannot know that, so the chain is instantiated up to the generic depth limit, and every level needs gsharedvt wrappers. The same app with the recursion made non-generic publishes in about a minute. The output is equivalent, and both builds run correctly.

Reproduction

Standalone repro, no third-party code: https://github.com/LostBeard/mono-wasm-aot-gsharedvt-recursion

  • ON: dotnet publish src/Repro.csproj -c Release
  • OFF: dotnet publish src/Repro.csproj -c Release -p:NoPolymorphicRecursion=true (the only difference is the #if NOREC blocks in src/Program.cs)

GitHub Actions measures both arms on .NET 9, 10 and 11 (ubuntu-24.04), and the results page also hosts the built apps: https://lostbeard.github.io/mono-wasm-aot-gsharedvt-recursion/

Framework MonoAOTCompiler task, ON OFF Publish wall time, ON OFF aot-instances compiled, ON / OFF
net9.0 6,497 s 5.2 s 109 min 82 s 7,462 / 7,252
net10.0 (10.0.15) 6,400 s 5.2 s 108 min 54 s 9,131 / 8,921
net11.0 (11.0.0) 4,797 s 6.2 s 81 min 64 s 9,043 / 8,833

On Windows with 10.0.14 locally: 8,050 s ON vs 5.15 s OFF. The recursion adds about 210 instances and more than an hour of AOT.

Where the time goes

I profiled the aot-instances pass of a real application with this shape (ILGPU's ArgumentMapper). I used a stack sampler and the public PDB for mono-aot-cross.exe 10.0.14, with 100 ms samples over an 84-minute pass. In every 10-minute window, 94-100% of samples were under:

compile_methods
  compile_method
    add_gsharedvt_wrappers
      mini_get_gsharedvt_in_sig_wrapper
        mini_get_underlying_signature
          get_wrapper_shared_type_full <-> get_wrapper_shared_vtype     (12-17 levels deep per sample, max 22)
            mono_class_inflate_generic_class_checked                     (~68% inclusive)
              mono_metadata_inflate_generic_inst / type_dup / malloc / free

get_wrapper_shared_vtype maps every field type through get_wrapper_shared_type_full, which maps every generic argument back through get_wrapper_shared_vtype. Nothing is cached, so a nested struct is re-expanded and re-inflated at every level, for every signature that contains it. The gsharedvt wrapper signatures in that app's aot-instances.dll nest Mono.ValueTuple 8 to 31 levels deep.

Mono's own counters for that pass were 19.2 ms per method in the emit phase (54,728 instances in 1,049 s of JIT). The 105 ordinary per-assembly passes of the same app ran at 0.54 ms per method. The collect phase, which prints no timing, took most of the remaining ~73 minutes.

Relation to #135029 / #135438 / #135334, and a request

#135029 / #135438 (merged to main) add a parent-frame chain with cycle detection and a depth limit of 16 to get_wrapper_shared_vtype, to fix a stack overflow. #135334 backported it to release/11.0 on 2026-11-16, after 11.0.0, so it should ship in 11.0.1. There is no backport to release/10.0.

I could not measure whether that depth limit also bounds this cost:

  • 10.0.x and 11.0.0 do not contain it.
  • The daily .NET 11 SDKs still bundle runtime 11.0.0-rc.2.26508.107.
  • .NET 12 dailies run browser-wasm on CoreCLR, so they do not run Mono AOT.

Requests:

  1. Could someone who can build mono-aot-cross from release/11.0 or main run the repro's ON arm? That would show whether [release/11.0] [mono][aot] Fix recursive struct wrapper normalization #135334 covers this.
  2. If it does, please consider backporting it to release/10.0. .NET 10 is LTS, and this makes browser AOT publishes take hours. We hit it in production deploys: our ML demo's GitHub Pages deploy took 2 h 40 min, and its aot-instances pass alone took 89 min.
  3. If it does not, memoizing get_wrapper_shared_type_full / get_wrapper_shared_vtype results per type would remove the repeated expansion.

Workaround for affected users

Make the polymorphic recursion non-generic. For example, keep the root type as a generic argument and carry the nested field path as a plain array, which is the OFF arm of the repro. ILGPU did this in SpawnDev.ILGPU 5.4.38. On the same GitHub runner, that app's whole Mono AOT step, including aot-instances, now takes 1.4 minutes; the aot-instances pass alone used to take 89. The repro README lists how to tell whether a build is affected.

Environment

  • Windows 11, .NET SDK 10.0.403, runtime packs 10.0.14 (local).
  • GitHub Actions ubuntu-24.04: .NET 9.0 (SDK 9.0.321), 10.0 (cross compiler 10.0.15), 11.0 (11.0.0).
  • wasm-tools workload, RunAOTCompilation=true, WasmDedup default (true), trimming default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions