Skip to content

Support for weak entry points in the VM #50649

Open
@mkustermann

Description

@mkustermann

Currently we can mark functions (and other objects) as @pragma('vm:entry-point').

Having this annotation implies currently a number of things:

  1. The function serves as a root for our AOT tree shaker (i.e. it's code and what it calls cannot be tree-shaken)
  2. The function may be looked up by name (i.e. cannot be obfuscated)
  3. The function needs to have it's Function object preserved for AOT runtime (i.e. cannot tree-shake function object)
  4. The function's parameter types etc need to be preserved so arguments can be type-checked before invoking the function (i.e. cannot tree-shake function metadata)

There's use cases where we'd only need 2/3/4 but not 1 and as such would allow us to sometimes tree shake chunks of apps away that happen to be unused.

Case for @pragma('vm:weak-entry-point')

Flutter provides an API to map a function to a "unique" integer. When the app later on gets started (e.g. due to an OS event that starts the app) the app can look up based on that unique integer the dart function and call it. More specifically an app can

Now the interesting thing to note here is that if we have e.g.

void register() {
  if (<always-false>) {
    // Dead code, call will be removed by AOT compiler.
    PluginUtilities.getCallbackHandle(myCallback);
  }
}

void foo() {
  final function = PluginUtilities.getCallbackFromhandle(CallbackHandle.fromRawHandle(123));
  // `function` can never be `myCallback` (since we tree-shaken `PluginUtilities.getCallbackHandle(myCallback)`).
}

@pragma('vm:entry-point')
void myCallback() {
  <lots of things we do here>
}

then we (as application developer) know that myCallback will never be invoked.

To support this use case, we could add a @pramga('vm:weak-entry-point') that will have the same implications as a normal @pragma('vm:entry-point') with the difference that we allow our AOT tree shaker to tree shake it, but if it survived we have to guarantee 2/3/4 above.

/cc @alexmarkov @mraleph

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions