groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [6/7] groovy git commit: Improve performance of generated hashCode() methods for instances with null fields and properties by avoiding calling NullObject.is() method for which no call site caching occurs.
Date Fri, 17 Nov 2017 11:56:05 GMT
Improve performance of generated hashCode() methods for instances with null fields and properties
by avoiding calling NullObject.is() method for which no call site caching occurs.


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0cd1bcca
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0cd1bcca
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0cd1bcca

Branch: refs/heads/GROOVY_2_6_X
Commit: 0cd1bcca86afea48dc9d75c04508e0b020bb4141
Parents: 1513fa8
Author: Marcin Erdmann <erdi84@gmail.com>
Authored: Thu Nov 16 22:47:23 2017 +0000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Nov 17 21:55:37 2017 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/ast/tools/GeneralUtils.java |  7 ++++
 .../EqualsAndHashCodeASTTransformation.java     | 40 ++++++++++----------
 2 files changed, 27 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0cd1bcca/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
index 252d6e3..b2beb96 100644
--- a/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -72,6 +72,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import static org.codehaus.groovy.syntax.Types.COMPARE_NOT_IDENTICAL;
+
 /**
  * Handy methods when working with the Groovy AST
  */
@@ -79,6 +81,7 @@ public class GeneralUtils {
     public static final Token ASSIGN = Token.newSymbol(Types.ASSIGN, -1, -1);
     public static final Token EQ = Token.newSymbol(Types.COMPARE_EQUAL, -1, -1);
     public static final Token NE = Token.newSymbol(Types.COMPARE_NOT_EQUAL, -1, -1);
+    public static final Token NOT_IDENTICAL = Token.newSymbol(COMPARE_NOT_IDENTICAL, -1,
-1);
     public static final Token LT = Token.newSymbol(Types.COMPARE_LESS_THAN, -1, -1);
     public static final Token AND = Token.newSymbol(Types.LOGICAL_AND, -1, -1);
     public static final Token OR = Token.newSymbol(Types.LOGICAL_OR, -1, -1);
@@ -565,6 +568,10 @@ public class GeneralUtils {
         return new BinaryExpression(lhv, LT, rhv);
     }
 
+    public static BinaryExpression notIdenticalX(Expression lhv, Expression rhv) {
+        return new BinaryExpression(lhv, NOT_IDENTICAL, rhv);
+    }
+
     /**
      * @deprecated use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
      */

http://git-wip-us.apache.org/repos/asf/groovy/blob/0cd1bcca/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
b/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
index 1c95a56..e8dbd53 100644
--- a/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
@@ -73,20 +73,20 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
             boolean includeFields = memberHasValue(anno, "includeFields", true);
             List<String> excludes = getMemberStringList(anno, "excludes");
             List<String> includes = getMemberStringList(anno, "includes");
-            final boolean allNames = memberHasValue(anno, "allNames", true);
+            final boolean allNames = memberHasValue(anno, "allNames", true);
             if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
return;
             if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields))
return;
             if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
return;
-            createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes,
allNames);
-            createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes,
allNames);
+            createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes,
allNames);
+            createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes,
allNames);
         }
     }
 
     public static void createHashCode(ClassNode cNode, boolean cacheResult, boolean includeFields,
boolean callSuper, List<String> excludes, List<String> includes) {
-        createHashCode(cNode, cacheResult, includeFields, callSuper, excludes, includes,
false);
-    }
-
-    public static void createHashCode(ClassNode cNode, boolean cacheResult, boolean includeFields,
boolean callSuper, List<String> excludes, List<String> includes, boolean allNames)
{
+        createHashCode(cNode, cacheResult, includeFields, callSuper, excludes, includes,
false);
+    }
+
+    public static void createHashCode(ClassNode cNode, boolean cacheResult, boolean includeFields,
boolean callSuper, List<String> excludes, List<String> includes, boolean allNames)
{
         // make a public method if none exists otherwise try a private method with leading
underscore
         boolean hasExistingHashCode = hasDeclaredMethod(cNode, "hashCode", 0);
         if (hasExistingHashCode && hasDeclaredMethod(cNode, "_hashCode", 0)) return;
@@ -98,11 +98,11 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
             final Expression hash = varX(hashField);
             body.addStatement(ifS(
                     isZeroX(hash),
-                    calculateHashStatements(cNode, hash, includeFields, callSuper, excludes,
includes, allNames)
+                    calculateHashStatements(cNode, hash, includeFields, callSuper, excludes,
includes, allNames)
             ));
             body.addStatement(returnS(hash));
         } else {
-            body.addStatement(calculateHashStatements(cNode, null, includeFields, callSuper,
excludes, includes, allNames));
+            body.addStatement(calculateHashStatements(cNode, null, includeFields, callSuper,
excludes, includes, allNames));
         }
 
         cNode.addMethod(new MethodNode(
@@ -114,7 +114,7 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
                 body));
     }
 
