Reduce compiler runtime overhead of MaxStackFrameSizeAndLocalsCalculator

Profiling the compilation of kotlinx.serialization, MaxStackFrameSizeAndLocalsCalculator
causes ~7% of the runtime to be spent in java.lang.Object.hashCode

This is through two uses:
- visitMaxs(..) has a pushed hashSet that causes ~2%
- labelWrappersMap used to attach additional data to asm Labels, causes ~ 5%

visitMaxs can use the existing SmartSet (not to be confused with SmartHashSet)

Analysis of the visitMaxs HashSet creation & sizes:

| What               | Amount |
| calls to visitMaxs | 4416   |
| max pushed	     | 158    |
| median pushed	     | 4      |
| average pushed     | 5.20   |
| stddev pushed	     | 7.66   |
| 90 percentile      | 10     |

Analysis of labelWrappersMap creation & sizes:

| What               | Amount |
| ------------------ | ------ |
| hashtables created | 4006   |
| max entries        | 175    |
| median entries     | 5      |
| average entries    | 6.10   |
| stdev entries      | 8.28   |
| 90 percentile      | 11     |

testing with a non hash based map using an array for keys and an array for values
showed that the cost of MaxStackFrameSizeAndLocalsCalculator became neglible to
the overall running time.

SmartIdentityTable is a Map like structure that uses reference identity for keys.
It uses 2 arrays to store keys & values until the number of entries stored is larger than 10.
At that point it switches to using an IdentityHashMap.

This structure can be used instead of HashMap when reference identity can be used and
the number of entries inserted is small (<= 10) on average, drastically reducing the overhead
of calls to Object.hashCode

Between the two changes, compilation of kotlinx.serialization through kotlinc
commandline decreased from 14 seconds to 11 seconds on my machine
This commit is contained in:
Jeffrey van Gogh
2019-06-07 15:51:13 -07:00
committed by Alexander Udalov
parent 0b19a4a32b
commit 1dbe487077
3 changed files with 198 additions and 4 deletions
@@ -46,8 +46,9 @@
package org.jetbrains.kotlin.codegen.inline;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.utils.SmartSet;
import org.jetbrains.kotlin.utils.SmartIdentityTable;
import org.jetbrains.org.objectweb.asm.*;
import java.util.*;
@@ -106,7 +107,7 @@ public class MaxStackFrameSizeAndLocalsCalculator extends MaxLocalsCalculator {
private int maxStack;
private final Collection<ExceptionHandler> exceptionHandlers = new LinkedList<>();
private final Map<Label, LabelWrapper> labelWrappersMap = new HashMap<>();
private final SmartIdentityTable<Label, LabelWrapper> labelWrappersTable = new SmartIdentityTable<>();
public MaxStackFrameSizeAndLocalsCalculator(int api, int access, String descriptor, MethodVisitor mv) {
super(api, access, descriptor, mv);
@@ -336,7 +337,7 @@ public class MaxStackFrameSizeAndLocalsCalculator extends MaxLocalsCalculator {
*/
int max = 0;
Stack<LabelWrapper> stack = new Stack<>();
Set<LabelWrapper> pushed = new HashSet<>();
Set<LabelWrapper> pushed = SmartSet.create();
stack.push(firstLabel);
pushed.add(firstLabel);
@@ -434,7 +435,7 @@ public class MaxStackFrameSizeAndLocalsCalculator extends MaxLocalsCalculator {
// ------------------------------------------------------------------------
private LabelWrapper getLabelWrapper(Label label) {
return ContainerUtil.<Label, LabelWrapper>getOrCreate(labelWrappersMap, label, () -> new LabelWrapper(label));
return labelWrappersTable.getOrCreate(label, () -> new LabelWrapper(label));
}
private void increaseStackSize(int variation) {