Skip to content

Commit 6af9aaf

Browse files
committed
Improve performance of getting this variable in methods.
1 parent 17eddd0 commit 6af9aaf

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/ClassStmtCompiler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ protected void writeConstructor() {
382382
false
383383
));
384384

385+
LabelNode label = expressionCompiler.writeLabel(methodCompiler.node);
386+
LocalVariable local = methodCompiler.addLocalVariable("$THIS", label, Memory.class);
387+
expressionCompiler.writeDefineThis(local, null);
388+
389+
expressionCompiler.writeVarLoad("~this");
390+
expressionCompiler.writeVarLoad("$THIS");
391+
expressionCompiler.writePutDynamic("$THIS", Memory.class);
392+
385393
// PROPERTIES
386394
for (ClassVarStmtToken property : statement.getProperties()) {
387395
ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(methodCompiler, null);
@@ -487,6 +495,13 @@ protected void writeConstant(ConstStmtToken constant) {
487495

488496
@SuppressWarnings("unchecked")
489497
protected void writeSystemInfo() {
498+
node.fields.add(new FieldNode(
499+
ACC_PUBLIC, "$THIS",
500+
Type.getDescriptor(Memory.class),
501+
null,
502+
null
503+
));
504+
490505
node.fields.add(new FieldNode(
491506
ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "$FN",
492507
Type.getDescriptor(String.class),

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/ExpressionStmtCompiler.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,18 +1467,24 @@ public void writePushThis() {
14671467
writeVarLoad("~this");
14681468
writeGetDynamic("self", Memory.class);
14691469
} else {
1470-
if (method.getLocalVariable("this") == null) {
1471-
if (!methodStatement.isStatic()) {
1470+
if (!methodStatement.isStatic()) {
1471+
if (method.getLocalVariable("this") == null) {
14721472
LabelNode label = writeLabel(node);
14731473
LocalVariable local = method.addLocalVariable("this", label, Memory.class);
1474-
writeDefineThis(local, null);
1475-
} else {
1476-
writePushNull();
1477-
return;
1474+
1475+
if (method.clazz.entity.isTrait()) { // if trait we need get this dynamically
1476+
writeDefineThis(local, null);
1477+
} else { // in normal case, get by this
1478+
writeVarLoad("~this");
1479+
writeGetDynamic("$THIS", Memory.class);
1480+
writeVarStore(local, false, false);
1481+
}
14781482
}
1479-
}
14801483

1481-
writeVarLoad("this");
1484+
writeVarLoad("this");
1485+
} else {
1486+
writePushNull();
1487+
}
14821488
}
14831489
}
14841490

0 commit comments

Comments
 (0)