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>
def private generateAugmentField(boolean isPrivate) '''
«IF augmentField != null»
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();
if (augmentation == null) {
return remove«augmentField.name.toFirstUpper»(augmentationType);
}
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».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);
+ }
case 0:
this.«augmentField.name» = «Collections.importedName».emptyMap();
break;
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»);
break;
default :
this.«augmentField.name» = new «HashMap.importedName»<>(base.«augmentField.name»);
«ELSE»
if (base instanceof «type.name»«IMPL») {
«type.name»«IMPL» impl = («type.name»«IMPL») base;
«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;
} 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());
+ }