TeaVM(C): fix unnecessary call to Fiber.isResuming

This commit is contained in:
Alexey Andreev 2019-06-25 14:03:02 +03:00
parent c430578426
commit b6cfbe7f5b
4 changed files with 17 additions and 13 deletions

View File

@ -152,6 +152,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
private List<GeneratorFactory> generatorFactories = new ArrayList<>(); private List<GeneratorFactory> generatorFactories = new ArrayList<>();
private Characteristics characteristics; private Characteristics characteristics;
private Set<MethodReference> asyncMethods; private Set<MethodReference> asyncMethods;
private boolean hasThreads;
private MethodNodeCache astCache = EmptyMethodNodeCache.INSTANCE; private MethodNodeCache astCache = EmptyMethodNodeCache.INSTANCE;
private boolean incremental; private boolean incremental;
private boolean lineNumbersGenerated; private boolean lineNumbersGenerated;
@ -285,11 +286,11 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
@Override @Override
public void analyzeBeforeOptimizations(ListableClassReaderSource classSource) { public void analyzeBeforeOptimizations(ListableClassReaderSource classSource) {
AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph(), AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph());
controller.getDiagnostics());
asyncFinder.find(classSource); asyncFinder.find(classSource);
asyncMethods = new HashSet<>(asyncFinder.getAsyncMethods()); asyncMethods = new HashSet<>(asyncFinder.getAsyncMethods());
asyncMethods.addAll(asyncFinder.getAsyncFamilyMethods()); asyncMethods.addAll(asyncFinder.getAsyncFamilyMethods());
hasThreads = asyncFinder.hasAsyncMethods();
} }
@Override @Override
@ -302,7 +303,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
classInitializerEliminator.apply(program); classInitializerEliminator.apply(program);
classInitializerTransformer.transform(program); classInitializerTransformer.transform(program);
nullCheckTransformation.apply(program, method.getResultType()); nullCheckTransformation.apply(program, method.getResultType());
new CoroutineTransformation(controller.getUnprocessedClassSource(), asyncMethods) new CoroutineTransformation(controller.getUnprocessedClassSource(), asyncMethods, hasThreads)
.apply(program, method.getReference()); .apply(program, method.getReference());
ShadowStackTransformer shadowStackTransformer = !incremental ShadowStackTransformer shadowStackTransformer = !incremental
? this.shadowStackTransformer ? this.shadowStackTransformer

View File

@ -470,8 +470,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost {
} }
private List<PreparedClass> modelToAst(ListableClassHolderSource classes) { private List<PreparedClass> modelToAst(ListableClassHolderSource classes) {
AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph(), AsyncMethodFinder asyncFinder = new AsyncMethodFinder(controller.getDependencyInfo().getCallGraph());
controller.getDiagnostics());
asyncFinder.find(classes); asyncFinder.find(classes);
asyncMethods.addAll(asyncFinder.getAsyncMethods()); asyncMethods.addAll(asyncFinder.getAsyncMethods());
asyncFamilyMethods.addAll(asyncFinder.getAsyncFamilyMethods()); asyncFamilyMethods.addAll(asyncFinder.getAsyncFamilyMethods());

View File

@ -79,10 +79,13 @@ public class CoroutineTransformation {
private SwitchInstruction resumeSwitch; private SwitchInstruction resumeSwitch;
private int parameterCount; private int parameterCount;
private ValueType returnType; private ValueType returnType;
private boolean hasThreads;
public CoroutineTransformation(ClassReaderSource classSource, Set<MethodReference> asyncMethods) { public CoroutineTransformation(ClassReaderSource classSource, Set<MethodReference> asyncMethods,
boolean hasThreads) {
this.classSource = classSource; this.classSource = classSource;
this.asyncMethods = asyncMethods; this.asyncMethods = asyncMethods;
this.hasThreads = hasThreads;
} }
public void apply(Program program, MethodReference methodReference) { public void apply(Program program, MethodReference methodReference) {
@ -233,7 +236,7 @@ public class CoroutineTransformation {
} else if (instruction instanceof InitClassInstruction) { } else if (instruction instanceof InitClassInstruction) {
return isSplittingClassInitializer(((InitClassInstruction) instruction).getClassName()); return isSplittingClassInitializer(((InitClassInstruction) instruction).getClassName());
} else { } else {
return instruction instanceof MonitorEnterInstruction; return hasThreads && instruction instanceof MonitorEnterInstruction;
} }
} }

View File

@ -25,7 +25,6 @@ import java.util.Set;
import org.teavm.callgraph.CallGraph; import org.teavm.callgraph.CallGraph;
import org.teavm.callgraph.CallGraphNode; import org.teavm.callgraph.CallGraphNode;
import org.teavm.callgraph.CallSite; import org.teavm.callgraph.CallSite;
import org.teavm.diagnostics.Diagnostics;
import org.teavm.interop.Async; import org.teavm.interop.Async;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.ElementModifier; import org.teavm.model.ElementModifier;
@ -44,13 +43,11 @@ public class AsyncMethodFinder {
private Set<MethodReference> readonlyAsyncMethods = Collections.unmodifiableSet(asyncMethods); private Set<MethodReference> readonlyAsyncMethods = Collections.unmodifiableSet(asyncMethods);
private Set<MethodReference> readonlyAsyncFamilyMethods = Collections.unmodifiableSet(asyncFamilyMethods.keySet()); private Set<MethodReference> readonlyAsyncFamilyMethods = Collections.unmodifiableSet(asyncFamilyMethods.keySet());
private CallGraph callGraph; private CallGraph callGraph;
private Diagnostics diagnostics;
private ListableClassReaderSource classSource; private ListableClassReaderSource classSource;
private boolean hasAsyncMethods; private boolean hasAsyncMethods;
public AsyncMethodFinder(CallGraph callGraph, Diagnostics diagnostics) { public AsyncMethodFinder(CallGraph callGraph) {
this.callGraph = callGraph; this.callGraph = callGraph;
this.diagnostics = diagnostics;
} }
public Set<MethodReference> getAsyncMethods() { public Set<MethodReference> getAsyncMethods() {
@ -63,7 +60,7 @@ public class AsyncMethodFinder {
public void find(ListableClassReaderSource classSource) { public void find(ListableClassReaderSource classSource) {
this.classSource = classSource; this.classSource = classSource;
hasAsyncMethods = hasAsyncMethods(); hasAsyncMethods = findAsyncMethods();
for (String clsName : classSource.getClassNames()) { for (String clsName : classSource.getClassNames()) {
ClassReader cls = classSource.get(clsName); ClassReader cls = classSource.get(clsName);
for (MethodReader method : cls.getMethods()) { for (MethodReader method : cls.getMethods()) {
@ -104,7 +101,7 @@ public class AsyncMethodFinder {
} }
} }
private boolean hasAsyncMethods() { private boolean findAsyncMethods() {
boolean result = false; boolean result = false;
loop: for (String clsName : classSource.getClassNames()) { loop: for (String clsName : classSource.getClassNames()) {
ClassReader cls = classSource.get(clsName); ClassReader cls = classSource.get(clsName);
@ -123,6 +120,10 @@ public class AsyncMethodFinder {
return result && method != null; return result && method != null;
} }
public boolean hasAsyncMethods() {
return hasAsyncMethods;
}
private boolean hasMonitor(MethodReader method) { private boolean hasMonitor(MethodReader method) {
if (method.hasModifier(ElementModifier.SYNCHRONIZED)) { if (method.hasModifier(ElementModifier.SYNCHRONIZED)) {
return true; return true;