Skip to content

Commit

Permalink
Avoid unnecessary copying in getDirectoryEntries implementations
Browse files Browse the repository at this point in the history
`File#list` is documented to never return `"."` and `".."`.

Closes bazelbuild#23435.

PiperOrigin-RevId: 668327754
Change-Id: I0beb70a82232990a83671485dcd97208ea4bd147
  • Loading branch information
fmeum authored and copybara-github committed Aug 28, 2024
1 parent 1ab2c82 commit bf2eff8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -142,11 +142,7 @@ protected Collection<String> getDirectoryEntries(PathFragment path) throws IOExc
} finally {
profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, name);
}
Collection<String> result = new ArrayList<>(entries.length);
for (String entry : entries) {
result.add(entry);
}
return result;
return Arrays.asList(entries);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -92,7 +92,7 @@ private LinkOption[] linkOpts(boolean followSymlinks) {
@Override
protected Collection<String> getDirectoryEntries(PathFragment path) throws IOException {
File file = getIoFile(path);
String[] entries = null;
String[] entries;
long startTime = Profiler.nanoTimeMaybe();
try {
entries = file.list();
Expand All @@ -106,13 +106,7 @@ protected Collection<String> getDirectoryEntries(PathFragment path) throws IOExc
} finally {
profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, file.getPath());
}
Collection<String> result = new ArrayList<>(entries.length);
for (String entry : entries) {
if (!entry.equals(".") && !entry.equals("..")) {
result.add(entry);
}
}
return result;
return Arrays.asList(entries);
}

@Override
Expand Down

0 comments on commit bf2eff8

Please sign in to comment.