aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-04-01 15:19:27 +0000
committerGravatar Klaus Aehlig <aehlig@google.com>2016-04-01 18:25:10 +0000
commit081d202c0137fe7ed795de77cf4d479977510adc (patch)
tree0b9fa156a99b67131df83fe5059c3cba9bcf680a /src/main/java/com
parentfdcb1244c63b40dae2c1a0bdd37271e8bf92109a (diff)
Enable all Label instantiations to use a single intern pool to optimize memory usage
Label instances do not share a single intern pool today, as some other objects, such as SkyKey and PackageIdentifier. This change allows all Label instantiations to use a single intern pool for better memory usage. -- MOS_MIGRATED_REVID=118780952
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java22
1 files changed, 12 insertions, 10 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 030e689b82..d8d5a46b9d 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
@@ -15,6 +15,8 @@ package com.google.devtools.build.lib.cmdline;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
import com.google.devtools.build.lib.cmdline.LabelValidator.BadLabelException;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
@@ -63,6 +65,8 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
public static final String EXTERNAL_PATH_PREFIX = "external";
+ private static final Interner<Label> LABEL_INTERNER = Interners.newWeakInterner();
+
/**
* Factory for Labels from absolute string form. e.g.
* <pre>
@@ -106,9 +110,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
if (repo.isEmpty() && ABSOLUTE_PACKAGE_NAMES.contains(packageFragment)) {
repo = "@";
}
- return new Label(
- PackageIdentifier.create(repo, packageFragment),
- labelParts.getTargetName());
+ return create(PackageIdentifier.create(repo, packageFragment), labelParts.getTargetName());
} catch (BadLabelException e) {
throw new LabelSyntaxException(e.getMessage());
}
@@ -144,7 +146,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
* @throws LabelSyntaxException if either of the arguments was invalid.
*/
public static Label create(String packageName, String targetName) throws LabelSyntaxException {
- return new Label(packageName, targetName);
+ return LABEL_INTERNER.intern(new Label(packageName, targetName));
}
/**
@@ -153,7 +155,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
*/
public static Label create(PackageIdentifier packageId, String targetName)
throws LabelSyntaxException {
- return new Label(packageId, targetName);
+ return LABEL_INTERNER.intern(new Label(packageId, targetName));
}
/**
@@ -189,7 +191,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
PathFragment path = workspaceRelativePath.getRelative(label.substring(0, index));
// Use the String, String constructor, to make sure that the package name goes through the
// validity check.
- return new Label(path.getPathString(), label.substring(index + 1));
+ return create(path.getPathString(), label.substring(index + 1));
}
/**
@@ -409,7 +411,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
* @throws LabelSyntaxException if {@code targetName} is not a valid target name
*/
public Label getLocalTargetLabel(String targetName) throws LabelSyntaxException {
- return new Label(packageIdentifier, targetName);
+ return create(packageIdentifier, targetName);
}
/**
@@ -468,9 +470,9 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
return relative;
} else {
try {
- return new Label(
- PackageIdentifier
- .create(packageIdentifier.getRepository(), relative.getPackageFragment()),
+ return create(
+ PackageIdentifier.create(
+ packageIdentifier.getRepository(), relative.getPackageFragment()),
relative.getName());
} catch (LabelSyntaxException e) {
// We are creating the new label from an existing one which is guaranteed to be valid, so