]> source.dussan.org Git - aspectj.git/commitdiff
lessen the cost of hashcode and equals
authoraclement <aclement>
Mon, 13 Feb 2006 10:51:24 +0000 (10:51 +0000)
committeraclement <aclement>
Mon, 13 Feb 2006 10:51:24 +0000 (10:51 +0000)
weaver/src/org/aspectj/weaver/patterns/NamePattern.java
weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java

index 45f4b154ac31032c21697c74b7d872e50e9b9822..abcceb841cae3429b6a198836f6a00e8ab3ee084 100644 (file)
@@ -21,6 +21,7 @@ import org.aspectj.weaver.VersionedDataInputStream;
 public class NamePattern extends PatternNode {
        char[] pattern;
        int starCount = 0;
+       private int hashcode = -1;
 
        public static final NamePattern ELLIPSIS = new NamePattern("");
        public static final NamePattern ANY = new NamePattern("*");
@@ -35,6 +36,7 @@ public class NamePattern extends PatternNode {
                for (int i=0, len=pattern.length; i<len; i++) {
                        if (pattern[i] == '*') starCount++;
                }
+               hashcode = new String(pattern).hashCode();
        }
        
        public boolean matches(char[] a2) {
@@ -122,13 +124,18 @@ public class NamePattern extends PatternNode {
        public boolean equals(Object other) {
                if (other instanceof NamePattern) {
                        NamePattern otherPat = (NamePattern)other;
-                       return otherPat.starCount == this.starCount &&
-                            new String(otherPat.pattern).equals(new String(this.pattern));
+                       if (otherPat.starCount!=this.starCount) return false;
+                       if (otherPat.pattern.length!=this.pattern.length) return false;
+                       for (int i = 0; i < this.pattern.length; i++) {
+                               if (this.pattern[i]!=otherPat.pattern[i]) return false;                 
+                       }
+                       return true;
                }
                return false;
        }
+       
     public int hashCode() {
-        return new String(pattern).hashCode();
+       return hashcode;
     }
     
     
index 1e9369666269b33db279e889224cf6a9ed65d291..94953c29587d8b0a5a6535ff8fbc111a19c9a6ad 100644 (file)
@@ -55,6 +55,7 @@ public class SignaturePattern extends PatternNode {
     private TypePatternList parameterTypes;
     private ThrowsPattern throwsPattern;
     private AnnotationTypePattern annotationPattern;
+    private transient int hashcode = -1;
        
        public SignaturePattern(Member.Kind kind, ModifiersPattern modifiers,
                                 TypePattern returnType, TypePattern declaringType,
@@ -96,7 +97,7 @@ public class SignaturePattern extends PatternNode {
                        annotationPattern = annotationPattern.resolveBindings(scope,bindings,false);
                        checkForIncorrectTargetKind(annotationPattern,scope,true);
                }
-               
+               hashcode =-1;
        return this;
     }
     
@@ -629,16 +630,18 @@ public class SignaturePattern extends PatternNode {
                        && o.annotationPattern.equals(this.annotationPattern);
     }
     public int hashCode() {
-        int result = 17;
-        result = 37*result + kind.hashCode();
-        result = 37*result + modifiers.hashCode();
-        result = 37*result + returnType.hashCode();
-        result = 37*result + declaringType.hashCode();
-        result = 37*result + name.hashCode();
-        result = 37*result + parameterTypes.hashCode();
-        result = 37*result + throwsPattern.hashCode();
-        result = 37*result + annotationPattern.hashCode();
-        return result;
+       if (hashcode==-1) {
+          hashcode = 17;
+          hashcode = 37*hashcode + kind.hashCode();
+          hashcode = 37*hashcode + modifiers.hashCode();
+          hashcode = 37*hashcode + returnType.hashCode();
+          hashcode = 37*hashcode + declaringType.hashCode();
+          hashcode = 37*hashcode + name.hashCode();
+          hashcode = 37*hashcode + parameterTypes.hashCode();
+          hashcode = 37*hashcode + throwsPattern.hashCode();
+          hashcode = 37*hashcode + annotationPattern.hashCode();
+       }
+        return hashcode;
     }
     
        public void write(DataOutputStream s) throws IOException {