From f4c5b5073a65e4560080d242ab85f32e31756065 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 5 Nov 2017 16:12:31 +0300 Subject: [PATCH] Minor performance improvement --- .../teavm/dependency/DependencyChecker.java | 33 +++++++++++++++++-- .../dependency/DependencyClassSource.java | 1 - .../DependencyNodeToNodeTransition.java | 33 +++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/teavm/dependency/DependencyChecker.java b/core/src/main/java/org/teavm/dependency/DependencyChecker.java index 45e980e8c..eddf33d53 100644 --- a/core/src/main/java/org/teavm/dependency/DependencyChecker.java +++ b/core/src/main/java/org/teavm/dependency/DependencyChecker.java @@ -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 classCache; private List listeners = new ArrayList<>(); private ServiceRepository services; - private Queue tasks = new ArrayDeque<>(); + private Deque tasks = new ArrayDeque<>(); private Queue deferredTasks = new ArrayDeque<>(); List types = new ArrayList<>(); private Map 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; diff --git a/core/src/main/java/org/teavm/dependency/DependencyClassSource.java b/core/src/main/java/org/teavm/dependency/DependencyClassSource.java index 818ee956a..ecccc9ac0 100644 --- a/core/src/main/java/org/teavm/dependency/DependencyClassSource.java +++ b/core/src/main/java/org/teavm/dependency/DependencyClassSource.java @@ -72,7 +72,6 @@ class DependencyClassSource implements ClassHolderSource { for (ClassHolderTransformer transformer : transformers) { transformer.transformClass(cls, innerSource, diagnostics); } - cls = ModelUtils.copyClass(cls); } return cls; } diff --git a/core/src/main/java/org/teavm/dependency/DependencyNodeToNodeTransition.java b/core/src/main/java/org/teavm/dependency/DependencyNodeToNodeTransition.java index 10e1667b5..935d6169f 100644 --- a/core/src/main/java/org/teavm/dependency/DependencyNodeToNodeTransition.java +++ b/core/src/main/java/org/teavm/dependency/DependencyNodeToNodeTransition.java @@ -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); + } + } }