Skip to content

Commit

Permalink
Change argDesc to be a single primitive array of Strings. 300-400k sa…
Browse files Browse the repository at this point in the history
…vings on rails console
  • Loading branch information
enebo committed Mar 7, 2015
1 parent 944d98a commit c7276b5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
30 changes: 22 additions & 8 deletions core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java
Expand Up @@ -222,10 +222,17 @@ public static IRubyObject methodArgs(IRubyObject recv) {
argsArray.append(RubyArray.newArray(runtime, block, getNameFrom(runtime, args.getBlock())));
}
} else if (method instanceof IRMethodArgs) {
for (String[] argParam: ((IRMethodArgs)method).getParameterList()) {
RubySymbol argType = runtime.newSymbol(argParam[0]);
if (argParam[1] == "") argsArray.append(RubyArray.newArray(runtime, argType));
else argsArray.append(RubyArray.newArray(runtime, argType, runtime.newSymbol(argParam[1])));
String[] argsDesc = ((IRMethodArgs) method).getParameterList();

for (int i = 0; i < argsDesc.length; i++) {
RubySymbol argType = runtime.newSymbol(argsDesc[i]);
i++;
String argName = argsDesc[i];
if (argName.isEmpty()) {
argsArray.append(RubyArray.newArray(runtime, argType));
} else {
argsArray.append(RubyArray.newArray(runtime, argType, runtime.newSymbol(argName)));
}
}
} else {
if (method.getArity() == Arity.OPTIONAL) {
Expand Down Expand Up @@ -287,10 +294,17 @@ public static String[] methodParameters(Ruby runtime, DynamicMethod method) {
argsArray.add("b" + getNameFrom(runtime, args.getBlock()));
}
} else if (method instanceof IRMethodArgs) {
for (String[] argParam: ((IRMethodArgs)method).getParameterList()) {
RubySymbol argType = runtime.newSymbol(argParam[0]);
if (argParam[1] == "") argsArray.add(argParam[0]);
else argsArray.add(argParam[0] + argParam[1]);
String[] argsDesc = ((IRMethodArgs) method).getParameterList();

for (int i = 0; i < argsDesc.length; i++) {
String argType = argsDesc[i];
i++;
String argName = argsDesc[i];
if (argName.isEmpty()) {
argsArray.add(argType);
} else {
argsArray.add(argType + argName);
}
}
} else {
if (method.getArity() == Arity.OPTIONAL) {
Expand Down
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

public interface IRMethodArgs {
public List<String[]> getParameterList();
public String[] getParameterList();

public enum ArgType {
key, keyreq, keyrest, block, opt, rest, req
Expand Down
Expand Up @@ -22,8 +22,8 @@ public InterpretedIRBodyMethod(IRScope method, RubyModule implementationClass) {
}

@Override
public List<String[]> getParameterList() {
return new ArrayList<>();
public String[] getParameterList() {
return new String[0];
}

@Override
Expand Down
Expand Up @@ -55,7 +55,7 @@ public StaticScope getStaticScope() {
return method.getStaticScope();
}

public List<String[]> getParameterList() {
public String[] getParameterList() {
ensureInstrsReady(); // Make sure method is minimally built before returning this info
return ((IRMethod) method).getArgDesc();
}
Expand Down
Expand Up @@ -65,7 +65,7 @@ public StaticScope getStaticScope() {
return method.getStaticScope();
}

public List<String[]> getParameterList() {
public String[] getParameterList() {
ensureInstrsReady(); // Make sure method is minimally built before returning this info
return ((IRMethod) method).getArgDesc();
}
Expand Down
36 changes: 28 additions & 8 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -282,6 +282,7 @@ public IRLoop getCurrentLoop() {
protected IRManager manager;
protected IRScope scope;
protected List<Instr> instructions;
protected List<String> argumentDescriptions;

public IRBuilder(IRManager manager, IRScope scope, IRBuilder parent) {
this.manager = manager;
Expand All @@ -291,6 +292,13 @@ public IRBuilder(IRManager manager, IRScope scope, IRBuilder parent) {
this.activeRescuers.push(Label.UNRESCUED_REGION_LABEL);
}

public void addArgumentDescription(IRMethodArgs.ArgType type, String name) {
if (argumentDescriptions == null) argumentDescriptions = new ArrayList<>();

argumentDescriptions.add(type.toString());
argumentDescriptions.add(name);
}

public void addInstr(Instr instr) {
// If we are building an ensure body, stash the instruction
// in the ensure body's list. If not, add it to the scope directly.
Expand Down Expand Up @@ -1716,9 +1724,21 @@ protected InterpreterContext defineMethodInner(MethodDefNode defNode, IRScope pa
// If the method can receive non-local returns
if (scope.canReceiveNonlocalReturns()) handleNonlocalReturnInMethod();

String[] argDesc;
if (argumentDescriptions == null) {
argDesc = NO_ARG_DESCS;
} else {
argDesc = new String[argumentDescriptions.size()];
argumentDescriptions.toArray(argDesc);
}

((IRMethod) scope).setArgDesc(argDesc);

return scope.allocateInterpreterContext(instructions);
}

static final String[] NO_ARG_DESCS = new String[0];

private IRMethod defineNewMethod(MethodDefNode defNode, boolean isInstanceMethod) {
return new IRMethod(manager, scope, defNode, defNode.getName(), isInstanceMethod, defNode.getPosition().getLine(), defNode.getScope());

Expand Down Expand Up @@ -1758,7 +1778,7 @@ public void receiveRequiredArg(Node node, int argIndex, boolean post, int numPre
case ARGUMENTNODE: {
ArgumentNode a = (ArgumentNode)node;
String argName = a.getName();
if (scope instanceof IRMethod) ((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.req, argName);
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.req, argName);
// Ignore duplicate "_" args in blocks
// (duplicate _ args are named "_$0")
if (!argName.equals("_$0")) {
Expand All @@ -1770,7 +1790,7 @@ public void receiveRequiredArg(Node node, int argIndex, boolean post, int numPre
MultipleAsgn19Node childNode = (MultipleAsgn19Node) node;
Variable v = createTemporaryVariable();
addArgReceiveInstr(v, argIndex, post, numPreReqd, numPostRead);
if (scope instanceof IRMethod) ((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.req, "");
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.req, "");
Variable tmp = createTemporaryVariable();
addInstr(new ToAryInstr(tmp, v));
buildMultipleAsgn19Assignment(childNode, tmp, null);
Expand Down Expand Up @@ -1829,7 +1849,7 @@ protected void receiveNonBlockArgs(final ArgsNode argsNode) {
OptArgNode n = (OptArgNode)optArgs.get(j);
String argName = n.getName();
Variable av = getNewLocalVariable(argName, 0);
if (scope instanceof IRMethod) ((IRMethod)scope).addArgDesc(IRMethodArgs.ArgType.opt, argName);
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.opt, argName);
// You need at least required+j+1 incoming args for this opt arg to get an arg at all
addInstr(new ReceiveOptArgInstr(av, required, numPreReqd, j));
addInstr(BNEInstr.create(av, UndefinedValue.UNDEFINED, l)); // if 'av' is not undefined, go to default
Expand All @@ -1844,7 +1864,7 @@ protected void receiveNonBlockArgs(final ArgsNode argsNode) {
// For this code, there is no argument name available from the ruby code.
// So, we generate an implicit arg name
String argName = argsNode.getRestArgNode().getName();
if (scope instanceof IRMethod) ((IRMethod)scope).addArgDesc(IRMethodArgs.ArgType.rest, argName == null ? "" : argName);
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.rest, argName == null ? "" : argName);
argName = (argName == null || argName.equals("")) ? "*" : argName;

// You need at least required+opt+1 incoming args for the rest arg to get any args at all
Expand Down Expand Up @@ -1872,7 +1892,7 @@ protected void receiveBlockArg(final ArgsNode argsNode) {
if (blockArg != null) {
String blockArgName = blockArg.getName();
Variable blockVar = getLocalVariable(blockArgName, 0);
if (scope instanceof IRMethod) ((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.block, blockArgName);
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.block, blockArgName);
Variable tmp = createTemporaryVariable();
addInstr(new LoadImplicitClosureInstr(tmp));
addInstr(new ReifyClosureInstr(blockVar, tmp));
Expand Down Expand Up @@ -1936,7 +1956,7 @@ public void receiveArgs(final ArgsNode argsNode) {
if (keyRest != null) {
String argName = keyRest.getName();
Variable av = getNewLocalVariable(argName, 0);
if (scope instanceof IRMethod) ((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.keyrest, argName);
if (scope instanceof IRMethod) addArgumentDescription(IRMethodArgs.ArgType.keyrest, argName);
addInstr(new ReceiveKeywordRestArgInstr(av, required));
}

Expand All @@ -1946,9 +1966,9 @@ public void receiveArgs(final ArgsNode argsNode) {

private void addKeyArgDesc(AssignableNode kasgn, String argName) {
if (isRequiredKeywordArgumentValue(kasgn)) {
((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.keyreq, argName);
addArgumentDescription(IRMethodArgs.ArgType.keyreq, argName);
} else {
((IRMethod) scope).addArgDesc(IRMethodArgs.ArgType.key, argName);
addArgumentDescription(IRMethodArgs.ArgType.key, argName);
}
}

Expand Down
16 changes: 9 additions & 7 deletions core/src/main/java/org/jruby/ir/IRMethod.java
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.Map;
import org.jruby.ast.MethodDefNode;
import org.jruby.internal.runtime.methods.IRMethodArgs;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.ir.representations.BasicBlock;
Expand All @@ -17,7 +16,7 @@ public class IRMethod extends IRScope {
public final boolean isInstanceMethod;

// Argument description of the form [:req, "a"], [:opt, "b"] ..
private List<String[]> argDesc;
private String[] argDesc;

// Signatures to the jitted versions of this method
private Map<Integer, MethodType> signatures;
Expand All @@ -33,7 +32,7 @@ public IRMethod(IRManager manager, IRScope lexicalParent, MethodDefNode defn, St

this.defn = defn;
this.isInstanceMethod = isInstanceMethod;
this.argDesc = new ArrayList<>();
this.argDesc = null;
this.signatures = null;

if (!getManager().isDryRun() && staticScope != null) {
Expand Down Expand Up @@ -63,12 +62,15 @@ public IRScopeType getScopeType() {
return isInstanceMethod ? IRScopeType.INSTANCE_METHOD : IRScopeType.CLASS_METHOD;
}

public void addArgDesc(IRMethodArgs.ArgType type, String argName) {
argDesc.add(new String[]{type.name(), argName});
public String[] getArgDesc() {
return argDesc;
}

public List<String[]> getArgDesc() {
return argDesc;
/**
* Set upon completion of IRBuild of this IRMethod.
*/
public void setArgDesc(String[] argDesc) {
this.argDesc = argDesc;
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Expand Up @@ -2817,11 +2817,13 @@ public static RubyArray parameterListToParameters(Ruby runtime, String[] paramet
return parms;
}

public static String[] irMethodArgsToParameters(List<String[]> argDesc) {
String[] tmp = new String[argDesc.size()];
public static String[] irMethodArgsToParameters(String[] argDesc) {
String[] tmp = new String[argDesc.length];
for (int i = 0; i < tmp.length; i++) {
String[] arg = argDesc.get(i);
String encoded = arg[0].charAt(0) + arg[1];
String type = argDesc[i];
i++;
String name = argDesc[i];
String encoded = type.charAt(0) + name;
tmp[i] = encoded;
}

Expand Down

0 comments on commit c7276b5

Please sign in to comment.