diff options
author | 2018-01-09 13:05:23 -0800 | |
---|---|---|
committer | 2018-01-09 13:06:59 -0800 | |
commit | 784db69f81adb869c673682831e538c733c33716 (patch) | |
tree | ae45374cf4d2c269dba497ddf95277d3a2ba41e7 /src/main/java/com/google/devtools/build/lib/cmdline | |
parent | 995e157edbea8732769d63c3e0aecd29d6fa9774 (diff) |
Stop memoizing Label#hashCode()
Both members of Label (String & PackageIdentifier) have memoized hash codes so
this should be marginally more expensive but probably not noticably so. The
benefit is it makes Label objects smaller in certain vm conditions.
As to why things were the way they were, I believe this is from before
PackageIdentifier memoized its hashCode.
RELNOTES: None
PiperOrigin-RevId: 181362077
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/cmdline/Label.java | 25 |
1 files changed, 12 insertions, 13 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 caa5c5ec25..47a7d7bc17 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 @@ -35,6 +35,7 @@ import com.google.devtools.build.skyframe.SkyKey; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.Serializable; +import java.util.Arrays; import javax.annotation.Nullable; /** @@ -276,22 +277,12 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu /** The name of the target within the package. Canonical. */ private final String name; - /** Precomputed hash code. */ - private final int hashCode; - private Label(PackageIdentifier packageIdentifier, String name) { Preconditions.checkNotNull(packageIdentifier); Preconditions.checkNotNull(name); this.packageIdentifier = packageIdentifier; this.name = name; - this.hashCode = hashCode(this.name, this.packageIdentifier); - } - - /** A specialization of Arrays.HashCode() that does not require constructing a 2-element array. */ - private static final int hashCode(Object obj1, Object obj2) { - int result = 31 + (obj1 == null ? 0 : obj1.hashCode()); - return 31 * result + (obj2 == null ? 0 : obj2.hashCode()); } private Object writeReplace() { @@ -527,7 +518,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu @Override public int hashCode() { - return hashCode; + return hashCode(name, packageIdentifier); } /** Two labels are equal iff both their name and their package name are equal. */ @@ -538,8 +529,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu } Label otherLabel = (Label) other; // Perform the equality comparisons in order from least likely to most likely. - return hashCode == otherLabel.hashCode - && name.equals(otherLabel.name) + return name.equals(otherLabel.name) && packageIdentifier.equals(otherLabel.packageIdentifier); } @@ -582,4 +572,13 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkValu public void str(SkylarkPrinter printer) { printer.append(getCanonicalForm()); } + + /** + * Specialization of {@link Arrays#hashCode()} that does not require constructing a 2-element + * array. + */ + private static final int hashCode(Object obj1, Object obj2) { + int result = 31 + (obj1 == null ? 0 : obj1.hashCode()); + return 31 * result + (obj2 == null ? 0 : obj2.hashCode()); + } } |