From 16db760aab4b98e1980877bc6adb971b413af198 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 8 Aug 2017 04:15:43 +0200 Subject: [PATCH] Do not instantiate needless objects in augment Non-mutable versions of accessors are cheaper and should be used wherever we do not modify state. Change-Id: I200d843b830af59d407da346d0b09aa4909a4467 Signed-off-by: Robert Varga --- .../yang/parser/spi/meta/StmtContext.java | 11 +++++++++++ .../yang/parser/spi/meta/StmtContextUtils.java | 7 ++----- .../parser/spi/meta/SubstatementValidator.java | 3 +-- .../parser/stmt/rfc6020/AugmentStatementImpl.java | 11 ++--------- .../parser/stmt/rfc6020/DeviateStatementImpl.java | 6 +----- .../parser/stmt/rfc6020/UsesStatementImpl.java | 15 +++++++-------- .../effective/CaseEffectiveStatementImpl.java | 8 +------- 7 files changed, 25 insertions(+), 36 deletions(-) diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java index a4cb0a6ea4..f6bd7abf68 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java @@ -7,9 +7,12 @@ */ package org.opendaylight.yangtools.yang.parser.spi.meta; +import com.google.common.collect.Iterables; +import com.google.common.collect.Streams; import java.util.Collection; import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QNameModule; @@ -92,6 +95,14 @@ public interface StmtContext, E extends Effect @Nonnull Collection> effectiveSubstatements(); + default Iterable> allSubstatements() { + return Iterables.concat(declaredSubstatements(), effectiveSubstatements()); + } + + default Stream> allSubstatementsStream() { + return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream()); + } + /** * Builds {@link DeclaredStatement} for statement context. */ diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index a009f35d65..984c9e3eab 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -11,7 +11,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; import java.util.Collection; import java.util.Date; import java.util.Optional; @@ -72,8 +71,7 @@ public final class StmtContextUtils { public static > A firstSubstatementAttributeOf( final StmtContext ctx, final Class declaredType) { - final A firstAttribute = firstAttributeOf(ctx.effectiveSubstatements(), declaredType); - return firstAttribute != null ? firstAttribute : firstAttributeOf(ctx.declaredSubstatements(), declaredType); + return firstAttributeOf(ctx.allSubstatements(), declaredType); } @SuppressWarnings("unchecked") @@ -466,8 +464,7 @@ public final class StmtContextUtils { } private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext leafStmtCtx) { - Iterables.concat(leafStmtCtx.declaredSubstatements(), leafStmtCtx.effectiveSubstatements()).forEach( - leafSubstmtCtx -> { + leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> { final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition(); SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef) || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(), diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/SubstatementValidator.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/SubstatementValidator.java index 654d685993..2bacaa40af 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/SubstatementValidator.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/SubstatementValidator.java @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.spi.meta; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import java.util.HashMap; import java.util.Map; @@ -117,7 +116,7 @@ public final class SubstatementValidator { MissingSubstatementException { final Map stmtCounts = new HashMap<>(); - for (StmtContext stmtCtx : Iterables.concat(ctx.declaredSubstatements(), ctx.effectiveSubstatements())) { + for (StmtContext stmtCtx : ctx.allSubstatements()) { stmtCounts.computeIfAbsent(stmtCtx.getPublicDefinition(), key -> new Counter()).increment(); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java index 9a334b2b5a..e85881d286 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java @@ -8,11 +8,9 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import com.google.common.base.Verify; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; @@ -266,11 +264,7 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement> targetSubStatements = ImmutableList.>builder() - .addAll(targetCtx.mutableDeclaredSubstatements()).addAll(targetCtx.mutableEffectiveSubstatements()) - .build(); - - for (final Mutable subStatement : targetSubStatements) { + for (final StmtContext subStatement : targetCtx.allSubstatements()) { final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx .getPublicDefinition().getDeclaredRepresentationClass()); final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement @@ -293,8 +287,7 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition(); if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded) && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) { - final Iterable> targetCtxSubstatements = Iterables.concat( - targetCtx.declaredSubstatements(), targetCtx.effectiveSubstatements()); - - for (final StmtContext targetCtxSubstatement : targetCtxSubstatements) { + for (final StmtContext targetCtxSubstatement : targetCtx.allSubstatements()) { InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()), stmtCtxToBeAdded.getStatementSourceReference(), "Deviation cannot add substatement %s " + "to target node %s because it is already defined in target and can appear only once.", diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/UsesStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/UsesStatementImpl.java index 3db84b8e93..7e39d918d5 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/UsesStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/UsesStatementImpl.java @@ -260,7 +260,7 @@ public class UsesStatementImpl extends AbstractDeclaredStatement implemen public static void resolveUsesNode( final Mutable> usesNode, - final StatementContextBase targetNodeStmtCtx) { + final StmtContext targetNodeStmtCtx) { for (final Mutable subStmtCtx : usesNode.mutableDeclaredSubstatements()) { if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class) && areFeaturesSupported(subStmtCtx)) { @@ -269,15 +269,14 @@ public class UsesStatementImpl extends AbstractDeclaredStatement implemen } } - private static boolean areFeaturesSupported(final Mutable subStmtCtx) { + private static boolean areFeaturesSupported(final StmtContext subStmtCtx) { /* * In case of Yang 1.1, checks whether features are supported. */ return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || subStmtCtx.isSupportedByFeatures(); } - private static void performRefine(final Mutable subStmtCtx, - final StatementContextBase usesParentCtx) { + private static void performRefine(final Mutable subStmtCtx, final StmtContext usesParentCtx) { final Object refineArgument = subStmtCtx.getStatementArgument(); InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier), @@ -292,10 +291,10 @@ public class UsesStatementImpl extends AbstractDeclaredStatement implemen "Refine target node %s not found.", refineTargetNodeIdentifier); if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) { - LOG.debug( - "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}", - subStmtCtx.getStatementArgument(), subStmtCtx.getParentContext().getStatementArgument(), - refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference()); + LOG.debug("Refine node '{}' in uses '{}' has target node unknown statement '{}'. " + + "Refine has been skipped. At line: {}", subStmtCtx.getStatementArgument(), + subStmtCtx.getParentContext().getStatementArgument(), + refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference()); subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx); return; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java index c21e0865ab..7cc26e6a1d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java @@ -8,7 +8,6 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; import com.google.common.base.Optional; -import java.util.Collection; import java.util.Objects; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; @@ -29,17 +28,12 @@ public final class CaseEffectiveStatementImpl extends AbstractEffectiveSimpleDat this.original = (ChoiceCaseNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null); if (ctx.isConfiguration()) { - configuration = isAtLeastOneChildConfiguration(ctx.declaredSubstatements()) - || isAtLeastOneChildConfiguration(ctx.effectiveSubstatements()); + configuration = ctx.allSubstatementsStream().anyMatch(StmtContext::isConfiguration); } else { configuration = false; } } - private static boolean isAtLeastOneChildConfiguration(final Collection> children) { - return children.stream().anyMatch(StmtContext::isConfiguration); - } - @Override public Optional getOriginal() { return Optional.fromNullable(original); -- 2.36.6