mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Fix performance regression. See
https://github.com/konsoletyper/teavm/issues/102
This commit is contained in:
parent
5db66bb17c
commit
98e2142332
|
@ -156,6 +156,6 @@ class BreakEliminator implements StatementVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean escapes(List<Statement> statements) {
|
private boolean escapes(List<Statement> statements) {
|
||||||
return new EscapingStatementFinder().check(statements);
|
return new EscapingStatementFinder(usageCounter).check(statements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.teavm.javascript;
|
package org.teavm.javascript;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
import org.teavm.javascript.ast.*;
|
import org.teavm.javascript.ast.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,39 +23,48 @@ import org.teavm.javascript.ast.*;
|
||||||
* @author Alexey Andreev
|
* @author Alexey Andreev
|
||||||
*/
|
*/
|
||||||
class EscapingStatementFinder implements StatementVisitor {
|
class EscapingStatementFinder implements StatementVisitor {
|
||||||
|
AllBlocksCountVisitor blockCountVisitor;
|
||||||
public boolean escaping;
|
public boolean escaping;
|
||||||
private boolean last = true;
|
|
||||||
private Set<IdentifiedStatement> outerStatements = new HashSet<>();
|
public EscapingStatementFinder(AllBlocksCountVisitor blockCountVisitor) {
|
||||||
private Set<IdentifiedStatement> breakTargets = new HashSet<>();
|
this.blockCountVisitor = blockCountVisitor;
|
||||||
private IdentifiedStatement currentBlock;
|
}
|
||||||
|
|
||||||
|
private boolean isEmpty(Statement statement) {
|
||||||
|
if (!(statement instanceof SequentialStatement)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
SequentialStatement seq = (SequentialStatement)statement;
|
||||||
|
for (int i = seq.getSequence().size() - 1; i >= 0; --i) {
|
||||||
|
if (!isEmpty(seq.getSequence().get(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean check(List<Statement> statements) {
|
public boolean check(List<Statement> statements) {
|
||||||
if (escaping) {
|
if (escaping) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (statements.isEmpty()) {
|
if (statements.isEmpty()) {
|
||||||
escaping = last;
|
escaping = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
boolean oldLast = last;
|
for (int i = statements.size() - 1; i >= 0; --i) {
|
||||||
for (int i = 0; i < statements.size(); ++i) {
|
Statement stmt = statements.get(i);
|
||||||
last = false;
|
if (!isEmpty(stmt)) {
|
||||||
statements.get(i).acceptVisitor(this);
|
stmt.acceptVisitor(this);
|
||||||
if (escaping) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
last = oldLast;
|
|
||||||
if (!escaping) {
|
|
||||||
statements.get(statements.size() - 1).acceptVisitor(this);
|
|
||||||
}
|
|
||||||
last = oldLast;
|
|
||||||
return escaping;
|
return escaping;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
escaping = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(AssignmentStatement statement) {
|
public void visit(AssignmentStatement statement) {
|
||||||
escaping |= last;
|
escaping |= true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,56 +81,42 @@ class EscapingStatementFinder implements StatementVisitor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(SwitchStatement statement) {
|
public void visit(SwitchStatement statement) {
|
||||||
IdentifiedStatement oldCurrentBlock = currentBlock;
|
if (blockCountVisitor.getCount(statement) > 0) {
|
||||||
currentBlock = statement;
|
escaping = true;
|
||||||
outerStatements.add(statement);
|
return;
|
||||||
|
}
|
||||||
for (SwitchClause clause : statement.getClauses()) {
|
for (SwitchClause clause : statement.getClauses()) {
|
||||||
if (check(clause.getBody())) {
|
if (check(clause.getBody())) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!escaping) {
|
||||||
check(statement.getDefaultClause());
|
check(statement.getDefaultClause());
|
||||||
outerStatements.remove(statement);
|
|
||||||
currentBlock = oldCurrentBlock;
|
|
||||||
if (breakTargets.contains(statement)) {
|
|
||||||
escaping |= last;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(WhileStatement statement) {
|
public void visit(WhileStatement statement) {
|
||||||
IdentifiedStatement oldCurrentBlock = currentBlock;
|
if (blockCountVisitor.getCount(statement) > 0) {
|
||||||
currentBlock = statement;
|
escaping = true;
|
||||||
outerStatements.add(statement);
|
return;
|
||||||
if (!check(statement.getBody()) && statement.getCondition() != null) {
|
|
||||||
escaping |= last;
|
|
||||||
}
|
}
|
||||||
outerStatements.remove(statement);
|
if (statement.getCondition() != null && check(statement.getBody())) {
|
||||||
currentBlock = oldCurrentBlock;
|
escaping = true;
|
||||||
if (breakTargets.contains(statement)) {
|
|
||||||
escaping |= last;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(BlockStatement statement) {
|
public void visit(BlockStatement statement) {
|
||||||
IdentifiedStatement oldCurrentBlock = currentBlock;
|
if (blockCountVisitor.getCount(statement) > 0) {
|
||||||
currentBlock = statement;
|
escaping = true;
|
||||||
outerStatements.add(statement);
|
return;
|
||||||
check(statement.getBody());
|
|
||||||
outerStatements.remove(statement);
|
|
||||||
currentBlock = oldCurrentBlock;
|
|
||||||
if (breakTargets.contains(statement)) {
|
|
||||||
escaping |= last;
|
|
||||||
}
|
}
|
||||||
|
check(statement.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(BreakStatement statement) {
|
public void visit(BreakStatement statement) {
|
||||||
IdentifiedStatement target = statement.getTarget() != null ? statement.getTarget() : currentBlock;
|
|
||||||
if (target != null) {
|
|
||||||
breakTargets.add(target);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -140,14 +133,15 @@ class EscapingStatementFinder implements StatementVisitor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(InitClassStatement statement) {
|
public void visit(InitClassStatement statement) {
|
||||||
escaping |= last;
|
escaping = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(TryCatchStatement statement) {
|
public void visit(TryCatchStatement statement) {
|
||||||
check(statement.getProtectedBody());
|
if (!check(statement.getProtectedBody())) {
|
||||||
check(statement.getHandler());
|
check(statement.getHandler());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(GotoPartStatement statement) {
|
public void visit(GotoPartStatement statement) {
|
||||||
|
@ -155,11 +149,11 @@ class EscapingStatementFinder implements StatementVisitor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MonitorEnterStatement statement) {
|
public void visit(MonitorEnterStatement statement) {
|
||||||
escaping |= last;
|
escaping = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MonitorExitStatement statement) {
|
public void visit(MonitorExitStatement statement) {
|
||||||
escaping |= last;
|
escaping = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user