From fd71d4b0c492505e60c9f4d7161ab81a4f6de743 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 14 Feb 2021 13:59:10 +0100 Subject: [PATCH] Refactor AbstractEffectiveStatementInference SchemaInferenceStack.Inference is incuring reordered copying just because it is forced by AbstractEffectiveStatementInference. Add a level of indirection so that Inference can do its own thing for substatement list. JIRA: YANGTOOLS-1240 Change-Id: I1f1b9f81f1cf32240c0eb26d5338e6f2f11fc74a Signed-off-by: Robert Varga --- .../AbstractEffectiveStatementInference.java | 52 ++++++++++++------- .../model/spi/DefaultSchemaTreeInference.java | 5 +- .../yang/model/util/SchemaInferenceStack.java | 20 +++---- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/AbstractEffectiveStatementInference.java b/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/AbstractEffectiveStatementInference.java index 8dd25e29e0..53be93a8a0 100644 --- a/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/AbstractEffectiveStatementInference.java +++ b/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/AbstractEffectiveStatementInference.java @@ -19,35 +19,49 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; /** - * A simple capture of an {@link EffectiveModelContext} and a list of {@link EffectiveStatement}s. No further guarantees - * are made. + * A simple capture of an {@link AbstractEffectiveModelContextProvider} and {@link EffectiveStatementInference}s. * * @param T constituent {@link EffectiveStatement} type */ @Beta public abstract class AbstractEffectiveStatementInference> extends AbstractEffectiveModelContextProvider implements EffectiveStatementInference { - private final @NonNull List path; - - protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext, - final @NonNull ImmutableList path) { + protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext) { super(modelContext); - this.path = requireNonNull(path); - } - - protected AbstractEffectiveStatementInference(final @NonNull EffectiveModelContext modelContext, - final @NonNull List path) { - super(modelContext); - this.path = ImmutableList.copyOf(path); } @Override - public final List statementPath() { - return path; - } + public abstract List statementPath(); - @Override - protected ToStringHelper addToStringAttributes(final ToStringHelper helper) { - return super.addToStringAttributes(helper).add("statements", path); + /** + * A simple capture of an {@link AbstractEffectiveStatementInference} and a list of {@link EffectiveStatement}s. No + * further guarantees are made. + * + * @param T constituent {@link EffectiveStatement} type + */ + @Beta + public abstract static class WithPath> + extends AbstractEffectiveStatementInference { + private final @NonNull List path; + + protected WithPath(final @NonNull EffectiveModelContext modelContext, final @NonNull ImmutableList path) { + super(modelContext); + this.path = requireNonNull(path); + } + + protected WithPath(final @NonNull EffectiveModelContext modelContext, final @NonNull List path) { + super(modelContext); + this.path = ImmutableList.copyOf(path); + } + + @Override + public final List statementPath() { + return path; + } + + @Override + protected ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper).add("path", path); + } } } diff --git a/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java b/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java index 9917c3f0d1..94003e1503 100644 --- a/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java +++ b/yang/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java @@ -21,6 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveStatementInference.WithPath; /** * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its @@ -28,8 +29,8 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStateme */ @Beta @NonNullByDefault -public final class DefaultSchemaTreeInference - extends AbstractEffectiveStatementInference> implements SchemaTreeInference { +public final class DefaultSchemaTreeInference extends WithPath> + implements SchemaTreeInference { private DefaultSchemaTreeInference(final EffectiveModelContext modelContext, final ImmutableList> path) { super(modelContext, path); diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java index 708efcc914..7bae59a880 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java @@ -64,29 +64,31 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex */ @Beta public static final class Inference extends AbstractEffectiveStatementInference> { + private final ArrayDeque> deque; private final ModuleEffectiveStatement currentModule; private final int groupingDepth; private final boolean clean; - Inference(final @NonNull EffectiveModelContext modelContext, - final Iterator> path, + Inference(final @NonNull EffectiveModelContext modelContext, final ArrayDeque> deque, final ModuleEffectiveStatement currentModule, final int groupingDepth, final boolean clean) { - super(modelContext, ImmutableList.copyOf(path)); + super(modelContext); + this.deque = requireNonNull(deque); this.currentModule = currentModule; this.groupingDepth = groupingDepth; this.clean = clean; } + @Override + public List> statementPath() { + return ImmutableList.copyOf(deque.descendingIterator()); + } + /** * Convert this inference into a {@link SchemaInferenceStack}. * * @return A new stack */ public @NonNull SchemaInferenceStack toSchemaInferenceStack() { - final List> path = statementPath(); - final ArrayDeque> deque = new ArrayDeque<>(path.size()); - path.forEach(deque::push); - return new SchemaInferenceStack(getEffectiveModelContext(), deque, currentModule, groupingDepth, clean); } } @@ -119,7 +121,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex final ArrayDeque> deque, final ModuleEffectiveStatement currentModule, final int groupingDepth, final boolean clean) { this.effectiveModel = requireNonNull(effectiveModel); - this.deque = requireNonNull(deque); + this.deque = deque.clone(); this.currentModule = currentModule; this.groupingDepth = groupingDepth; this.clean = clean; @@ -393,7 +395,7 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex * @return An {@link Inference} */ public @NonNull Inference toInference() { - return new Inference(effectiveModel, deque.descendingIterator(), currentModule, groupingDepth, clean); + return new Inference(effectiveModel, deque.clone(), currentModule, groupingDepth, clean); } /** -- 2.36.6