Eliminate ReactorStmtCtx.boolFlag 45/100945/3
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 23 Apr 2022 11:44:46 +0000 (13:44 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 28 Oct 2022 14:06:10 +0000 (14:06 +0000)
The existence of this field is a pure memory layout optimization, taking
advantage of alignment shadow provided by isSupportedToBuildEffective.
JDK15+ memory layout logic can take advatage of this hole, rendering
this optimization unnecessary.

Separate the field into its users, naming it appropriatelly.

Change-Id: I807f31268bd384517e1c0db79f24d5fc92f9492c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/AbstractResumedStatement.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReplicaStatementContext.java

index d2efc1be634fb95f351b653adcb4aef1b80531e3..e47e3153979a467d6a273988df496008910c1224 100644 (file)
@@ -40,6 +40,7 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
     private StatementMap substatements = StatementMap.empty();
     private @Nullable D declaredInstance;
     private boolean implicitDeclared;
+    private boolean fullyDefined;
 
     // Copy constructor
     AbstractResumedStatement(final AbstractResumedStatement<A, D, E> original) {
@@ -47,6 +48,7 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
         this.rawArgument = original.rawArgument;
         this.substatements = original.substatements;
         this.declaredInstance = original.declaredInstance;
+        this.fullyDefined = original.fullyDefined;
     }
 
     AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
@@ -102,7 +104,11 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
 
     @Override
     public final boolean isFullyDefined() {
-        return fullyDefined();
+        return fullyDefined;
+    }
+
+    final void setFullyDefined() {
+        fullyDefined = true;
     }
 
     @Override
index 57d36b59f9bf442ec90186ba8f363b046701c15f..b33191aefb601234e66265a7929b1b18d2f04c52 100644 (file)
@@ -92,6 +92,10 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     private final QNameModule targetModule;
     private final A argument;
 
+    // Indicates whether or not this statement's substatement file was modified, i.e. it is not quite the same as the
+    // prototype's file.
+    private boolean modified;
+
     /**
      * Effective substatements, lazily materialized. This field can have four states:
      * <ul>
@@ -111,6 +115,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         this.prototype = original.prototype;
         this.originalCtx = original.originalCtx;
         this.argument = original.argument;
+        this.modified = original.modified;
         // Substatements are initialized here
         this.substatements = ImmutableList.of();
     }
@@ -295,7 +300,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         // the substatement list, as this operation turned out to not affect them.
         final E effective = createInferredEffective(factory);
         // Since we have forced instantiation to deal with this case, we also need to reset the 'modified' flag
-        setUnmodified();
+        modified = false;
 
         if (sameSubstatements(original.effectiveSubstatements(), effective)) {
             LOG.debug("Reusing unchanged substatements of: {}", prototype);
@@ -305,7 +310,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     }
 
     private @NonNull E internAlongCopyAxis(final StatementFactory<A, D, E> factory, final @NonNull E stmt) {
-        if (!isModified()) {
+        if (!modified) {
             final EffectiveStatementState state = factory.extractEffectiveState(stmt);
             if (state != null) {
                 return prototype.unmodifiedEffectiveSource().attachEffectiveCopy(state, stmt);
@@ -354,7 +359,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     @Override
     ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource() {
-        return isModified() ? this : prototype.unmodifiedEffectiveSource();
+        return modified ? this : prototype.unmodifiedEffectiveSource();
     }
 
     @Override
@@ -545,7 +550,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         final var ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
         ret.addAll(buffer);
         substatements = ret;
-        setModified();
+        modified = true;
 
         prototype.decRef();
         return ret;
@@ -607,7 +612,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
             // resizing operation.
             materializedSchemaTree = new HashMap<>(4);
             substatements = materializedSchemaTree;
-            setModified();
+            modified = true;
         } else {
             verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
             materializedSchemaTree = castMaterialized(substatements);
index a00b5446e56db4d2fbfd566267cc50fec0fd18af..4544771beaeb1ea506a86d934fc5354b8f5c05f1 100644 (file)
@@ -156,25 +156,18 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     // hence improve memory layout.
     private byte flags;
 
-    // Flag for use by AbstractResumedStatement, ReplicaStatementContext and InferredStatementContext. Each of them
-    // uses it to indicated a different condition. This is hiding in the alignment shadow created by
-    // 'isSupportedToBuildEffective'.
-    // FIXME: move this out once we have JDK15+
-    private boolean boolFlag;
-
     ReactorStmtCtx() {
         // Empty on purpose
     }
 
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
         isSupportedToBuildEffective = original.isSupportedToBuildEffective;
-        boolFlag = original.boolFlag;
         flags = original.flags;
     }
 
     // Used by ReplicaStatementContext only
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
-        boolFlag = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
+        isSupportedToBuildEffective = original.isSupportedToBuildEffective;
         flags = original.flags;
     }
 
@@ -587,36 +580,6 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         return false;
     }
 
-    // These two exist only due to memory optimization, should live in AbstractResumedStatement.
-    final boolean fullyDefined() {
-        return boolFlag;
-    }
-
-    final void setFullyDefined() {
-        boolFlag = true;
-    }
-
-    // This exists only due to memory optimization, should live in ReplicaStatementContext. In this context the flag
-    // indicates the need to drop source's reference count when we are being swept.
-    final boolean haveSourceReference() {
-        return boolFlag;
-    }
-
-    // These three exist due to memory optimization, should live in InferredStatementContext. In this context the flag
-    // indicates whether or not this statement's substatement file was modified, i.e. it is not quite the same as the
-    // prototype's file.
-    final boolean isModified() {
-        return boolFlag;
-    }
-
-    final void setModified() {
-        boolFlag = true;
-    }
-
-    final void setUnmodified() {
-        boolFlag = false;
-    }
-
     // These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
     // in flags, we default to 'false' and only set the flag to true when we are absolutely sure -- and all other cases
     // err on the side of caution by taking the time to evaluate each substatement separately.
index c0872c56712a32c44b444c3835b37422dcaac7bc..3e00865a2727fbdc1b7d4593875e5a03ec590aa7 100644 (file)
@@ -34,6 +34,8 @@ final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends
         extends ReactorStmtCtx<A, D, E> {
     private final StatementContextBase<?, ?, ?> parent;
     private final ReactorStmtCtx<A, D, E> source;
+    // We need to drop source's reference count when we are being swept.
+    private final boolean haveSourceRef;
 
     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final ReactorStmtCtx<A, D, E> source) {
         super(source, null);
@@ -41,6 +43,9 @@ final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends
         this.source = requireNonNull(source);
         if (source.isSupportedToBuildEffective()) {
             source.incRef();
+            haveSourceRef = true;
+        } else {
+            haveSourceRef = false;
         }
     }
 
@@ -146,7 +151,7 @@ final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends
 
     @Override
     int sweepSubstatements() {
-        if (haveSourceReference()) {
+        if (haveSourceRef) {
             source.decRef();
         }
         return 0;