Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instanceless virtual allocator methods #1835

Open
dmlloyd opened this issue May 1, 2023 · 1 comment
Open

Instanceless virtual allocator methods #1835

dmlloyd opened this issue May 1, 2023 · 1 comment
Labels
kind: enhancement ✨ A new feature or use case

Comments

@dmlloyd
Copy link
Collaborator

dmlloyd commented May 1, 2023

There are cases - particularly around object allocation - where we could benefit from virtual method dispatch behavior despite not having a receiver instance to operate on.

For example, with object allocation, the base Object class has complex, customizable behavior (dealing with method headers for example, or eventually supporting lock nursery and lightweight locking optimizations), and subclasses may have additional interesting behavior (for example Throwable would record the stack pointer per #1778, and arrays would need to dynamically choose their allocation size and initialize the length immediately). These behaviors would precede constructor invocation, yet are type-dependent. Right now we just have distinct compiler-side code paths for these cases, but that ignores cases where the exact type ID could be unknown at compile time, and also puts somewhat heavy code in line with every allocation.

Right now VirtualMethodLookup operates on an object instance. But, it should probably be operating directly on a type ID value instead, with the default lookup behavior using an explicit Load to get the type ID. This would allow these loads to be more easily coalesced in addition to avoiding the requirement of having a valid this reference.

Since there is no this, it could be argued that the actual target instance method type would actually behave more like static. On the language side, there are pros and cons to having these methods be static. One significant con of using a static method on the language would be that the super.xxx() call would not work correctly. Since subclass allocators would generally call their superclass allocator first, this would be a significant hindrance. However, using instance methods on the language side means that we would have to do some special work to handle this. The easiest solution would be to reject any instanceless virtual method which accesses this. A more nuanced approach could be to add an intrinsic which implements a thisIs(xx) method, after which this becomes defined, e.g. thisIs(super.allocate()).

Another future use case for instanceless invocation would be optimization cases where this is not used in any reachable callee. Standard virtual methods could be transformed into their instanceless equivalents, allowing a more compact calling convention to be used instead of other techniques such as passing in null for this, which wastes a call value slot (typically a register).

@dmlloyd dmlloyd added the kind: enhancement ✨ A new feature or use case label May 1, 2023
@dmlloyd
Copy link
Collaborator Author

dmlloyd commented May 1, 2023

This also may imply that we need a way to acquire the receiver's type ID from a "static" context, particularly if the type ID becomes an implicit parameter for these types of methods (thought this could partially or completely invalidate the benefits of the aforementioned optimization). On a normal instance method, this would be equivalent to typeIdOf(this); on an instanceless method, it would read the implicit parameter similar to the manner of currentThread().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement ✨ A new feature or use case
Projects
None yet
Development

No branches or pull requests

1 participant