jps model: cache role instances to optimize memory usage

This commit is contained in:
nik
2016-05-05 13:45:26 +03:00
parent fc11164224
commit a2284824ff
3 changed files with 26 additions and 5 deletions
@@ -25,7 +25,7 @@ public class JpsElementChildRoleBase<E extends JpsElement> extends JpsElementChi
private String myDebugName;
protected JpsElementChildRoleBase(String debugName) {
myDebugName = debugName.intern();
myDebugName = debugName;
}
@Override
@@ -28,12 +28,17 @@ import org.jetbrains.jps.model.library.*;
import org.jetbrains.jps.util.JpsPathUtil;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
/**
* @author nik
*/
public class JpsLibraryImpl<P extends JpsElement> extends JpsNamedCompositeElementBase<JpsLibraryImpl<P>> implements JpsTypedLibrary<P> {
private static final ConcurrentMap<JpsOrderRootType, JpsElementCollectionRole<JpsLibraryRoot>> ourRootRoles = ContainerUtil.newConcurrentMap();
private final JpsLibraryType<P> myLibraryType;
public JpsLibraryImpl(@NotNull String name, @NotNull JpsLibraryType<P> type, @NotNull P properties) {
@@ -103,7 +108,10 @@ public class JpsLibraryImpl<P extends JpsElement> extends JpsNamedCompositeEleme
}
private static JpsElementCollectionRole<JpsLibraryRoot> getRole(JpsOrderRootType type) {
return JpsElementCollectionRole.create(new JpsLibraryRootRole(type));
JpsElementCollectionRole<JpsLibraryRoot> role = ourRootRoles.get(type);
if (role != null) return role;
ourRootRoles.putIfAbsent(type, JpsElementCollectionRole.create(new JpsLibraryRootRole(type)));
return ourRootRoles.get(type);
}
@Override
@@ -15,6 +15,7 @@
*/
package org.jetbrains.jps.model.module.impl;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsElement;
@@ -25,11 +26,14 @@ import org.jetbrains.jps.model.library.sdk.JpsSdkReference;
import org.jetbrains.jps.model.library.sdk.JpsSdkType;
import org.jetbrains.jps.model.module.JpsSdkReferencesTable;
import java.util.concurrent.ConcurrentMap;
/**
* @author nik
*/
public class JpsSdkReferencesTableImpl extends JpsCompositeElementBase<JpsSdkReferencesTableImpl> implements JpsSdkReferencesTable {
public static final JpsSdkReferencesTableRole ROLE = new JpsSdkReferencesTableRole();
private static final ConcurrentMap<JpsSdkType, JpsSdkReferenceRole> ourReferenceRoles = ContainerUtil.newConcurrentMap();
public JpsSdkReferencesTableImpl() {
super();
@@ -47,7 +51,7 @@ public class JpsSdkReferencesTableImpl extends JpsCompositeElementBase<JpsSdkRef
@Override
public <P extends JpsElement> void setSdkReference(@NotNull JpsSdkType<P> type, @Nullable JpsSdkReference<P> sdkReference) {
JpsSdkReferenceRole<P> role = new JpsSdkReferenceRole<P>(type);
JpsSdkReferenceRole<P> role = getSdkReferenceRole(type);
if (sdkReference != null) {
myContainer.setChild(role, sdkReference);
}
@@ -58,7 +62,16 @@ public class JpsSdkReferencesTableImpl extends JpsCompositeElementBase<JpsSdkRef
@Override
public <P extends JpsElement> JpsSdkReference<P> getSdkReference(@NotNull JpsSdkType<P> type) {
return myContainer.getChild(new JpsSdkReferenceRole<P>(type));
return myContainer.getChild(getSdkReferenceRole(type));
}
@SuppressWarnings("unchecked")
@NotNull
private static <P extends JpsElement> JpsSdkReferenceRole<P> getSdkReferenceRole(@NotNull JpsSdkType<P> type) {
JpsSdkReferenceRole<P> role = ourReferenceRoles.get(type);
if (role != null) return role;
ourReferenceRoles.putIfAbsent(type, new JpsSdkReferenceRole<P>(type));
return ourReferenceRoles.get(type);
}
private static class JpsSdkReferencesTableRole extends JpsElementChildRoleBase<JpsSdkReferencesTable> implements JpsElementCreator<JpsSdkReferencesTable> {