From ec0d5547a69b2d56d49001448dbb9d3a1837e3eb Mon Sep 17 00:00:00 2001 From: shafirov Date: Mon, 8 Jul 2019 19:22:00 +0300 Subject: [PATCH] Memory optimizations Optimize for a) no errors case (most of them) b) no dependencies case (all of the library nodes). These two account for around 50mb of memory usage GitOrigin-RevId: e92d6a1e5d50e7bfe94a41949905a576e80206d3 --- .../view/ExternalSystemNode.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/view/ExternalSystemNode.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/view/ExternalSystemNode.java index 98000ca21cff..08639e8767a5 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/view/ExternalSystemNode.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/view/ExternalSystemNode.java @@ -58,14 +58,17 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar protected static final ExternalSystemNode[] NO_CHILDREN = new ExternalSystemNode[0]; + private static final List> NO_CHILDREN_LIST = Collections.emptyList(); + private static final List NO_ERRORS_LIST = Collections.emptyList(); + private final ExternalProjectsView myExternalProjectsView; - private final List> myChildrenList = new ArrayList<>(); + private List> myChildrenList = NO_CHILDREN_LIST; protected DataNode myDataNode; @Nullable private ExternalSystemNode myParent; private ExternalSystemNode[] myChildren; private ExternalProjectsStructure.ErrorLevel myErrorLevel = ExternalProjectsStructure.ErrorLevel.NONE; - private final List myErrors = new ArrayList<>(); + private List myErrors = NO_ERRORS_LIST; private ExternalProjectsStructure.ErrorLevel myTotalErrorLevel = null; public ExternalSystemNode(@NotNull ExternalProjectsView externalProjectsView, @@ -209,7 +212,7 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar public void cleanUpCache() { myChildren = null; - myChildrenList.clear(); + myChildrenList = NO_CHILDREN_LIST; myTotalErrorLevel = null; } @@ -219,7 +222,9 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar } protected void sort(List list) { - Collections.sort(list, ORDER_AWARE_COMPARATOR); + if (!list.isEmpty()) { + Collections.sort(list, ORDER_AWARE_COMPARATOR); + } } public boolean addAll(Collection externalSystemNodes) { @@ -229,6 +234,10 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar private boolean addAll(Collection externalSystemNodes, boolean silently) { if (externalSystemNodes.isEmpty()) return false; + if (myChildrenList == NO_CHILDREN_LIST) { + myChildrenList = new ArrayList<>(); + } + for (ExternalSystemNode externalSystemNode : externalSystemNodes) { externalSystemNode.setParent(this); myChildrenList.add(externalSystemNode); @@ -253,7 +262,7 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar for (ExternalSystemNode externalSystemNode : externalSystemNodes) { externalSystemNode.setParent(null); - myChildrenList.remove(externalSystemNode); + if (myChildrenList != NO_CHILDREN_LIST) myChildrenList.remove(externalSystemNode); } if (!silently) { childrenChanged(); @@ -322,8 +331,13 @@ public abstract class ExternalSystemNode extends SimpleNode implements Compar public void setErrorLevel(ExternalProjectsStructure.ErrorLevel level, String... errors) { if (myErrorLevel == level) return; myErrorLevel = level; - myErrors.clear(); - Collections.addAll(myErrors, errors); + + if (errors.length == 0) { + myErrors = NO_ERRORS_LIST; + } + else { + myErrors = Arrays.asList(errors); + } myExternalProjectsView.updateUpTo(this); }