Skip to content

Reduce number of classes generated with MethodHandles/invokedynamic #323

@rahulmutt

Description

@rahulmutt

Should be done after #322. Same example will be used:

package main;

public class Main {
  public StgClosure add_closure = new add();
  public static class add {
    public void enter(StgContext context) {
      StgClosure x = context.R(2);
      StgClosure y = context.R(3);
      add_entry(context, x, y);
    }
  }
  public static void add_entry(StgContext context, StgClosure x, StgClosure y) {
    // Non-trivial part of entry code
  }
}

It seems like the code for enter in the add class is pretty common and any top-level function that takes two argument will have the exact same form with the difference being the static method that is called at the end. If we can find a way to abstract over that, we can reduce the number of classes we generate significantly (but we might take a slight hit in performance?) which is a win for platforms like Android that have method limits.

Here's an SO post that outlines the different ways of handling the problem. To summarise:

  1. Generate the class at runtime - Looks promising but may add to startup time. The generation should happen when creating the closure for the first time when populating the private closure fields.
  2. Use reflection - OK but not too happy about the potential performance hit.
  3. Use method handles/using the lambda factory methods in Java 8 - Perfect solution for the problem but forces Java 8 on the users.

I think a balance between 1 & 3 is the ideal solution. Something like - use the lambda factory methods if Java 8 is there, otherwise fallback on (1) which is backwards compatible.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions