Minor performance improvement

This commit is contained in:
Alexey Andreev 2017-11-05 16:12:31 +03:00
parent a9d47b5ce7
commit f4c5b5073a
3 changed files with 64 additions and 3 deletions

View File

@ -18,6 +18,7 @@ package org.teavm.dependency;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -67,7 +68,7 @@ public class DependencyChecker implements DependencyInfo {
private CachedMapper<String, ClassDependency> classCache;
private List<DependencyListener> listeners = new ArrayList<>();
private ServiceRepository services;
private Queue<Runnable> tasks = new ArrayDeque<>();
private Deque<Runnable> tasks = new ArrayDeque<>();
private Queue<Runnable> deferredTasks = new ArrayDeque<>();
List<DependencyType> types = new ArrayList<>();
private Map<String, DependencyType> typeMap = new HashMap<>();
@ -245,7 +246,35 @@ public class DependencyChecker implements DependencyInfo {
}
}
void schedulePropagation(DependencyNodeToNodeTransition consumer, DependencyType[] types) {
if (types.length == 0) {
return;
}
if (types.length == 1) {
schedulePropagation(consumer, types[0]);
return;
}
if (propagationDepth < 50) {
++propagationDepth;
consumer.consume(types);
--propagationDepth;
} else {
tasks.add(() -> {
consumer.consume(types);
});
}
}
void schedulePropagation(DependencyConsumer consumer, DependencyType[] types) {
if (types.length == 0) {
return;
}
if (types.length == 1) {
schedulePropagation(consumer, types[0]);
return;
}
if (propagationDepth < 50) {
++propagationDepth;
for (DependencyType type : types) {
@ -511,7 +540,7 @@ public class DependencyChecker implements DependencyInfo {
int index = 0;
while (true) {
while (!tasks.isEmpty()) {
tasks.poll().run();
tasks.removeLast().run();
if (++index == 100) {
if (interruptor != null && !interruptor.shouldContinue()) {
interrupted = true;

View File

@ -72,7 +72,6 @@ class DependencyClassSource implements ClassHolderSource {
for (ClassHolderTransformer transformer : transformers) {
transformer.transformClass(cls, innerSource, diagnostics);
}
cls = ModelUtils.copyClass(cls);
}
return cls;
}

View File

@ -15,6 +15,8 @@
*/
package org.teavm.dependency;
import java.util.Arrays;
class DependencyNodeToNodeTransition implements DependencyConsumer {
private DependencyNode source;
DependencyNode destination;
@ -43,4 +45,35 @@ class DependencyNodeToNodeTransition implements DependencyConsumer {
destination.propagate(type);
}
}
void consume(DependencyType[] types) {
DependencyType[] filtered = new DependencyType[types.length];
int j = 0;
for (DependencyType type : types) {
if (type.getName().startsWith("[")) {
source.getArrayItem().connect(destination.getArrayItem());
destination.getArrayItem().connect(source.getArrayItem());
}
if (type.getName().equals("java.lang.Class")) {
source.getClassValueNode().connect(destination.getClassValueNode());
}
if ((filter == null || filter.match(type)) && !destination.hasType(type)) {
filtered[j++] = type;
}
}
if (j == 0) {
return;
}
if (j == 1) {
destination.propagate(filtered[0]);
} else {
if (j < filtered.length) {
filtered = Arrays.copyOf(filtered, j);
}
destination.propagate(filtered);
}
}
}