aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-04-08 20:28:09 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-08 20:29:21 -0700
commit825946bb036c10b0340d02638b18cec2edb2a5c2 (patch)
tree95fb7e85a8624414d8b19b6448bc24b767533fa3 /src/main/java/com/google/devtools/build/lib/actions/Artifact.java
parent47d1e21d182eaee34a13a11ebf2da3eb733037cf (diff)
Serialize Artifacts by their root-relative path rather than their exec path. The exec path contains redundant information when considered with the ArtifactRoot, so, since the ArtifactRoot is memoized, we can save space and time by only serializing the non-redundant root-relative path part.
PiperOrigin-RevId: 192076469
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/actions/Artifact.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/Artifact.java50
1 files changed, 41 insertions, 9 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
index 58c72204a3..68af8179d5 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
@@ -167,6 +167,32 @@ public class Artifact
private final ArtifactOwner owner;
/**
+ * The {@code rootRelativePath is a few characters shorter than the {@code execPath} for derived
+ * artifacts, so we save a few bytes by serializing it rather than the {@code execPath},
+ * especially when the {@code root} is common to many artifacts and therefore memoized.
+ */
+ @AutoCodec.VisibleForSerialization
+ @AutoCodec.Instantiator
+ static Artifact createForSerialization(
+ ArtifactRoot root, ArtifactOwner owner, PathFragment rootRelativePath) {
+ if (rootRelativePath == null || rootRelativePath.isAbsolute() != root.getRoot().isAbsolute()) {
+ throw new IllegalArgumentException(
+ rootRelativePath
+ + ": illegal rootRelativePath for "
+ + rootRelativePath
+ + " (root: "
+ + root
+ + ")");
+ }
+ PathFragment rootExecPath = root.getExecPath();
+ return new Artifact(
+ root,
+ rootExecPath.isEmpty() ? rootRelativePath : rootExecPath.getRelative(rootRelativePath),
+ rootRelativePath,
+ owner);
+ }
+
+ /**
* Constructs an artifact for the specified path, root and execPath. The root must be an ancestor
* of path, and execPath must be a non-absolute tail of path. Outside of testing, this method
* should only be called by ArtifactFactory. The ArtifactOwner may be null.
@@ -186,13 +212,24 @@ public class Artifact
* </pre>
*/
@VisibleForTesting
- @AutoCodec.Instantiator
public Artifact(ArtifactRoot root, PathFragment execPath, ArtifactOwner owner) {
- Preconditions.checkNotNull(root);
- if (execPath == null || execPath.isAbsolute() != root.getRoot().isAbsolute()) {
+ this(
+ Preconditions.checkNotNull(root),
+ Preconditions.checkNotNull(execPath, "Null execPath not allowed (root %s", root),
+ root.getExecPath().isEmpty() ? execPath : execPath.relativeTo(root.getExecPath()),
+ Preconditions.checkNotNull(owner));
+ if (execPath.isAbsolute() != root.getRoot().isAbsolute()) {
throw new IllegalArgumentException(
execPath + ": illegal execPath for " + execPath + " (root: " + root + ")");
}
+ }
+
+ private Artifact(
+ ArtifactRoot root,
+ PathFragment execPath,
+ PathFragment rootRelativePath,
+ ArtifactOwner owner) {
+ Preconditions.checkNotNull(root);
if (execPath.isEmpty()) {
throw new IllegalArgumentException(
"it is illegal to create an artifact with an empty execPath");
@@ -200,12 +237,7 @@ public class Artifact
this.hashCode = execPath.hashCode();
this.root = root;
this.execPath = execPath;
- PathFragment rootExecPath = root.getExecPath();
- if (rootExecPath.isEmpty()) {
- this.rootRelativePath = execPath;
- } else {
- this.rootRelativePath = execPath.relativeTo(root.getExecPath());
- }
+ this.rootRelativePath = rootRelativePath;
this.owner = Preconditions.checkNotNull(owner);
}