Skip to content

Commit 407d164

Browse files
committed
Inline hash check in class path node
String instances keep their own hashes cached, so our field is redundant
1 parent 70cccf1 commit 407d164

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

recaf-core/src/main/java/software/coley/recaf/path/ClassPathNode.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public class ClassPathNode extends AbstractPathNode<String, ClassInfo> {
2222
* Type identifier for class nodes.
2323
*/
2424
public static final String TYPE_ID = "class";
25-
/**
26-
* Name hash for faster {@link #equals(Object)} comparison
27-
*/
28-
private int nameHash = Integer.MIN_VALUE;
2925

3026
/**
3127
* Node without parent.
@@ -161,17 +157,13 @@ public int localCompare(PathNode<?> o) {
161157
@Override
162158
public boolean equals(Object o) {
163159
// If the class names are the same and the parent paths are also equal, then this path points to the same location.
164-
if (o instanceof ClassPathNode otherPath)
165-
return nameHash() == otherPath.nameHash() && Objects.equals(getParent(), otherPath.getParent());
160+
if (o instanceof ClassPathNode otherPath) {
161+
String name = getValue().getName();
162+
String otherName = otherPath.getValue().getName();
163+
return name.hashCode() == otherName.hashCode() // Hash check first which is very fast, and the result is cached.
164+
&& name.equals(otherName) // Sanity check for matching items to prevent hash collisions.
165+
&& Objects.equals(getParent(), otherPath.getParent()); // Parents must also match.
166+
}
166167
return false;
167168
}
168-
169-
private int nameHash() {
170-
// Having many 'equals' calls with string comparisons is slow.
171-
// Instead, we compute the hash of the name and do much faster int comparisons.
172-
// In practice when using the search UI this moves the bottleneck to the 'instanceof' check.
173-
if (nameHash == Integer.MIN_VALUE)
174-
nameHash = getValue().getName().hashCode();
175-
return nameHash;
176-
}
177169
}

0 commit comments

Comments
 (0)