aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Eric Fellheimer <felly@google.com>2016-06-24 18:23:13 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-06-27 11:39:19 +0000
commitb32eea7ee5c1f3bd4fc753fa72c99f1d5b8259ea (patch)
treeb967913a58efc314eddd34f6c42f4114f0bee2d0 /src/main
parentdd548b0b50a1648e5684970fe9cd4de6a93be67f (diff)
Fix some GC churn issues when dealing with Labels:
- reduce use of #substring(), which must copy the sub-region of the string - specialize hashCode() implementation so that we don't need to push objects into an array. -- MOS_MIGRATED_REVID=125798978
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java11
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java12
2 files changed, 18 insertions, 5 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 73c7e5ecdb..75d4677365 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,7 +32,6 @@ 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.
@@ -274,7 +273,15 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
}
throw e;
}
- this.hashCode = Objects.hash(this.name, this.packageIdentifier);
+ 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() {
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java b/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
index 52c7e0e1fd..42510c7f56 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
@@ -165,11 +165,11 @@ public final class LabelValidator {
continue;
}
if (c == '/') {
- if (targetName.substring(ii).startsWith("/../")) {
+ if (stringRegionMatch(targetName, "/../", ii)) {
return "target names may not contain up-level references '..'";
- } else if (targetName.substring(ii).startsWith("/./")) {
+ } else if (stringRegionMatch(targetName, "/./", ii)) {
return "target names may not contain '.' as a path segment";
- } else if (targetName.substring(ii).startsWith("//")) {
+ } else if (stringRegionMatch(targetName, "//", ii)) {
return "target names may not contain '//' path separators";
}
continue;
@@ -194,6 +194,12 @@ public final class LabelValidator {
return null; // ok
}
+ // Prefer this implementation over calls to String#subString(), as the latter implies copying
+ // the subregion.
+ private static boolean stringRegionMatch(String fullString, String possibleMatch, int offset) {
+ return fullString.regionMatches(offset, possibleMatch, 0, possibleMatch.length());
+ }
+
/**
* Validate the label and parse it into a pair of package name and target name. If the label is
* not valid, it throws an {@link BadLabelException}.