Repository navigation
Avoid reflection delegate creation in FromEvent producers when a conversion is supplied - #2386
Open
Sergio0694 wants to merge 1 commit into
Open
Sergio0694 wants to merge 1 commit into
Sergio0694 wants to merge 1 commit into
Conversation
…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>
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enhancement
FromEvent<TDelegate, TEventArgs>andFromEventPattern.Impl<TDelegate, TEventArgs>had a_conversion == nullbranch that creates the handler viaMethodInfo.CreateDelegate. That branch is only taken by the overloads without aconversionargument, but sinceGetHandleris virtual on a constructed type, NativeAOT always compiled it, even for apps that only use theconversionoverloads (which never hit it).This matters for binary size: ILC has a rule that every delegate type's
Invokemethod 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 delegateInvokethrough reflection, such asMethodInfo.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
conversionrequired in these producers and removes the null branch. The no-conversion entry points inQueryLanguagenow pass astaticlambda that does the same reflection call, so it is only compiled when those overloads are used.FromEventPattern.Impl<TDelegate, TSender, TEventArgs>andHandler<,,>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
conversionoverloads:ReflectionUtilsandMethodInfo.CreateDelegateare compiled before this change and absent after it. ExistingFromEvent,FromEventPattern,Qbservable,ArgumentValidationandToEventtests pass (257 on net8.0).