From: Robert Varga Date: Tue, 26 May 2015 22:18:57 +0000 (+0200) Subject: Do not instantiate augmentation HashMap in DTO builders X-Git-Tag: release/lithium~67 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=2cb012f4e814859e10726d02cb1c073321316ab3;p=yangtools.git Do not instantiate augmentation HashMap in DTO builders 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 --- diff --git a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend index 710f102210..dc7f9d4390 100644 --- a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend +++ b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend @@ -429,7 +429,7 @@ class BuilderTemplate extends BaseTemplate { def private generateAugmentField(boolean isPrivate) ''' «IF augmentField != null» - «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.importedName», «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>(); + «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.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» 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», «augmentField.returnType.importedName»> e = base.«augmentField.name».entrySet().iterator().next(); - this.«augmentField.name» = «Collections.importedName».<«Class.importedName», «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue()); + case 1: + final «Map.importedName».Entry<«Class.importedName», «augmentField.returnType.importedName»> e = base.«augmentField.name».entrySet().iterator().next(); + this.«augmentField.name» = «Collections.importedName».<«Class.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»