Make asserts buildDeclared() more lenient 52/87252/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Jan 2020 15:54:24 +0000 (16:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 29 Jan 2020 11:03:23 +0000 (12:03 +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)

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

index 57b2bb11c6d6fbd9522568ed90fe8b2b6406d24d..50ce3ca06157e1f017dc331c9b39281799dffca6 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));
     }
 
     /**