Skip to content

Add trimming switch to restrict Linq Expressions to Native AOT delegate types (alternative to #135372) - #137444

Closed
davidwrighton wants to merge 1 commit into
dotnet:mainfrom
davidwrighton:linq-expressions-aot-delegate-switch
Closed

davidwrighton wants to merge 1 commit into
dotnet:mainfrom
davidwrighton:linq-expressions-aot-delegate-switch

Conversation

@davidwrighton

Copy link
Copy Markdown
Member

This is an alternative to #135372 (prototype runtime delegate-type factory). Rather than adding a restricted runtime surface for custom delegate creation, this adds a trimming feature switch that limits System.Linq.Expressions to the same delegate types that are supported on Native AOT.

Change

  • New feature switch System.Linq.Expressions.CanEmitCustomDelegateTypes (default true, [FeatureSwitchDefinition]).
  • When set to false, delegate type inference (Expression.GetDelegateType, inferred-type Expression.Lambda, call-site delegates) only uses Func<...>, Action<...>, and the predefined call-site delegates. Requests that would require a custom delegate type (more than 16 parameters, or by-ref, by-ref-like, or pointer parameters) throw PlatformNotSupportedException, matching Native AOT behavior. The Reflection.Emit-based custom delegate generation (AssemblyGen/TypeBuilder) becomes trimmable.
  • Compiling lambdas to IL is unaffected.
  • No native/runtime changes.
  • Documented in docs/workflow/trimming/feature-switches.md. An app opts in with:
<RuntimeHostConfigurationOption Include="System.Linq.Expressions.CanEmitCustomDelegateTypes" Value="false" Trim="true" />

A follow-up could have the Blazor WebAssembly SDK set this by default, since Blazor app expression usage generally uses explicit or Func/Action-shaped delegate types (see below).

Size analysis: Havit Blazor documentation app (CoreCLR WebAssembly, composite R2R)

All publishes used the same SDK (12.0.100-alpha.1.26480.102), Release CoreCLR browser runtime, and composite R2R publish. Sizes are the sum of all published .br files (94 files in each publish).

Publish Total .br bytes Delta
This PR, switch at default (true) 8,550,156 —
This PR, switch false 8,512,728 −37,428 (−0.44%)
#135372 prototype (for comparison, vs. its matched upstream baseline) 8,515,093 −33,901 vs. baseline

Breakdown of switch false vs. default:

Asset Default Switch false Delta
Composite R2R image (Havit.Blazor.Documentation.r2r) 4,056,724 4,033,037 −23,687
System.Private.CoreLib 776,388 765,079 −11,309
System.Linq.Expressions 115,467 114,693 −774
dotnet.native.wasm 1,327,793 1,327,793 0

Notes:

  • With the switch off, AssemblyGen is no longer present in the published System.Linq.Expressions assembly.
  • dotnet.native.wasm is byte-identical between the two configurations. By comparison, the Prototype: allocator-scoped runtime delegate-type factory (not for merge) #135372 prototype's new QCalls add +375 bytes uncompressed / +466 bytes Brotli to dotnet.native.wasm (measured from the same checkout path).
  • Against an upstream baseline built in a different directory, dotnet.native.wasm differs by ~555 Brotli bytes purely due to embedded __FILE__ source paths; with that removed, switch false saves ~36.8 KB vs. upstream.
  • Both configurations loaded successfully in Chromium. These are size measurements only; no startup timing conclusions are drawn.

Does Havit/Blazor need custom delegate types?

I scanned the IL of the published app dependencies for Expressions usage:

  • Microsoft.AspNetCore.Components.Forms/.Web and Havit.Blazor.Components.Web.Bootstrap use Expression.Lambda<TDelegate> with explicit Func<...> types, or Expression.Lambda(Type, ...).
  • Havit.Core (ExpressionExt.AndAlso, SubstitutionVisitor.VisitLambda) uses inferred-type Expression.Lambda, but for predicate shapes that map to Func<T, bool>.
  • Microsoft.CSharp is not published, so there are no dynamic call-site binders.

So the custom delegate path is reachable by the trimmer but not needed at runtime in this scenario.

Validation

  • ./build.sh clr+libs -rc Release — passed.
  • dotnet build /t:Test src/libraries/System.Linq.Expressions/tests/System.Linq.Expressions.Tests.csproj /p:RuntimeConfiguration=Release — passed: 35,119 total, 0 failed, 0 skipped, including the new GetDelegateTypeTests.CustomDelegateTypesNotEmittedWhenSwitchDisabled (RemoteExecutor with the switch disabled).
  • Browser CoreCLR runtime/packs built and Havit published/loaded with the switch on and off.
  • Not tested: Mono, NativeAOT (behavior there is unchanged since IsDynamicCodeSupported is already false).

Note

This PR description and change were generated with assistance from GitHub Copilot.

…te types

System.Linq.Expressions.CanEmitCustomDelegateTypes=false makes delegate type
inference use the same predefined delegate types as Native AOT, so the
Reflection.Emit-based custom delegate type generation can be trimmed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 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: @333fred
See info in area-owners.md if you want to be subscribed.

@jkoritzinsky

Copy link
Copy Markdown
Member

Can you get numbers for a relinked dotnet.native.wasm? If we're able to trim out all of the managed SRE logic then we should be able to trim some of the native logic too

@davidwrighton

Copy link
Copy Markdown
Member Author

@jkoritzinsky, it's not a that big of a delta unless we can drop the entire RW metadata handling logic, which isn't going to happen easily. There are too many tendrils.

@davidwrighton

Copy link
Copy Markdown
Member Author

I'm going to close this for now. While I think this is better than changing Linq expressions to have a completely different delegate creation path, it is still not quite worth making a non-trivial change to System.Linq.Expressions given its archived state.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants