Make asserts buildDeclared() more lenient 89/87289/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Jan 2020 15:54:24 +0000 (16:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 30 Jan 2020 10:41:57 +0000 (11:41 +0100)
Declared instance is guarded by completed phase, hence if it is
filled with a value we have completed specified phase (at some point).

This results in fewer memory accesses in case the declared statement
is accessed again.

JIRA: YANGTOOLS-652
Change-Id: Iac21fb6e74268770d91e4b9eefc120b892fd0753
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit e32df17e048c955ef24ef36871a078fec43a3574)
(cherry picked from commit 5d1a99d3b37029df33992a36bba41984393529e4)

yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java

index 4d1158bf489723b86866df3e57a80eb39633fa94..0efd0a6b230ee01a63b1d15137ba0cae715e0487 100644 (file)
@@ -511,20 +511,19 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     @Override
     public D buildDeclared() {
+        final D existing = declaredInstance;
+        if (existing != null) {
+            return existing;
+        }
         checkArgument(completedPhase == ModelProcessingPhase.FULL_DECLARATION
                 || completedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
-        if (declaredInstance == null) {
-            declaredInstance = definition.getFactory().createDeclared(this);
-        }
-        return declaredInstance;
+        return declaredInstance = definition.getFactory().createDeclared(this);
     }
 
     @Override
     public E buildEffective() {
-        if (effectiveInstance == null) {
-            effectiveInstance = definition.getFactory().createEffective(this);
-        }
-        return effectiveInstance;
+        final E existing = effectiveInstance;
+        return existing != null ? existing : (effectiveInstance = definition.getFactory().createEffective(this));
     }
 
     /**