Do not instantiate augmentation HashMap in DTO builders 62/21162/2
authorRobert Varga <rovarga@cisco.com>
Tue, 26 May 2015 22:18:57 +0000 (00:18 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 27 May 2015 16:15:55 +0000 (16:15 +0000)
Tracing has revealed that the OpenFlowPlugin creates a lot of builders,
instantiation a million HashMaps, which accounts for 1% of CPU time.
Most of these maps are never touched, so we can easily optimize this
away by lazily instantiating a HashMap when the first augmentation is
added. Also prevents copying of empty augmentation in the copy
constructor.

Change-Id: I592fe6af65c9f3b65038def8bb4069666bfdc375
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend

index 710f1022104491baf8853ad89d4a2806be45a2eb..dc7f9d4390282f0da82e9844a8430f57212806ce 100644 (file)
@@ -429,7 +429,7 @@ class BuilderTemplate extends BaseTemplate {
 
     def private generateAugmentField(boolean isPrivate) '''
         «IF augmentField != null»
-            «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>();
+            «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> «augmentField.name» = «Collections.importedName».emptyMap();
         «ENDIF»
     '''
 
@@ -456,12 +456,19 @@ class BuilderTemplate extends BaseTemplate {
                 if (augmentation == null) {
                     return remove«augmentField.name.toFirstUpper»(augmentationType);
                 }
+
+                if (!(this.«augmentField.name» instanceof «HashMap.importedName»)) {
+                    this.«augmentField.name» = new «HashMap.importedName»<>();
+                }
+
                 this.«augmentField.name».put(augmentationType, augmentation);
                 return this;
             }
 
             public «type.name»«BUILDER» remove«augmentField.name.toFirstUpper»(«Class.importedName»<? extends «augmentField.returnType.importedName»> augmentationType) {
-                this.«augmentField.name».remove(augmentationType);
+                if (this.«augmentField.name» instanceof «HashMap.importedName») {
+                    this.«augmentField.name».remove(augmentationType);
+                }
                 return this;
             }
         «ENDIF»
@@ -562,9 +569,9 @@ class BuilderTemplate extends BaseTemplate {
                     case 0:
                         this.«augmentField.name» = «Collections.importedName».emptyMap();
                         break;
-                        case 1:
-                            final «Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = base.«augmentField.name».entrySet().iterator().next();
-                            this.«augmentField.name» = «Collections.importedName».<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
+                    case 1:
+                        final «Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = base.«augmentField.name».entrySet().iterator().next();
+                        this.«augmentField.name» = «Collections.importedName».<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
                         break;
                     default :
                         this.«augmentField.name» = new «HashMap.importedName»<>(base.«augmentField.name»);
@@ -572,11 +579,15 @@ class BuilderTemplate extends BaseTemplate {
                 «ELSE»
                     if (base instanceof «type.name»«IMPL») {
                         «type.name»«IMPL» impl = («type.name»«IMPL») base;
-                        this.«augmentField.name» = new «HashMap.importedName»<>(impl.«augmentField.name»);
+                        if (!impl.«augmentField.name».isEmpty()) {
+                            this.«augmentField.name» = new «HashMap.importedName»<>(impl.«augmentField.name»);
+                        }
                     } else if (base instanceof «AugmentationHolder.importedName») {
                         @SuppressWarnings("unchecked")
                         «AugmentationHolder.importedName»<«type.importedName»> casted =(«AugmentationHolder.importedName»<«type.importedName»>) base;
-                        this.«augmentField.name» = new «HashMap.importedName»<>(casted.augmentations());
+                        if (!casted.augmentations().isEmpty()) {
+                            this.«augmentField.name» = new «HashMap.importedName»<>(casted.augmentations());
+                        }
                     }
                 «ENDIF»
             «ENDIF»