Bug 2287 - TypeProviderImpl retains empty HashMaps 77/13477/7
authorLadislav Borak <lborak@cisco.com>
Tue, 9 Dec 2014 11:32:24 +0000 (12:32 +0100)
committerRobert Varga <rovarga@cisco.com>
Thu, 12 Mar 2015 23:10:42 +0000 (00:10 +0100)
Allocating default HashMaps is wasteful, especially if we end up not
storing anything in them. This fixes that case by making sure we
allocate a map only after we are sure we are going to put something into
it. Also be conservative about the size, so we do not waste too much
space.

Change-Id: I85ad1506e732a5c3728ef3529e7e0b5b430d758a
Signed-off-by: Ladislav Borak <lborak@cisco.com>
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java

index b393c13367806ea9fb7211e698906e569ebe421f..11edd05925046b52f6bd309ca86d1b897c74f035 100644 (file)
@@ -621,7 +621,7 @@ public final class TypeProviderImpl implements TypeProvider {
      */
     private void resolveTypeDefsFromContext() {
         final Set<Module> modules = schemaContext.getModules();
-        Preconditions.checkArgument(modules != null, "Sef of Modules cannot be NULL!");
+        Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL!");
         final Module[] modulesArray = new Module[modules.size()];
         int i = 0;
         for (Module modul : modules) {
@@ -635,8 +635,7 @@ public final class TypeProviderImpl implements TypeProvider {
             if (dateTypeMap == null) {
                 dateTypeMap = new HashMap<>();
             }
-            final Map<String, Type> typeMap = new HashMap<>();
-            dateTypeMap.put(module.getRevision(), typeMap);
+            dateTypeMap.put(module.getRevision(), Collections.<String, Type>emptyMap());
             genTypeDefsContextMap.put(module.getName(), dateTypeMap);
         }
 
@@ -731,8 +730,12 @@ public final class TypeProviderImpl implements TypeProvider {
                 }
                 if (returnType != null) {
                     final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(moduleName);
-                    final Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
+                    Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
                     if (typeMap != null) {
+                        if (typeMap.isEmpty()) {
+                            typeMap = new HashMap<>(4);
+                            modulesByDate.put(moduleRevision, typeMap);
+                        }
                         typeMap.put(typedefName, returnType);
                     }
                     return returnType;