From 7fb1f1c146106d4a1ff36eb3cd49b963329fd70f Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 30 Nov 2016 18:22:11 +0300 Subject: [PATCH] memory optimisation: eliminate TIntArrayList slack --- .../intellij/vcs/log/data/CompressedRefs.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/data/CompressedRefs.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/data/CompressedRefs.java index 772579231019..f0c04dcecf5d 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/data/CompressedRefs.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/data/CompressedRefs.java @@ -27,10 +27,7 @@ import gnu.trove.TIntArrayList; import gnu.trove.TIntObjectHashMap; import org.jetbrains.annotations.NotNull; -import java.util.AbstractCollection; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -38,16 +35,13 @@ public class CompressedRefs { @NotNull private final VcsLogStorage myHashMap; // maps each commit id to the list of tag ids on this commit - @NotNull private final TIntObjectHashMap myTags; + @NotNull private final TIntObjectHashMap myTags = new TIntObjectHashMap<>(); // maps each commit id to the list of branches on this commit - @NotNull private final TIntObjectHashMap> myBranches; + @NotNull private final TIntObjectHashMap> myBranches = new TIntObjectHashMap<>(); public CompressedRefs(@NotNull Set refs, @NotNull VcsLogStorage hashMap) { myHashMap = hashMap; - myTags = new TIntObjectHashMap<>(); - myBranches = new TIntObjectHashMap<>(); - Ref root = new Ref<>(); refs.forEach(ref -> { @@ -61,12 +55,15 @@ public class CompressedRefs { putRefIndex(myTags, ref, myHashMap); } }); - + myTags.forEachValue(list -> { + list.trimToSize(); + return true; + }); myHashMap.flush(); } @NotNull - public SmartList refsToCommit(int index) { + SmartList refsToCommit(int index) { SmartList result = new SmartList<>(); if (myBranches.containsKey(index)) result.addAll(myBranches.get(index)); TIntArrayList tags = myTags.get(index); @@ -80,7 +77,7 @@ public class CompressedRefs { } @NotNull - public Stream streamBranches() { + Stream streamBranches() { return TroveUtil.streamValues(myBranches).flatMap(Collection::stream); } @@ -100,6 +97,7 @@ public class CompressedRefs { private final Supplier> myLoadedRefs = Suppliers.memoize(() -> CompressedRefs.this.stream().collect(Collectors.toList())); + @NotNull @Override public Iterator iterator() { return myLoadedRefs.get().iterator(); @@ -120,14 +118,14 @@ public class CompressedRefs { return result; } - public static void putRef(@NotNull TIntObjectHashMap> map, @NotNull VcsRef ref, @NotNull VcsLogStorage hashMap) { + private static void putRef(@NotNull TIntObjectHashMap> map, @NotNull VcsRef ref, @NotNull VcsLogStorage hashMap) { int index = hashMap.getCommitIndex(ref.getCommitHash(), ref.getRoot()); - SmartList list = map.get(index); + List list = map.get(index); if (list == null) map.put(index, list = new SmartList<>()); list.add(ref); } - public static void putRefIndex(@NotNull TIntObjectHashMap map, @NotNull VcsRef ref, @NotNull VcsLogStorage hashMap) { + private static void putRefIndex(@NotNull TIntObjectHashMap map, @NotNull VcsRef ref, @NotNull VcsLogStorage hashMap) { int index = hashMap.getCommitIndex(ref.getCommitHash(), ref.getRoot()); TIntArrayList list = map.get(index); if (list == null) map.put(index, list = new TIntArrayList());