Skip to content

Simplify OptimizeGeneratedCodeHandler to speed up trimming #11477

Closed
3 of 3 issues completed
Closed

Description

@marek-safar

Existing OptimizeGeneratedCodeHandler logic duplicates a lot of processing and logic which is done as part of core IL linker steps. The code can be significantly simplified if migrated to use .NET6 features.

A proposal about how this can be done for each of the optimizations

ProcessCalls

EnsureUIThread

Convert field into property returning constant or add feature switch to set the value.

get_Size

Remove completely as it's done by linker by default.

get_IsDirectBinding

Set the constant via https://github.com/mono/linker/blob/main/src/linker/ref/Linker/Annotations.cs#L27-L28 for any override which should return true/false

get_DynamicRegistrationSupported

Remove completely, linker propagates the value already

SetupBlock and SetupBlockUnsafe

This would be much better done with source generator and can probably stay for now as it has nothing to do with linker.

ProcessLoadStaticField

IsARM64CallingConvention

I'm not sure why it's needed as field if we build arm64 specific runtime pack.

Arch

Should be a feature switch with which substitutes field

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    EnsureUIThread
    Convert field into property returning constant

    Binary breaking change.

    or add feature switch to set the value.

    yes, that's was my plan.

  2. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    get_IsDirectBinding
    Set the constant via https://github.com/mono/linker/blob/main/src/linker/ref/Linker/Annotations.cs#L27-L28 for any override which should return true/false

    Can you explain how that works ?

    IsDirectBinding is not a global constant. The value can be different for each NSObject subclass. In some cases (like if we know there's no subclasses for the type) this can be optimized. Otherwise the check must remain.

  3. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    get_DynamicRegistrationSupported
    Remove completely, linker propagates the value already

    This value is true for the original, compile code. However if/when we can remove the dynamic registrar (and drop a lot of code by doing so) then IIRC its updated to return false.

  4. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    IsARM64CallingConvention
    I'm not sure why it's needed as field if we build arm64 specific runtime pack.

    The (Xamarin.iOS) managed code has to call different p/invoke signatures based on the ABI.

  5. added
    dotnet
    dotnet-pri2
    on May 7, 2021
  6. added this to the .NET 6 milestone on May 7, 2021
  7. marek-safar commented on May 7, 2021

    @marek-safar
    ContributorAuthor

    The value can be different for each NSObject subclass. In some cases (like if we know there's no subclasses for the type) this can be optimized.

    For those which you know it can be constant, you register the method with Annotations.SetAction (method, MethodAction. ConvertToStub) and set the value via Annotations.SetStubValue (method, 1)

    However if/when we can remove the dynamic registrar

    It was not clear to me if this should be configurable in net6 if so you keep it as it and add feature switch on top of it

    managed code has to call different p/invoke signatures based on the ABI.

    Right but it's already constant https://github.com/xamarin/xamarin-macios/blob/2b7dc07d20fc06344d7f164af8934f5e755572c2/src/ObjCRuntime/Runtime.cs#L1833 and if we add feature switch for Arch linker will automatically evaluate the method as constant returning.

  8. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    For those which you know it can be constant, you register the method with Annotations.SetAction (method, MethodAction. ConvertToStub) and set the value via Annotations.SetStubValue (method, 1)

    I might be missing something obvious here but which method are you talking about in your code snippet ?

    Example: Let's say that NSData.Foo and NSString.Bar have calls to the IsDirectBinding property.

    By analyzing the app we know that

    • NSMutableData is used and that it subclass NSData so we cannot optimize/remove the call to IsDirectBinding;
    • NSString has no subclass (used by the app) so it does not need to call IsDirectBinding

    So what's the method to be used ?

    • it can't be NSData.Foo as it can not be optimized (would break the subclasses);
    • NSString.Bar as it can be optimized ? but how does it know it's IsDirectBinding that can be optimized (and other other properties inside the method) ?
    • NSObject.IsDirectBinding property ? it cannot be since optimizing this into NSObject would also apply to NSData ?
  9. spouliot commented on May 7, 2021

    @spouliot
    Contributor

    or add feature switch to set the value.

    yes, that's was my plan.

    The easy/initial (not exhaustive) candidates for substitutions I identified are

    API Stubbed on
    ObjCRuntime.Runtime::RegisterEntryAssembly [1] Device
    UIKit.UIButton::VerifyIsUIButton [2] Release
    UIKit.UIApplication::EnsureUIThread Release [3]
    UIKit.UIApplication::EnsureEventAndDelegateAreNotMismatched Release [4]
    UIKit.UIApplication::EnsureDelegateAssignIsNotOverwritingInternalDelegate Release [4]

    [1] #10457
    [2] #9613
    [3] can be overridden (legacy) with optional arguments
    [4] were not removed by legacy linker

  10. marek-safar commented on May 7, 2021

    @marek-safar
    ContributorAuthor

    You could consider guarding some of them under the existing System.Diagnostics.Debugger.IsSupported feature switch which is unset for Release build.

  11. marek-safar commented on May 10, 2021

    @marek-safar
    ContributorAuthor

    I might be missing something obvious here but which method are you talking about in your code snippet ?

    This will probably need a different approach than I thought. Based on your description it sounds that IsDirectBinding is another version of typeof (Foo) != GetType () comparison, right? If that's the case I believe the IL Linker will be able to understand that condition soon.

  12. added
    enhancement
    on Mar 29, 2022
  13. 7 remaining items

  14. moved this to Technical Debt in .NET 9on Aug 27, 2024
  15. modified the milestones: .NET 9, .NET 10 on Sep 27, 2024
  16. modified the milestones: .NET 10, .NET 11 on Oct 22, 2025
  17. rolfbjarne commented on Jul 15, 2026

    @rolfbjarne
    Member

    I had a look with the current state of the steps:

    • EnsureUIThread: convert to feature switch
    • get_Size: already gone (c098811)
    • IsDirectBinding: has to stay, too complex to use a feature switch
    • DynamicRegistrationSupported: already converted to feature switch (bf5e35a)
    • BlockLiteral.SetupBlock[Unsafe]: has to stay, too complex for a feature switch, and can't use a source generator because we have to work with existing binaries (NuGets).
    • BlockLiteral.ctor: has to stay, too complex for a feature switch, and can't use a source generator because we have to work with existing binaries (NuGets).
    • IsARM64CallingConvention: convert to feature switch
    • Runtime.Arch: convert to feature switch
  18. rolfbjarne commented on Aug 6, 2026

    @rolfbjarne
    Member

    All we can do is done now.

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

    dotnetenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions