Skip to content

Commit

Permalink
Avoid a superfluous stat() in DigestUtil.
Browse files Browse the repository at this point in the history
Similar in spirit to bb80319. DigestUtil wraps DigestUtils, trading in Digests instead of byte arrays.

There are probably other callsites that would benefit from the optimization, but I'm focused on UploadManifest at the moment.

PiperOrigin-RevId: 607695005
Change-Id: Iaa9b1f07b26f05482cb6f3fe8dfaacf50e0c07f5
  • Loading branch information
tjgq authored and Copybara-Service committed Feb 16, 2024
1 parent 0e7b867 commit a9b5bff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void addFiles(Collection<Path> files) throws ExecException, IOException, Interru
continue;
}
if (statNoFollow.isFile() && !statNoFollow.isSpecialFile()) {
Digest digest = digestUtil.compute(file, statNoFollow.getSize());
Digest digest = digestUtil.compute(file, statNoFollow);
addFile(digest, file);
continue;
}
Expand All @@ -264,7 +264,7 @@ void addFiles(Collection<Path> files) throws ExecException, IOException, Interru
if (statFollow.isFile() && !statFollow.isSpecialFile()) {
if (followSymlinks || target.isAbsolute()) {
// Symlink to file uploaded as a file.
addFile(digestUtil.compute(file), file);
addFile(digestUtil.compute(file, statFollow), file);
} else {
// Symlink to file uploaded as a symlink.
if (target.isAbsolute()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.DigestUtils;
import com.google.devtools.build.lib.vfs.FileStatus;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.XattrProvider;
import com.google.protobuf.Message;
Expand Down Expand Up @@ -67,12 +68,27 @@ public Digest compute(byte[] blob) {
return buildDigest(hashFn.getHashFunction().hashBytes(blob).toString(), blob.length);
}

public Digest compute(Path file) throws IOException {
return compute(file, file.getFileSize());
/**
* Computes a digest for a file.
*
* <p>Prefer calling {@link #compute(Path, FileStatus)} when a recently obtained {@link
* FileStatus} is available.
*
* @param path the file path
*/
public Digest compute(Path path) throws IOException {
return compute(path, path.stat());
}

public Digest compute(Path file, long fileSize) throws IOException {
return buildDigest(DigestUtils.getDigestWithManualFallback(file, xattrProvider), fileSize);
/**
* Computes a digest for a file.
*
* @param path the file path
* @param status a recently obtained file status, if available
*/
public Digest compute(Path path, FileStatus status) throws IOException {
return buildDigest(
DigestUtils.getDigestWithManualFallback(path, xattrProvider, status), status.getSize());
}

public Digest compute(VirtualActionInput input) throws IOException {
Expand Down

0 comments on commit a9b5bff

Please sign in to comment.