Skip to content

Commit

Permalink
Improve performance of getting this variable in methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Mar 12, 2020
1 parent 17eddd0 commit 6af9aaf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ protected void writeConstructor() {
false
));

LabelNode label = expressionCompiler.writeLabel(methodCompiler.node);
LocalVariable local = methodCompiler.addLocalVariable("$THIS", label, Memory.class);
expressionCompiler.writeDefineThis(local, null);

expressionCompiler.writeVarLoad("~this");
expressionCompiler.writeVarLoad("$THIS");
expressionCompiler.writePutDynamic("$THIS", Memory.class);

// PROPERTIES
for (ClassVarStmtToken property : statement.getProperties()) {
ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(methodCompiler, null);
Expand Down Expand Up @@ -487,6 +495,13 @@ protected void writeConstant(ConstStmtToken constant) {

@SuppressWarnings("unchecked")
protected void writeSystemInfo() {
node.fields.add(new FieldNode(
ACC_PUBLIC, "$THIS",
Type.getDescriptor(Memory.class),
null,
null
));

node.fields.add(new FieldNode(
ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "$FN",
Type.getDescriptor(String.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1467,18 +1467,24 @@ public void writePushThis() {
writeVarLoad("~this");
writeGetDynamic("self", Memory.class);
} else {
if (method.getLocalVariable("this") == null) {
if (!methodStatement.isStatic()) {
if (!methodStatement.isStatic()) {
if (method.getLocalVariable("this") == null) {
LabelNode label = writeLabel(node);
LocalVariable local = method.addLocalVariable("this", label, Memory.class);
writeDefineThis(local, null);
} else {
writePushNull();
return;

if (method.clazz.entity.isTrait()) { // if trait we need get this dynamically
writeDefineThis(local, null);
} else { // in normal case, get by this
writeVarLoad("~this");
writeGetDynamic("$THIS", Memory.class);
writeVarStore(local, false, false);
}
}
}

writeVarLoad("this");
writeVarLoad("this");
} else {
writePushNull();
}
}
}

Expand Down

0 comments on commit 6af9aaf

Please sign in to comment.