Skip to content

Commit

Permalink
Share LineNumberInstr across scopes. This is entering into the weird …
Browse files Browse the repository at this point in the history
…territory but

LNI has no operands and cannot raise so it should be totally safe to share.  This
reduces empty rails server usage of LineNumberInstr from 1.7M to 95K.
enebo committed Jun 18, 2015
1 parent 7fac768 commit 7b22490
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
@@ -479,7 +479,7 @@ public Node skipOverNewlines(Node n) {
if (RubyInstanceConfig.FULL_TRACE_ENABLED) {
addInstr(new TraceInstr(RubyEvent.LINE, methodNameFor(), getFileName(), currLineNum));
}
addInstr(new LineNumberInstr(currLineNum));
addInstr(manager.newLineNumber(currLineNum));
_lastProcessedLineNum = currLineNum;
}
}
@@ -3256,7 +3256,7 @@ public Operand buildReturn(ReturnNode returnNode) {
}

public InterpreterContext buildEvalRoot(RootNode rootNode) {
addInstr(new LineNumberInstr(scope.getLineNumber()));
addInstr(manager.newLineNumber(scope.getLineNumber()));

prepareImplicitState(); // recv_self, add frame block, etc)
addCurrentScopeAndModule(); // %current_scope/%current_module
28 changes: 28 additions & 0 deletions core/src/main/java/org/jruby/ir/IRManager.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import java.util.EnumSet;
import org.jruby.RubyInstanceConfig;
import org.jruby.ir.instructions.Instr;
import org.jruby.ir.instructions.LineNumberInstr;
import org.jruby.ir.listeners.IRScopeListener;
import org.jruby.ir.listeners.InstructionsListener;
import org.jruby.ir.operands.*;
@@ -178,6 +179,33 @@ public void addListener(IRScopeListener listener) {
}
}

public LineNumberInstr newLineNumber(int line) {
if (line >= lineNumbers.length-1) growLineNumbersPool(line);

if (line < 0) line = 0;
LineNumberInstr tempVar = lineNumbers[line];

if (tempVar == null) {
tempVar = new LineNumberInstr(line);
lineNumbers[line] = tempVar;
}

return tempVar;

}

private LineNumberInstr[] lineNumbers = new LineNumberInstr[3000];

protected LineNumberInstr[] growLineNumbersPool(int index) {
int newLength = index * 2;
LineNumberInstr[] newPool = new LineNumberInstr[newLength];

System.arraycopy(lineNumbers, 0, newPool, 0, lineNumbers.length);
lineNumbers = newPool;
return newPool;
}


public void removeListener(IRScopeListener listener) {
if (irScopeListener.equals(listener)) irScopeListener = null;
}
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ public void encode(IRWriterEncoder e) {
}

public static LineNumberInstr decode(IRReaderDecoder d) {
return new LineNumberInstr(d.decodeInt());
return d.getCurrentScope().getManager().newLineNumber(d.decodeInt());
}

@Override
Original file line number Diff line number Diff line change
@@ -68,7 +68,6 @@ private Instr[] prepareBuildInstructions(List<Instr> instructions) {
Instr[] linearizedInstrArray = instructions.toArray(new Instr[length]);
for (int ipc = 0; ipc < length; ipc++) {
Instr i = linearizedInstrArray[ipc];
i.setIPC(ipc);

if (i instanceof LabelInstr) ((LabelInstr) i).getLabel().setTargetPC(ipc + 1);
}

0 comments on commit 7b22490

Please sign in to comment.