From 7a3f09aa5b0c1cac37f257a7aceeb02c6d1d0e1a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 7 Feb 2021 13:23:26 +0100 Subject: [PATCH] Do not instantiate transient replicas Refactor ReactorStmtCtx.haveRef() into an accurate noRefs(), taking parents into account. If we do not have such a reference, use a new substatement sentinel instead of a full list. JIRA: YANGTOOLS-1223 Change-Id: I4ed132d36b1006bfe3ee650cbef6efb4c88cb8d2 Signed-off-by: Robert Varga --- .../reactor/InferredStatementContext.java | 21 +++++++++++-------- .../parser/stmt/reactor/ReactorStmtCtx.java | 9 ++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java index 7cc1efd10b..a3d5e9d459 100644 --- a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java +++ b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java @@ -81,8 +81,9 @@ final class InferredStatementContext, E extend private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class); - // Sentinel object for 'substatements' - private static final Object SWEPT_SUBSTATEMENTS = new Object(); + // Sentinel objects for 'substatements', String is a good enough type + private static final @NonNull String REUSED_SUBSTATEMENTS = "reused"; + private static final @NonNull String SWEPT_SUBSTATEMENTS = "swept"; private final @NonNull StatementContextBase prototype; private final @NonNull StatementContextBase parent; @@ -212,6 +213,7 @@ final class InferredStatementContext, E extend E createEffective(final StatementFactory factory) { // If we have not materialized we do not have a difference in effective substatements, hence we can forward // towards the source of the statement. + accessSubstatements(); return substatements == null ? tryToReusePrototype(factory) : super.createEffective(factory); } @@ -245,9 +247,9 @@ final class InferredStatementContext, E extend if (allReused(declCopy) && allReused(effCopy)) { LOG.debug("Reusing after substatement check: {}", origEffective); - // FIXME: can we skip this if !haveRef()? - substatements = reusePrototypeReplicas(Streams.concat(declCopy.stream(), effCopy.stream()) - .map(copy -> copy.toReusedChild(this))); + substatements = noRefs() ? REUSED_SUBSTATEMENTS + : reusePrototypeReplicas(Streams.concat(declCopy.stream(), effCopy.stream()) + .map(copy -> copy.toReusedChild(this))); prototype.decRef(); return origEffective; } @@ -269,8 +271,7 @@ final class InferredStatementContext, E extend private @NonNull E tryToReuseSubstatements(final StatementFactory factory, final @NonNull E original) { if (allSubstatementsContextIndependent()) { LOG.debug("Reusing substatements of: {}", prototype); - // FIXME: can we skip this if !haveRef()? - substatements = reusePrototypeReplicas(); + substatements = noRefs() ? REUSED_SUBSTATEMENTS : reusePrototypeReplicas(); prototype.decRef(); return factory.copyEffective(this, original); } @@ -461,7 +462,9 @@ final class InferredStatementContext, E extend } private void accessSubstatements() { - verify(substatements != SWEPT_SUBSTATEMENTS, "Attempted to access substatements of %s", this); + if (substatements instanceof String) { + throw new VerifyException("Access to " + substatements + " substatements of " + this); + } } @Override @@ -477,7 +480,7 @@ final class InferredStatementContext, E extend final Object local = substatements; substatements = SWEPT_SUBSTATEMENTS; int count = 0; - if (local != null) { + if (local instanceof List) { final List> list = castEffective(local); sweep(list); count = countUnswept(list); diff --git a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java index 2237fa03ef..7b9afcb96f 100644 --- a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java +++ b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java @@ -693,12 +693,13 @@ abstract class ReactorStmtCtx, E extends Effec } /** - * Return {@code true} if this context has an outstanding reference. + * Return {@code true} if this context has no outstanding references. * - * @return True if this context has an outstanding reference. + * @return True if this context has no outstanding references. */ - final boolean haveRef() { - return refcount > REFCOUNT_NONE; + final boolean noRefs() { + final int local = refcount; + return local < REFCOUNT_NONE || local == REFCOUNT_NONE && noParentRef(); } private void lastDecRef() { -- 2.36.6