From ffa8004bd8448cb0c2e1681611505cee2c09db65 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Wed, 7 Feb 2024 19:55:04 +0100 Subject: [PATCH] [7.1.0] Avoid exception-based control flow in RemoteActionFileSystem#stat. (#21236) When statting a file in the local filesystem, we first call InMemoryFileSystem#stat, which unnecessarily allocates and throws a FileNotFoundException. Instead, call InMemoryFileSystem#statIfFound, which doesn't. PiperOrigin-RevId: 604253151 Change-Id: Iabb9573f710e657a9ea893f1f7577a6b4bb147d2 --- .../build/lib/remote/RemoteActionFileSystem.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteActionFileSystem.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteActionFileSystem.java index a24ffac2a14b95..d7e130d8709bbc 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteActionFileSystem.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteActionFileSystem.java @@ -605,14 +605,16 @@ private FileStatus statUnchecked( } } - try { - return remoteOutputTree.stat(path, /* followSymlinks= */ false); - } catch (FileNotFoundException e) { - if (statSources == StatSources.ALL) { - return localFs.getPath(path).stat(Symlinks.NOFOLLOW); - } - throw e; + FileStatus stat = remoteOutputTree.statIfFound(path, /* followSymlinks= */ false); + if (stat != null) { + return stat; } + + if (statSources == StatSources.ALL) { + return localFs.getPath(path).stat(Symlinks.NOFOLLOW); + } + + throw new FileNotFoundException(path.getPathString() + " (No such file or directory)"); } private static FileStatusWithMetadata statFromMetadata(FileArtifactValue m) {