From 0715b98433da93758203f9cda9ef36d13832fc89 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 21 Oct 2016 10:38:21 +0200 Subject: [PATCH] BUG-6522: Adjust collection sizes 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 --- .../spi/meta/StatementSupportBundle.java | 5 +- .../stmt/reactor/NamespaceStorageSupport.java | 6 +- .../stmt/reactor/StatementContextBase.java | 80 +++++++++---------- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StatementSupportBundle.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StatementSupportBundle.java index 1f008e723d..badd2b6bcc 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StatementSupportBundle.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StatementSupportBundle.java @@ -88,11 +88,11 @@ public final class StatementSupportBundle implements Immutable,NamespaceBehaviou } public static class Builder implements org.opendaylight.yangtools.concepts.Builder { - - private StatementSupportBundle parent; private final Map> statements = new HashMap<>(); private final Map, 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)); } - } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceStorageSupport.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceStorageSupport.java index e578e912ab..c9a9cde762 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceStorageSupport.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceStorageSupport.java @@ -22,8 +22,7 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; abstract class NamespaceStorageSupport implements NamespaceStorageNode { - private final Map,Map> namespaces = new HashMap<>(); - + private final Map, 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 > Map getAllFromCurrentStmtCtxNamespace(final Class type){ return (Map) namespaces.get(type); } @@ -110,7 +110,7 @@ abstract class NamespaceStorageSupport implements NamespaceStorageNode { Map localNamespace = (Map) namespaces.get(type); if (localNamespace == null) { checkLocalNamespaceAllowed(type); - localNamespace = new HashMap<>(); + localNamespace = new HashMap<>(1); namespaces.put(type, localNamespace); } localNamespace.put(key,value); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index cf79951a3c..2b13c725cf 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -73,13 +73,47 @@ public abstract class StatementContextBase, E private final StatementDefinitionContext definition; private final StatementIdentifier identifier; private final StatementSourceReference statementDeclSource; + + private final Multimap phaseListeners = + Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1)); + + private final Multimap phaseMutation = + Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1)); + + private final Map> substatements = new LinkedHashMap<>(1); + + private final List copyHistory = new ArrayList<>(1); + private final Collection> declared = new ArrayList<>(1); + private final Collection> effective = new ArrayList<>(1); + private final Collection> 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> substatements = new LinkedHashMap<>(); + StatementContextBase(@Nonnull final ContextBuilder builder) { + this.definition = builder.getDefinition(); + this.identifier = builder.createIdentifier(); + this.statementDeclSource = builder.getStamementSource(); + this.completedPhase = null; + this.copyHistory.add(TypeOfCopy.ORIGINAL); + } - private final Collection> declared = new ArrayList<>(); - private final Collection> effective = new ArrayList<>(); - private final Collection> effectOfStatement = new ArrayList<>(); + StatementContextBase(final StatementContextBase 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> getEffectOfStatement() { @@ -90,24 +124,6 @@ public abstract class StatementContextBase, E public void addAsEffectOfStatement(final StatementContextBase ctx) { effectOfStatement.add(ctx); } - - private ModelProcessingPhase completedPhase; - - private final Multimap phaseListeners = - Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>()); - - private final Multimap phaseMutation = - Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>()); - - private D declaredInstance; - private E effectiveInstance; - - private StatementContextBase originalCtx; - private final List 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, E this.completedPhase = completedPhase; } - StatementContextBase(@Nonnull final ContextBuilder builder) { - this.definition = builder.getDefinition(); - this.identifier = builder.createIdentifier(); - this.statementDeclSource = builder.getStamementSource(); - this.completedPhase = null; - this.copyHistory.add(TypeOfCopy.ORIGINAL); - } - - StatementContextBase(final StatementContextBase 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 */ -- 2.36.6