import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
private boolean implicitDeclaredFlag;
// TODO: we a single byte of alignment shadow left, we should think how we can use it to cache information we build
- // during buildEffective()
+ // during InferredStatementContext.tryToReusePrototype(). We usually end up being routed to
+ // copyAsChildOfImpl() -- which performs an eager instantiation and checks for changes afterwards. We should
+ // be able to capture how parent scope affects the copy in a few bits. If we can do that, than we can reap
+ // the benefits by just examining new parent context and old parent context contribution to the state. If
+ // their impact is the same, we can skip instantiation of statements and directly reuse them (individually,
+ // or as a complete file).
+ //
+ // Whatever we end up tracking, we need to track two views of that -- for the statement itself
+ // (sans substatements) and a summary of substatements. I think it should be possible to get this working
+ // with 2x5bits -- we have up to 15 mutable bits available if we share the field with implicitDeclaredFlag.
// Copy constructor used by subclasses to implement reparent()
StatementContextBase(final StatementContextBase<A, D, E> original) {
@Override
public final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
- Mutable<X, Y, Z> addUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
+ Mutable<X, Y, Z> createUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
requireNonNull(support);
checkArgument(support instanceof UndeclaredStatementFactory, "Unsupported statement support %s", support);
final var ret = new UndeclaredStmtCtx<>(this, support, arg);
support.onStatementAdded(ret);
- addEffectiveSubstatement(ret);
return ret;
}
final List<ReactorStmtCtx<?, ?, ?>> addEffectiveSubstatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
final Mutable<?, ?, ?> substatement) {
- verifyStatement(substatement);
-
+ final ReactorStmtCtx<?, ?, ?> stmt = verifyStatement(substatement);
final List<ReactorStmtCtx<?, ?, ?>> resized = beforeAddEffectiveStatement(effective, 1);
- final ReactorStmtCtx<?, ?, ?> stmt = (ReactorStmtCtx<?, ?, ?>) substatement;
ensureCompletedExecution(stmt);
resized.add(stmt);
return resized;
}
+ static final void afterAddEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
+ // Undeclared statements still need to have 'onDeclarationFinished()' triggered
+ if (substatement instanceof UndeclaredStmtCtx) {
+ finishDeclaration((UndeclaredStmtCtx<?, ?, ?>) substatement);
+ }
+ }
+
+ // Split out to keep generics working without a warning
+ private static <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> void finishDeclaration(
+ final UndeclaredStmtCtx<X, Y, Z> substatement) {
+ substatement.definition().onDeclarationFinished(substatement, ModelProcessingPhase.FULL_DECLARATION);
+ }
+
@Override
public final void addEffectiveSubstatements(final Collection<? extends Mutable<?, ?, ?>> statements) {
if (!statements.isEmpty()) {
abstract Iterator<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete();
// exposed for InferredStatementContext only
- final void ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
- verifyStatement(stmt);
- ensureCompletedExecution((ReactorStmtCtx<?, ?, ?>) stmt);
+ final ReactorStmtCtx<?, ?, ?> ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
+ final var ret = verifyStatement(stmt);
+ ensureCompletedExecution(ret);
+ return ret;
}
// Make sure target statement has transitioned at least to our phase (if we have one). This method is just before we
verify(stmt.tryToCompletePhase(executionOrder), "Statement %s cannot complete phase %s", stmt, executionOrder);
}
- private static void verifyStatement(final Mutable<?, ?, ?> stmt) {
+ private static ReactorStmtCtx<?, ?, ?> verifyStatement(final Mutable<?, ?, ?> stmt) {
verify(stmt instanceof ReactorStmtCtx, "Unexpected statement %s", stmt);
+ return (ReactorStmtCtx<?, ?, ?>) stmt;
}
private List<ReactorStmtCtx<?, ?, ?>> beforeAddEffectiveStatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
abstract @NonNull E createEffective(@NonNull StatementFactory<A, D, E> factory);
- /**
- * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
- * definition site. This is needed to pick the correct instantiation method: for declared statements we will
- * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
- * {@link UndeclaredStmtCtx}.
- *
- * @param factory Statement factory
- * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
- * @return Built effective stateue
- */
- abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
- @NonNull InferredStatementContext<A, D, E> ctx);
-
/**
* Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per
* {@link StmtContext#buildEffective()} contract.
* @return Stream of supported declared statements.
*/
// FIXME: we really want to unify this with streamEffective(), under its name
- abstract Stream<? extends @NonNull StmtContext<?, ?, ?>> streamDeclared();
+ abstract Stream<? extends @NonNull ReactorStmtCtx<?, ?, ?>> streamDeclared();
/**
* Return a stream of inferred statements which can be built into an {@link EffectiveStatement}, as per
* @return Stream of supported effective statements.
*/
// FIXME: this method is currently a misnomer, but unifying with streamDeclared() would make this accurate again
- abstract Stream<? extends @NonNull StmtContext<?, ?, ?>> streamEffective();
+ abstract Stream<? extends @NonNull ReactorStmtCtx<?, ?, ?>> streamEffective();
@Override
final boolean doTryToCompletePhase(final byte targetOrder) {
return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule));
}
- private ReactorStmtCtx<A, D, E> copyAsChildOfImpl(final Mutable<?, ?, ?> parent, final CopyType type,
+ private @Nullable ReactorStmtCtx<A, D, E> copyAsChildOfImpl(final Mutable<?, ?, ?> parent, final CopyType type,
final QNameModule targetModule) {
final StatementSupport<A, D, E> support = definition.support();
final CopyPolicy policy = support.copyPolicy();
return null;
}
- parent.ensureCompletedPhase(copy);
+ parent.ensureCompletedExecution(copy);
return canReuseCurrent(copy) ? this : copy;
}
- private boolean canReuseCurrent(final ReactorStmtCtx<A, D, E> copy) {
+ private boolean canReuseCurrent(final @NonNull ReactorStmtCtx<A, D, E> copy) {
// Defer to statement factory to see if we can reuse this object. If we can and have only context-independent
// substatements we can reuse the object. More complex cases are handled indirectly via the copy.
return definition.getFactory().canReuseCurrent(copy, this, buildEffective().effectiveSubstatements())
copy = new InferredStatementContext<>(result, original, childCopyType, type, targetModule);
result.addEffectiveSubstatement(copy);
+ result.definition.onStatementAdded(result);
} else {
result = copy = new InferredStatementContext<>(this, original, type, type, targetModule);
}