-    private static Statement calculateHashStatements(ClassNode cNode, Expression hash, boolean
includeFields, boolean callSuper, List<String> excludes, List<String> includes,
boolean allNames) {
+    private static Statement calculateHashStatements(ClassNode cNode, Expression hash, boolean
includeFields, boolean callSuper, List<String> excludes, List<String> includes,
boolean allNames) {
         final List<PropertyNode> pList = getInstanceProperties(cNode);
         final List<FieldNode> fList = new ArrayList<FieldNode>();
         if (includeFields) {
@@ -126,22 +126,22 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
         body.addStatement(declS(result, callX(HASHUTIL_TYPE, "initHash")));
 
         for (PropertyNode pNode : pList) {
-            if (shouldSkip(pNode.getName(), excludes, includes, allNames)) continue;
+            if (shouldSkip(pNode.getName(), excludes, includes, allNames)) continue;
             // _result = HashCodeHelper.updateHash(_result, getProperty()) // plus self-reference
checking
             Expression getter = getterThisX(cNode, pNode);
             final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, getter));
             body.addStatement(ifS(
-                    notX(sameX(getter, varX("this"))),
+                    notIdenticalX(getter, varX("this")),
                     assignS(result, current)));
 
         }
         for (FieldNode fNode : fList) {
-            if (shouldSkip(fNode.getName(), excludes, includes, allNames)) continue;
+            if (shouldSkip(fNode.getName(), excludes, includes, allNames)) continue;
             // _result = HashCodeHelper.updateHash(_result, field) // plus self-reference
checking
             final Expression fieldExpr = varX(fNode);
             final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, fieldExpr));
             body.addStatement(ifS(
-                    notX(sameX(fieldExpr, varX("this"))),
+                    notIdenticalX(fieldExpr, varX("this")),
                     assignS(result, current)));
         }
         if (callSuper) {
@@ -176,10 +176,10 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
     }
 
     public static void createEquals(ClassNode cNode, boolean includeFields, boolean callSuper,
boolean useCanEqual, List<String> excludes, List<String> includes) {
-        createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes, false);
-    }
-
-    public static void createEquals(ClassNode cNode, boolean includeFields, boolean callSuper,
boolean useCanEqual, List<String> excludes, List<String> includes, boolean allNames)
{
+        createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes, false);
+    }
+
+    public static void createEquals(ClassNode cNode, boolean includeFields, boolean callSuper,
boolean useCanEqual, List<String> excludes, List<String> includes, boolean allNames)
{
         if (useCanEqual) createCanEqual(cNode);
         // make a public method if none exists otherwise try a private method with leading
underscore
         boolean hasExistingEquals = hasDeclaredMethod(cNode, "equals", 1);
@@ -209,7 +209,7 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
 
         List<PropertyNode> pList = getInstanceProperties(cNode);
         for (PropertyNode pNode : pList) {
-            if (shouldSkip(pNode.getName(), excludes, includes, allNames)) continue;
+            if (shouldSkip(pNode.getName(), excludes, includes, allNames)) continue;
             boolean canBeSelf = StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(
                     pNode.getOriginType(), cNode
             );
@@ -232,7 +232,7 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
             fList.addAll(getInstanceNonPropertyFields(cNode));
         }
         for (FieldNode fNode : fList) {
-            if (shouldSkip(fNode.getName(), excludes, includes, allNames)) continue;
+            if (shouldSkip(fNode.getName(), excludes, includes, allNames)) continue;
             body.addStatement(
                     ifS(notX(hasSameFieldX(fNode, otherTyped)),
                             ifElseS(differentSelfRecursiveFieldX(fNode, otherTyped),


Mime
View raw message