Aviod creation of closures in async methods

This commit is contained in:
Alexey Andreev 2015-03-12 18:11:04 +04:00
parent 4ab4035522
commit 3a29e869ef
2 changed files with 12 additions and 15 deletions

View File

@ -698,18 +698,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(";").softNewLine(); writer.append(";").softNewLine();
} }
writer.append("function $save()").ws().append("{").indent().softNewLine();
writer.append("$rt_nativeThread()");
int firstToSave = 0; int firstToSave = 0;
if (methodNode.getModifiers().contains(NodeModifier.STATIC)) { if (methodNode.getModifiers().contains(NodeModifier.STATIC)) {
firstToSave = 1; firstToSave = 1;
} }
for (int i = firstToSave; i < variableCount; ++i) {
writer.append(".push(").append(variableName(i)).append(")");
}
writer.append(".push($ptr);");
writer.softNewLine();
writer.outdent().append("}").softNewLine();
writer.append("$ptr").ws().append('=').ws().append("0;").softNewLine(); writer.append("$ptr").ws().append('=').ws().append("0;").softNewLine();
writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine(); writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine();
@ -721,8 +713,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.softNewLine(); writer.softNewLine();
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
writer.append("while").ws().append("(true)").ws().append("{").indent().softNewLine(); writer.append("$main:").ws().append("while").ws().append("(true)").ws().append("{").ws();
writer.append("$main: switch").ws().append("($ptr)").ws().append('{').softNewLine(); writer.append("switch").ws().append("($ptr)").ws().append('{').softNewLine();
for (int i = 0; i < methodNode.getBody().size(); ++i) { for (int i = 0; i < methodNode.getBody().size(); ++i) {
writer.append("case ").append(i).append(":").indent().softNewLine(); writer.append("case ").append(i).append(":").indent().softNewLine();
AsyncMethodPart part = methodNode.getBody().get(i); AsyncMethodPart part = methodNode.getBody().get(i);
@ -730,8 +722,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.outdent(); writer.outdent();
} }
writer.append("default:").ws().append("throw new Error('Invalid recorded state');").softNewLine(); writer.append("default:").ws().append("throw new Error('Invalid recorded state');").softNewLine();
writer.append("}").softNewLine(); writer.append("}}").softNewLine();
writer.outdent().append("}").softNewLine(); writer.append("$rt_nativeThread()");
for (int i = firstToSave; i < variableCount; ++i) {
writer.append(".push(").append(variableName(i)).append(")");
}
writer.append(".push($ptr);");
writer.softNewLine();
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
} }
@ -1982,7 +1979,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
try { try {
writer.append("$ptr").ws().append("=").ws().append(statement.getPart()).append(";") writer.append("$ptr").ws().append("=").ws().append(statement.getPart()).append(";")
.softNewLine(); .softNewLine();
writer.append("break $main;").softNewLine(); writer.append("continue $main;").softNewLine();
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }
@ -2012,7 +2009,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private void emitSuspendChecker() throws IOException { private void emitSuspendChecker() throws IOException {
writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine();
writer.append("return $save();").softNewLine(); writer.append("break $main;").softNewLine();
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
} }

View File

@ -73,7 +73,7 @@ public final class AsyncProgram {
private static void findPrimes() { private static void findPrimes() {
report("Finding primes"); report("Finding primes");
boolean[] prime = new boolean[1000]; boolean[] prime = new boolean[1000000];
prime[2] = true; prime[2] = true;
prime[3] = true; prime[3] = true;
nextPrime: for (int i = 5; i < prime.length; i += 2) { nextPrime: for (int i = 5; i < prime.length; i += 2) {