Skip to content

Commit

Permalink
So parser will use SourcePosition to not heavily modify things but the
Browse files Browse the repository at this point in the history
finished AST will no longer have a ISourcePosition instance in it.  It will
be it.  The filename itself will also no longer be stored in every node but
the RootNode already has the name and the rest of the runtime already asks
the root for the filename.

This removes at least 1.5M from a single controller rails app (which is
an interesting stat in this is only AST nodes of methods which have never
been called).

Follow up commits will be to slowly remove the use of ISourcePosition
altogether (although it will be mostly Deprecations).
enebo committed Apr 14, 2017
1 parent 475b5fa commit a91d076
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/org/jruby/ast/Node.java
Original file line number Diff line number Diff line change
@@ -47,12 +47,12 @@
/**
* Base class for all Nodes in the AST
*/
public abstract class Node implements ISourcePositionHolder, ParseResult {
public abstract class Node implements ISourcePositionHolder, ParseResult, ISourcePosition {
// We define an actual list to get around bug in java integration (1387115)
static final List<Node> EMPTY_LIST = new ArrayList<>();

private ISourcePosition position;

private int line;

// Does this node contain a node which is an assignment. We can use this knowledge when emitting IR
// instructions to do more or less depending on whether we have to cope with scenarios like:
// a = 1; [a, a = 2];
@@ -62,7 +62,7 @@ public abstract class Node implements ISourcePositionHolder, ParseResult {
protected boolean newline;

public Node(ISourcePosition position, boolean containsAssignment) {
this.position = position;
this.line = position.getLine();
this.containsVariableAssignment = containsAssignment;
}

@@ -78,15 +78,19 @@ public boolean isNewline() {
* Location of this node within the source
*/
public ISourcePosition getPosition() {
return position;
return this;
}

public int getLine() {
return position.getLine();
return line;
}

public String getFile() {
return null;
}

public void setPosition(ISourcePosition position) {
this.position = position;
this.line = position.getLine();
}

public abstract <T> T accept(NodeVisitor<T> visitor);
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Original file line number Diff line number Diff line change
@@ -1025,7 +1025,7 @@ public Operand buildCall(Variable result, CallNode callNode) {
// Frozen string optimization: check for "string".freeze
if (receiverNode instanceof StrNode && callNode.getName().equals("freeze")) {
StrNode asString = (StrNode) receiverNode;
return new FrozenString(asString.getValue(), asString.getCodeRange(), asString.getPosition().getFile(), asString.getPosition().getLine());
return new FrozenString(asString.getValue(), asString.getCodeRange(), scope.getFileName(), asString.getPosition().getLine());
}

// The receiver has to be built *before* call arguments are built
@@ -3773,11 +3773,11 @@ public Operand buildStr(StrNode strNode) {
public Operand buildStrRaw(StrNode strNode) {
if (strNode instanceof FileNode) return new Filename();

ISourcePosition pos = strNode.getPosition();
int line = strNode.getLine();

if (strNode.isFrozen()) return new FrozenString(strNode.getValue(), strNode.getCodeRange(), pos.getFile(), pos.getLine());
if (strNode.isFrozen()) return new FrozenString(strNode.getValue(), strNode.getCodeRange(), scope.getFileName(), line);

return new StringLiteral(strNode.getValue(), strNode.getCodeRange(), pos.getFile(), pos.getLine());
return new StringLiteral(strNode.getValue(), strNode.getCodeRange(), scope.getFileName(), line);
}

private Operand buildSuperInstr(Operand block, Operand[] args) {
@@ -3909,7 +3909,8 @@ public Operand buildWhile(final WhileNode whileNode) {
}

public Operand buildXStr(XStrNode node) {
return addResultInstr(new BacktickInstr(createTemporaryVariable(), new Operand[] { new FrozenString(node.getValue(), node.getCodeRange(), node.getPosition().getFile(), node.getPosition().getLine())}));
return addResultInstr(new BacktickInstr(createTemporaryVariable(),
new Operand[] { new FrozenString(node.getValue(), node.getCodeRange(), scope.getFileName(), node.getLine())}));
}

public Operand buildYield(YieldNode node, Variable result) {

0 comments on commit a91d076

Please sign in to comment.