diff options
author | Nathan Harmata <nharmata@google.com> | 2016-05-18 13:54:42 +0000 |
---|---|---|
committer | Kristina Chodorow <kchodorow@google.com> | 2016-05-19 16:26:44 +0000 |
commit | 102a9a101a52f4ca92c9e97387ae159e54e87b05 (patch) | |
tree | 888c92c28059839d90478cf65f522ba515d2a216 /src/main/java/com/google/devtools | |
parent | bfe9deb05cc6fe28995e3f3474dc6508d981585e (diff) |
Precompute Label#hashCode and use it to implement a fast-path for #equals. Label#equals was a cpu hotspot revealed during profiling.
The additional field for the precomputed hash code has no memory cost since each Label instance was already wasting 4 bytes due to object alignment requirements.
--
MOS_MIGRATED_REVID=122626707
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/cmdline/Label.java | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java index ba050c670f..73c7e5ecdb 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.Serializable; +import java.util.Objects; /** * A class to identify a BUILD target. All targets belong to exactly one package. @@ -242,6 +243,9 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin /** The name of the target within the package. Canonical. */ private final String name; + /** Precomputed hash code. */ + private final int hashCode; + /** * Constructor from a package name, target name. Both are checked for validity * and a SyntaxException is thrown if either is invalid. @@ -258,8 +262,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin Preconditions.checkNotNull(packageIdentifier); Preconditions.checkNotNull(name); + this.packageIdentifier = packageIdentifier; try { - this.packageIdentifier = packageIdentifier; this.name = canonicalizeTargetName(name); } catch (LabelSyntaxException e) { // This check is just for a more helpful error message @@ -270,6 +274,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin } throw e; } + this.hashCode = Objects.hash(this.name, this.packageIdentifier); } private Object writeReplace() { @@ -485,7 +490,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin @Override public int hashCode() { - return name.hashCode() ^ packageIdentifier.hashCode(); + return hashCode; } /** @@ -497,7 +502,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin return false; } Label otherLabel = (Label) other; - return name.equals(otherLabel.name) // least likely one first + // Perform the equality comparisons in order from least likely to most likely. + return hashCode == otherLabel.hashCode && name.equals(otherLabel.name) && packageIdentifier.equals(otherLabel.packageIdentifier); } |