BUG-3641: cache hashCode value 41/22241/4
authorRobert Varga <rovarga@cisco.com>
Tue, 9 Jun 2015 23:09:51 +0000 (01:09 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 10 Jun 2015 18:15:00 +0000 (20:15 +0200)
Builder-derived classes can be used as HashMap keys, which means they
can see their hashcode computed multiple times. Since this is a
recursive operation, the cost of it can be quite high, so dedicate two
fields for caching the hashcode result.

Change-Id: I0d95fd94187a421e4018b2f6992dfeaf2e126732
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 46a6ee011c86b56e44c924342dc922cfbe51d1c8..2585526e0c7e6d40144a84cbcdde33f775ea1887 100644 (file)
@@ -687,8 +687,15 @@ class BuilderTemplate extends BaseTemplate {
      */
     def protected generateHashCode() '''
         «IF !properties.empty || augmentField != null»
+            private int hash = 0;
+            private volatile boolean hashValid = false;
+
             @Override
             public int hashCode() {
+                if (hashValid) {
+                    return hash;
+                }
+
                 final int prime = 31;
                 int result = 1;
                 «FOR property : properties»
@@ -701,6 +708,9 @@ class BuilderTemplate extends BaseTemplate {
                 «IF augmentField != null»
                     result = prime * result + ((«augmentField.name» == null) ? 0 : «augmentField.name».hashCode());
                 «ENDIF»
+
+                hash = result;
+                hashValid = true;
                 return result;
             }
         «ENDIF»