BUG-6522: Adjust collection sizes 28/47328/3
authorRobert Varga <rovarga@cisco.com>
Fri, 21 Oct 2016 08:38:21 +0000 (10:38 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 21 Oct 2016 11:55:51 +0000 (11:55 +0000)
StatementContexts are typically leaves or have a low number
of children, leading to wasted space during parsing.

Force collections to be allocated with minimum size and have
them grow as needed, so we limit the run-time overhead.

Change-Id: I30b87e7164f081251bdd44092858be1254d42cba
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 0715b98433da93758203f9cda9ef36d13832fc89)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StatementSupportBundle.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceStorageSupport.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java

index 1f008e723d6cadc7cb060ec00079fde32d36b8dc..badd2b6bccd7b1f728748a2f236df79003d796cf 100644 (file)
@@ -88,11 +88,11 @@ public final class StatementSupportBundle implements Immutable,NamespaceBehaviou
     }
 
     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
-
-        private StatementSupportBundle parent;
         private final Map<QName, StatementSupport<?, ?, ?>> statements = new HashMap<>();
         private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
 
+        private StatementSupportBundle parent;
+
         Builder(final StatementSupportBundle parent) {
             this.parent = parent;
         }
@@ -124,7 +124,6 @@ public final class StatementSupportBundle implements Immutable,NamespaceBehaviou
         public StatementSupportBundle build() {
             return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));
         }
-
     }
 
 }
index e578e912ab210a982be1c53b3f0bd2849f625b26..c9a9cde76265eb910d9b6cab302a5bc606b59cc9 100644 (file)
@@ -22,8 +22,7 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
 
-    private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
-
+    private final Map<Class<?>, Map<?,?>> namespaces = new HashMap<>();
 
     @Override
     public abstract NamespaceStorageNode getParentNamespaceStorage();
@@ -47,6 +46,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
     }
 
+    @SuppressWarnings("unchecked")
     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(final Class<N> type){
         return (Map<K, V>) namespaces.get(type);
     }
@@ -110,7 +110,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode {
         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
         if (localNamespace == null) {
             checkLocalNamespaceAllowed(type);
-            localNamespace = new HashMap<>();
+            localNamespace = new HashMap<>(1);
             namespaces.put(type, localNamespace);
         }
         localNamespace.put(key,value);
index cf79951a3c2be837c7c3c3b28fb44b90e67289d8..2b13c725cf5a696b164093437ead3487e27f6445 100644 (file)
@@ -73,13 +73,47 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private final StatementDefinitionContext<A, D, E> definition;
     private final StatementIdentifier identifier;
     private final StatementSourceReference statementDeclSource;
+
+    private final Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners =
+            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
+
+    private final Multimap<ModelProcessingPhase, ContextMutation> phaseMutation =
+            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
+
+    private final Map<StatementIdentifier, StatementContextBase<?, ?, ?>> substatements = new LinkedHashMap<>(1);
+
+    private final List<TypeOfCopy> copyHistory = new ArrayList<>(1);
+    private final Collection<StatementContextBase<?, ?, ?>> declared = new ArrayList<>(1);
+    private final Collection<StatementContextBase<?, ?, ?>> effective = new ArrayList<>(1);
+    private final Collection<StatementContextBase<?, ?, ?>> effectOfStatement = new ArrayList<>(1);
+
+    private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
+    private boolean isSupportedToBuildEffective = true;
+    private StatementContextBase<?, ?, ?> originalCtx;
+    private ModelProcessingPhase completedPhase;
+    private D declaredInstance;
+    private E effectiveInstance;
     private int order = 0;
 
-    private final Map<StatementIdentifier, StatementContextBase<?, ?, ?>> substatements = new LinkedHashMap<>();
+    StatementContextBase(@Nonnull final ContextBuilder<A, D, E> builder) {
+        this.definition = builder.getDefinition();
+        this.identifier = builder.createIdentifier();
+        this.statementDeclSource = builder.getStamementSource();
+        this.completedPhase = null;
+        this.copyHistory.add(TypeOfCopy.ORIGINAL);
+    }
 
-    private final Collection<StatementContextBase<?, ?, ?>> declared = new ArrayList<>();
-    private final Collection<StatementContextBase<?, ?, ?>> effective = new ArrayList<>();
-    private final Collection<StatementContextBase<?, ?, ?>> effectOfStatement = new ArrayList<>();
+    StatementContextBase(final StatementContextBase<A, D, E> original) {
+        this.definition = Preconditions.checkNotNull(original.definition,
+                "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
+        this.identifier = Preconditions.checkNotNull(original.identifier,
+                "Statement context identifier cannot be null copying from: %s", original.getStatementSourceReference());
+        this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
+                "Statement context statementDeclSource cannot be null copying from: %s",
+                original.getStatementSourceReference());
+        this.completedPhase = null;
+        this.copyHistory.add(TypeOfCopy.ORIGINAL);
+    }
 
     @Override
     public Collection<StatementContextBase<?, ?, ?>> getEffectOfStatement() {
@@ -90,24 +124,6 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     public void addAsEffectOfStatement(final StatementContextBase<?, ?, ?> ctx) {
         effectOfStatement.add(ctx);
     }
-
-    private ModelProcessingPhase completedPhase;
-
-    private final Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners =
-            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>());
-
-    private final Multimap<ModelProcessingPhase, ContextMutation> phaseMutation =
-            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>());
-
-    private D declaredInstance;
-    private E effectiveInstance;
-
-    private StatementContextBase<?, ?, ?> originalCtx;
-    private final List<TypeOfCopy> copyHistory = new ArrayList<>(1);
-
-    private boolean isSupportedToBuildEffective = true;
-    private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
-
     @Override
     public SupportedByFeatures getSupportedByFeatures() {
         return supportedByFeatures;
@@ -173,26 +189,6 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.completedPhase = completedPhase;
     }
 
-    StatementContextBase(@Nonnull final ContextBuilder<A, D, E> builder) {
-        this.definition = builder.getDefinition();
-        this.identifier = builder.createIdentifier();
-        this.statementDeclSource = builder.getStamementSource();
-        this.completedPhase = null;
-        this.copyHistory.add(TypeOfCopy.ORIGINAL);
-    }
-
-    StatementContextBase(final StatementContextBase<A, D, E> original) {
-        this.definition = Preconditions.checkNotNull(original.definition,
-                "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
-        this.identifier = Preconditions.checkNotNull(original.identifier,
-                "Statement context identifier cannot be null copying from: %s", original.getStatementSourceReference());
-        this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
-                "Statement context statementDeclSource cannot be null copying from: %s",
-                original.getStatementSourceReference());
-        this.completedPhase = null;
-        this.copyHistory.add(TypeOfCopy.ORIGINAL);
-    }
-
     /**
      * @return context of parent of statement
      */