Skip to content

Avoid reflection delegate creation in FromEvent producers when a conversion is supplied - #2386

Open
Sergio0694 wants to merge 1 commit into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fromevent-avoid-reflection-delegates
Open

Sergio0694 wants to merge 1 commit into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fromevent-avoid-reflection-delegates

Conversation

@Sergio0694

Copy link
Copy Markdown

Enhancement

FromEvent<TDelegate, TEventArgs> and FromEventPattern.Impl<TDelegate, TEventArgs> had a _conversion == null branch that creates the handler via MethodInfo.CreateDelegate. That branch is only taken by the overloads without a conversion argument, but since GetHandler is virtual on a constructed type, NativeAOT always compiled it, even for apps that only use the conversion overloads (which never hit it).

This matters for binary size: ILC has a rule that every delegate type's Invoke method must be fully reflectable, which is expensive (~861 KB in a large app). That rule only needs to apply when some reachable code can actually use delegate Invoke through reflection, such as MethodInfo.CreateDelegate. This dead branch was the only such code reachable in our app, so it was blocking a planned ILC change that would let that metadata be trimmed.

The fix makes conversion required in these producers and removes the null branch. The no-conversion entry points in QueryLanguage now pass a static lambda that does the same reflection call, so it is only compiled when those overloads are used. FromEventPattern.Impl<TDelegate, TSender, TEventArgs> and Handler<,,> are unchanged, as they always use reflection and are only reachable from reflection-based overloads.

No public API or behavior changes, and no extra per-subscription allocations. Verified with a NativeAOT app using only the conversion overloads: ReflectionUtils and MethodInfo.CreateDelegate are compiled before this change and absent after it. Existing FromEvent, FromEventPattern, Qbservable, ArgumentValidation and ToEvent tests pass (257 on net8.0).

…ersion is supplied

Make the conversion function required in FromEvent<TDelegate, TEventArgs> and
FromEventPattern.Impl<TDelegate, TEventArgs>, removing the runtime null check
that fell back to ReflectionUtils.CreateDelegate. The no-conversion entry points
in QueryLanguage now pass a static conversion that performs the reflection-based
delegate creation, so NativeAOT only compiles that path when those overloads
are actually used.

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

Copy link
Copy Markdown
Author

Hey @idg10, I've been doing some investigations into binary size on Native AOT and this was a blocker to allow ILC to trim out all the Invoke metadata for delegate types. Combined with an ILC fix to do that, it saves over 800KB of size in the Microsoft Store. There's no public API changes and the behavior is the same. Could you help review this one? Thank you! 🙂

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址