jps model: cache role instances to optimize memory usage
[idea/community.git] / jps / model-impl / src / org / jetbrains / jps / model / library / impl / JpsLibraryImpl.java
index e2a4c50310040d0999501e319d1fd4291c977c2e..cdbb379c43c9302fa64d5465f4e138ce915e2344 100644 (file)
@@ -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