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

Use method handles for binding ungenerated invokers #8044

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.lang.invoke.MethodHandles;
import java.util.function.Supplier;

import com.headius.invokebinder.Binder;
import com.headius.invokebinder.SmartBinder;
import com.headius.invokebinder.SmartHandle;
import org.jruby.RubyModule;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
Expand Down Expand Up @@ -74,8 +76,10 @@ public class HandleMethod extends DynamicMethod implements MethodArgs2, Cloneabl
private final int max;
private final String parameterDesc;
private final Signature signature;
private final boolean checkArity;
private final boolean builtin;
private final boolean notImplemented;
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();

private static final com.headius.invokebinder.Signature ARITY_0 =
com.headius.invokebinder.Signature.from(
Expand All @@ -85,6 +89,7 @@ public class HandleMethod extends DynamicMethod implements MethodArgs2, Cloneabl
private static final com.headius.invokebinder.Signature ARITY_1 = ARITY_0.insertArg(4, "arg0", IRubyObject.class);
private static final com.headius.invokebinder.Signature ARITY_2 = ARITY_1.insertArg(5, "arg1", IRubyObject.class);
private static final com.headius.invokebinder.Signature ARITY_3 = ARITY_2.insertArg(6, "arg2", IRubyObject.class);
private static final com.headius.invokebinder.Signature ARITY_N = ARITY_0.insertArg(4, "args", IRubyObject[].class);
private static final com.headius.invokebinder.Signature[] ARITIES = {ARITY_0, ARITY_1, ARITY_2, ARITY_3};

public HandleMethod(
Expand All @@ -97,6 +102,7 @@ public HandleMethod(
String parameterDesc,
final int min,
final int max,
final boolean checkArity,
final Supplier<MethodHandle> maker0,
final Supplier<MethodHandle> maker1,
final Supplier<MethodHandle> maker2,
Expand All @@ -110,6 +116,7 @@ public HandleMethod(
this.parameterDesc = parameterDesc;
this.min = min;
this.max = max;
this.checkArity = checkArity;
this.maker0 = maker0;
this.maker1 = maker1;
this.maker2 = maker2;
Expand Down Expand Up @@ -222,7 +229,19 @@ private MethodHandle ensureTarget4() {
target4 = null;
} else {
target4 = maker4.get();

// wrap with arity check if requested
if (checkArity) {
SmartHandle arityCheck = SmartBinder.from(ARITY_N.changeReturn(int.class))
.permute("context", "args")
.append(arrayOf("min", "max"), arrayOf(int.class, int.class), min, max)
.invokeStaticQuiet(LOOKUP, Arity.class, "checkArgumentCount");
target4 = Binder.from(target4.type())
.foldVoid(arityCheck.handle())
.invoke(target4);
}
}

this.target4 = target4;
initialized4 = true;
this.maker4 = null;
Expand Down Expand Up @@ -323,7 +342,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz

@Override
public DynamicMethod dup() {
return new HandleMethod(implementationClass, getVisibility(), name, signature.encode(), builtin, notImplemented, parameterDesc, min, max, maker0, maker1, maker2, maker3, maker4);
return new HandleMethod(implementationClass, getVisibility(), name, signature.encode(), builtin, notImplemented, parameterDesc, min, max, checkArity, maker0, maker1, maker2, maker3, maker4);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public DynamicMethod getAnnotatedMethod(final RubyModule implementationClass, fi
info.getParameterDesc(),
min,
max,
desc1.anno.checkArity(),
generators[0],
generators[1],
generators[2],
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/util/cli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class Options {
// ClassValue in OpenJDK appears to root data even after it goes away, so this is disabled again. See jruby/jruby#3228
public static final Option<Boolean> INVOKEDYNAMIC_CLASS_VALUES = bool(INVOKEDYNAMIC, "invokedynamic.class.values", false, "Use ClassValue to store class-specific data.");
public static final Option<Integer> INVOKEDYNAMIC_GLOBAL_MAXFAIL = integer(INVOKEDYNAMIC, "invokedynamic.global.maxfail", 100, "Maximum global cache failures after which to use slow path.");
public static final Option<Boolean> INVOKEDYNAMIC_HANDLES = bool(INVOKEDYNAMIC, "invokedynamic.handles", false, "Use MethodHandles rather than generated code to bind Ruby methods.");
public static final Option<Boolean> INVOKEDYNAMIC_HANDLES = bool(INVOKEDYNAMIC, "invokedynamic.handles", true, "Use MethodHandles rather than generated code to bind methods.");
public static final Option<Boolean> INVOKEDYNAMIC_YIELD = bool(INVOKEDYNAMIC, "invokedynamic.yield", true, "Bind yields directly using invokedynamic.");

// NOTE: -1 jit.threshold is way of having interpreter not promote full builds
Expand Down