Skip to content

Investigate whether it's beneficial to share P/Invokes with similar signatures #16673

Open

Description

@rolfbjarne

Example:

Messaging.void_objc_msgSend_IntPtr_nuint(base.Handle, Selector.GetHandle("setTitle:forState:"), intPtr, (nuint)(ulong)P_1);

P_1 is a [Native] enum, which is an n[u]int so it can have a different size on 32/64 bits architectures.
An IntPtr can also do this job and since we're already using them in many places (handles) we have a better chance to share an existing signature for objc_msg[Super]Send
e.g. void_objc_msgSend_IntPtr_IntPtr already exists

Potential:

  • smaller platform assembly (minor impact)
  • smaller app bundles for large apps (minor impact)

Activity

  1. added
    generator
    on Nov 9, 2022
  2. added this to the .NET 8 milestone on Nov 9, 2022
  3. added
    enhancement
    on Nov 9, 2022
  4. rolfbjarne commented on Nov 9, 2022

    @rolfbjarne
    MemberAuthor

    I wonder if we can use function signatures to completely get rid of all the overloads...

    [DllImport ("libc")]
    static extern void objc_msgSend ();
    
    internal unsafe static class MessagingFunctions {
        static void *GetFunctionPointer ()
        {
            delegate* unmanaged<void> func = &objc_msgSend;
            return (void *) func;
        }
        
        static delegate* unmanaged<IntPtr> GetFunction_IntPtr ()
        {
            delegate* unmanaged<void> func = &objc_msgSend;
            return (delegate* unmanaged<IntPtr>) (void *) func;
        }
    }
    
    // and then called like:
    
    var msgSend = MessagingFunctions.GetFunction_IntPtr ();
    var handle = msgSend (this.Handle, Selector.GetHandle ("init"));
  5. Therzok commented on Nov 9, 2022

    @Therzok
    Contributor

    I think the latter proposal will work if all CPU architectures pass return values in registers, and not on the stack.

  6. rolfbjarne commented on Nov 9, 2022

    @rolfbjarne
    MemberAuthor

    We'd still need to call the right objc_msgSend[_stret|_fpret] function.

  7. modified the milestones: .NET 8, .NET 9 on Sep 11, 2023
  8. moved this to Optimizations in .NET 9on Aug 27, 2024
  9. modified the milestones: .NET 9, .NET 10 on Sep 26, 2024
  10. modified the milestones: .NET 10, .NET 11 on Oct 21, 2025
  11. modified the milestones: .NET 11, .NET 12 on Jul 21, 2026
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

    app-sizeenhancementgenerator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions