Optimize control-flow analysis by use of persistent maps

The case that it's worth to optimize is functions
with a lot of variables.

After debugging it's recovered that control-flow works nearly
O(n * m) where n is pseudocode size and m is a number of variables:
the algorithm performs O(n) copies of hashmap of size O(m)

Persistent maps should help because we don't need to perform a
copy of them, so the expected performance after the change is applied
is O(n log m)

We've tried pcollections and javaslang, and the latter one has demonstrated
better results

See results before and after optimizations
before:
https://github.com/dzharkov/kotlin-compiler-benchmarks/blob/3da7ba45a704969653d70b50995f730e968540d8/reports/benchmarks-many-vars-2017-03-14.txt

after with pcollections:
https://github.com/dzharkov/kotlin-compiler-benchmarks/blob/3da7ba45a704969653d70b50995f730e968540d8/reports/benchmarks-many-vars-persistent-optimizations-2017-03-17.txt

after with javaslang:
https://github.com/dzharkov/kotlin-compiler-benchmarks/blob/d22a871b175b291fb337b51ef6465ba70bbfd96c/reports/benchmarks-many-vars-javaslang-2017-04-07.txt
This commit is contained in:
Denis Zharkov
2017-04-07 18:38:12 +03:00
parent 9e61eea758
commit 5e449fdc02
15 changed files with 135 additions and 81 deletions
+1
View File
@@ -35,5 +35,6 @@
<orderEntry type="module" module-name="ir.psi2ir" scope="TEST" />
<orderEntry type="module" module-name="ir.ir2cfg" scope="TEST" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="library" name="javaslang" level="project" />
</component>
</module>
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cfg;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.text.StringUtil;
import javaslang.Tuple2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cfg.pseudocode.PseudocodeImpl;
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction;
@@ -79,21 +80,22 @@ public abstract class AbstractDataFlowTest extends AbstractPseudocodeTest {
maxWidth = length;
}
}
return maxWidth;
}
@NotNull
private static <S, I extends ControlFlowInfo<S>> String dumpEdgesData(String prefix, @NotNull Edges<I> edges) {
private static <S, I extends ControlFlowInfo<?, S>> String dumpEdgesData(String prefix, @NotNull Edges<I> edges) {
return prefix +
" in: " + renderVariableMap(edges.getIncoming()) +
" out: " + renderVariableMap(edges.getOutgoing());
}
private static <S> String renderVariableMap(Map<VariableDescriptor, S> map) {
private static <S> String renderVariableMap(javaslang.collection.Map<VariableDescriptor, S> map) {
List<String> result = Lists.newArrayList();
for (Map.Entry<VariableDescriptor, S> entry : map.entrySet()) {
VariableDescriptor variable = entry.getKey();
S state = entry.getValue();
for (Tuple2<VariableDescriptor, S> entry : map) {
VariableDescriptor variable = entry._1;
S state = entry._2;
result.add(variable.getName() + "=" + state);
}
Collections.sort(result);