Skip to content

[RFC] Simplify block creation pattern #4543

Closed as not planned
Closed as not planned

Description

@rolfbjarne

The current pattern generated by the generator (and which we copy around liberally), goes something like this:

unsafe {
	BlockLiteral *block_ptr_handler;
	BlockLiteral block_handler;
	block_handler = new BlockLiteral ();
	block_ptr_handler = &block_handler;
	block_handler.SetupBlockUnsafe (blockCallback, userCallback);

	NativePInvoke ((void*) block_ptr_handler);
	block_ptr_handler->CleanupBlock ();
}

I think we can make it possible to simplify this a lot, so that we get down to:

using (var block = new BlockLiteral (blockCallback, userCallback, false))
    NativePInvoke (ref block);

This requires:

  • Make BlockLiteral implement IDisposable (and call CleanupBlock in Dispose).
  • Add a BlockLiteral constructor that takes the corresponding delegates.
  • Add support to the linker/optimizer for the new BlockLiteral constructor.
  • Change P/Invokes to take ref BlockLiteral instead of void * or IntPtr (this only works if the block isn't nullable).

Advantages

  • Much smaller & simpler C# code.
  • We won't leak the block if the P/Invoke (or any code after setting up the block) throws an exception.

Disadvantages

  • I think we'd end up creating more compiled (both IL and native), because of the additional exception handling by the using statement. This would need to be confirmed by testing though.

Activity

  1. added
    enhancement
    on Aug 1, 2018
  2. added this to the Future milestone on Aug 1, 2018
  3. spouliot commented on Aug 1, 2018

    @spouliot
    Contributor

    I think it's easy to measure the potential disadvantage (if any) before implementing the generator/linker changes.

    Another advantage is that we sometimes needs to use the blocks in non-generated (manual) code. Hiding most details will reduce potential bugs and ease maintainability (right now a required change would need a lot of sites to fix).

  4. added
    macOS
    iOS
    on Aug 1, 2018
  5. spouliot commented on Aug 1, 2018

    @spouliot
    Contributor

    Testing-wise we should ensure that, even if easier, manual implementation are optimizable (if decorated as such). In fact we might want to do that in parallel (or without the RFC) since this is an existing situation (even if they all work today it's hard to guarantee they will remain that way).
    #4547 (review)

  6. rolfbjarne commented on Aug 2, 2018

    @rolfbjarne
    MemberAuthor

    we should ensure that manual implementation are optimizable

    All block creations in our platform assemblies must be optimizable, otherwise we can't safely remove the dynamic registrar.

    This is enforced by tests.

    The only way to ensure that any optimization is working correctly, is to execute the optimized code. Which is one of the reasons we need to write tests (monotouch-tests) for all manually bound code 😄 (we execute monotouch-test both in optimized and non-optimized mode).

  7. added
    request-for-comments
    on Sep 20, 2018
  8. rolfbjarne commented on Sep 23, 2022

    @rolfbjarne
    MemberAuthor

    Duplicate of #15783.

  9. ghost locked as resolved and limited conversation to collaborators on Oct 23, 2022
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

    enhancementiOSmacOSrequest-for-comments

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions