Browse Source

lessen the cost of hashcode and equals

tags/POST_MEMORY_CHANGES
aclement 18 years ago
parent
commit
67ef5e2657

+ 10
- 3
weaver/src/org/aspectj/weaver/patterns/NamePattern.java View 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;
}

+ 14
- 11
weaver/src/org/aspectj/weaver/patterns/SignaturePattern.java View 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 {

Loading…
Cancel
Save