Skip to content

Commit 53d3517

Browse files
committedDec 19, 2017
Reduce more use of getName for getByteName in IRBuilder from AST.
1 parent 7b3576e commit 53d3517

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed
 

‎core/src/main/java/org/jruby/ir/IRBuilder.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public class IRBuilder {
8686
static final Operand[] NO_ARGS = new Operand[]{};
8787
static final UnexecutableNil U_NIL = UnexecutableNil.U_NIL;
8888

89-
public static final String USING_METHOD = "using";
89+
public static final ByteList USING_METHOD = new ByteList(new byte[] {'u', 's', 'i', 'n', 'g'});
90+
public static final ByteList DEFINE_METHOD_METHOD = new ByteList(new byte[] {'d', 'e', 'f', 'i', 'n', 'e', '_', 'm', 'e', 't', 'h', 'o', 'd'});
91+
public static final ByteList FREEZE_METHOD = new ByteList(new byte[] {'f', 'r', 'e', 'e', 'z', 'e'});
92+
public static final ByteList AREF_METHOD = new ByteList(new byte[] {'[', ']'});
93+
public static final ByteList NEW_METHOD = new ByteList(new byte[] {'n', 'e', 'w'});
9094

9195
public static Node buildAST(boolean isCommandLineScript, String arg) {
9296
Ruby ruby = Ruby.getGlobalRuntime();
@@ -1044,7 +1048,7 @@ public Operand buildCall(Variable result, CallNode callNode) {
10441048
Node receiverNode = callNode.getReceiverNode();
10451049

10461050
// Frozen string optimization: check for "string".freeze
1047-
if (receiverNode instanceof StrNode && callNode.getName().equals("freeze")) {
1051+
if (receiverNode instanceof StrNode && FREEZE_METHOD.equals(callNode.getByteName())) {
10481052
StrNode asString = (StrNode) receiverNode;
10491053
return new FrozenString(asString.getValue(), asString.getCodeRange(), scope.getFileName(), asString.getPosition().getLine());
10501054
}
@@ -1056,9 +1060,8 @@ public Operand buildCall(Variable result, CallNode callNode) {
10561060

10571061
// obj["string"] optimization for Hash
10581062
ArrayNode argsAry;
1059-
if (
1060-
!callNode.isLazy() &&
1061-
callNode.getName().equals("[]") &&
1063+
if (!callNode.isLazy() &&
1064+
AREF_METHOD.equals(callNode.getByteName()) &&
10621065
callNode.getArgsNode() instanceof ArrayNode &&
10631066
(argsAry = (ArrayNode) callNode.getArgsNode()).size() == 1 &&
10641067
argsAry.get(0) instanceof StrNode &&
@@ -1082,19 +1085,20 @@ public Operand buildCall(Variable result, CallNode callNode) {
10821085

10831086
CallInstr callInstr;
10841087
Operand block;
1088+
ByteList name = callNode.getByteName();
10851089
if (keywordArgs != null) {
10861090
Operand[] args = buildCallArgsExcept(callNode.getArgsNode(), keywordArgs);
10871091
List<KeyValuePair<Operand, Operand>> kwargs = buildKeywordArguments(keywordArgs);
10881092
block = setupCallClosure(callNode.getIterNode());
1089-
callInstr = CallInstr.createWithKwargs(scope, CallType.NORMAL, result, callNode.getByteName(), receiver, args, block, kwargs);
1093+
callInstr = CallInstr.createWithKwargs(scope, CallType.NORMAL, result, name, receiver, args, block, kwargs);
10901094
} else {
10911095
Operand[] args = setupCallArgs(callNode.getArgsNode());
10921096
block = setupCallClosure(callNode.getIterNode());
1093-
callInstr = CallInstr.create(scope, result, callNode.getByteName(), receiver, args, block);
1097+
callInstr = CallInstr.create(scope, result, name, receiver, args, block);
10941098
}
10951099

10961100
determineIfWeNeedLineNumber(callNode);
1097-
determineIfProcNew(receiverNode, callNode.getName(), callInstr);
1101+
determineIfProcNew(receiverNode, name, callInstr);
10981102
receiveBreakException(block, callInstr);
10991103

11001104
if (callNode.isLazy()) {
@@ -1115,9 +1119,9 @@ private List<KeyValuePair<Operand, Operand>> buildKeywordArguments(HashNode keyw
11151119
return kwargs;
11161120
}
11171121

1118-
private void determineIfProcNew(Node receiverNode, String name, CallInstr callInstr) {
1122+
private void determineIfProcNew(Node receiverNode, ByteList name, CallInstr callInstr) {
11191123
// This is to support the ugly Proc.new with no block, which must see caller's frame
1120-
if (name.equals("new") && receiverNode instanceof ConstNode && ((ConstNode)receiverNode).getName().equals("Proc")) {
1124+
if (NEW_METHOD.equals(name) && receiverNode instanceof ConstNode && ((ConstNode)receiverNode).getName().equals("Proc")) {
11211125
callInstr.setProcNew(true);
11221126
}
11231127
}
@@ -1711,7 +1715,7 @@ public Operand buildGetDefinition(Node node) {
17111715
IS_DEFINED_METHOD,
17121716
new Operand[] {
17131717
buildSelf(),
1714-
new FrozenString(((VCallNode) node).getName()),
1718+
new FrozenString(((VCallNode) node).getByteName()),
17151719
manager.getFalse(),
17161720
new FrozenString(DefinedMessage.METHOD.getText())
17171721
}
@@ -1829,7 +1833,7 @@ public Operand run() {
18291833
IS_DEFINED_METHOD,
18301834
new Operand[]{
18311835
buildSelf(),
1832-
new FrozenString(((FCallNode) node).getName()),
1836+
new FrozenString(((FCallNode) node).getByteName()),
18331837
manager.getFalse(),
18341838
new FrozenString(DefinedMessage.METHOD.getText())
18351839
}
@@ -1855,7 +1859,7 @@ public Operand run() {
18551859
IS_DEFINED_CALL,
18561860
new Operand[]{
18571861
build(callNode.getReceiverNode()),
1858-
new StringLiteral(callNode.getName()),
1862+
new StringLiteral(callNode.getByteName()),
18591863
new FrozenString(DefinedMessage.METHOD.getText())
18601864
}
18611865
)
@@ -1903,7 +1907,7 @@ public Operand run() {
19031907
IS_DEFINED_METHOD,
19041908
new Operand[] {
19051909
receiver,
1906-
new StringLiteral(attrAssign.getName()),
1910+
new StringLiteral(attrAssign.getByteName()),
19071911
manager.getTrue(),
19081912
new FrozenString(DefinedMessage.METHOD.getText())
19091913
}
@@ -2688,11 +2692,11 @@ public Operand buildFCall(Variable result, FCallNode fcallNode) {
26882692
} else {
26892693
Operand[] args = setupCallArgs(callArgsNode);
26902694
block = setupCallClosure(fcallNode.getIterNode());
2691-
determineIfMaybeUsingMethod(fcallNode.getName(), args);
2695+
determineIfMaybeUsingMethod(fcallNode.getByteName(), args);
26922696

26932697
// We will stuff away the iters AST source into the closure in the hope we can convert
26942698
// this closure to a method.
2695-
if (fcallNode.getName().equals("define_method") && block instanceof WrappedIRClosure) {
2699+
if (DEFINE_METHOD_METHOD.equals(fcallNode.getByteName()) && block instanceof WrappedIRClosure) {
26962700
IRClosure closure = ((WrappedIRClosure) block).getClosure();
26972701

26982702
// To convert to a method we need its variable scoping to appear like a normal method.
@@ -2727,7 +2731,7 @@ private Operand setupCallClosure(Node node) {
27272731
}
27282732

27292733
// FIXME: This needs to be called on super/zsuper too
2730-
private void determineIfMaybeUsingMethod(String methodName, Operand[] args) {
2734+
private void determineIfMaybeUsingMethod(ByteList methodName, Operand[] args) {
27312735
IRScope outerScope = scope.getNearestTopLocalVariableScope();
27322736

27332737
// 'using single_mod_arg' possible nearly everywhere but method scopes.

0 commit comments

Comments
 (0)
Please sign in to comment.