← Back to Pull requests

Avoid per-entry stats when listing in-memory directories #32063

Open
dzbarsky opened this pull request with 155 lines diff across 5 files
View on GitHub
dzbarsky dzbarsky commented on Oct 25, 2026 17:05 UTC
Author

InMemoryFileSystem.readdir obtains Dirent types directly from child inodes for NOFOLLOW, avoiding a stat and inode lookup per entry. readdir sizes the result list from the directory’s child count and checks search permission once for nonempty directories. Unreadable and non-executable directories retain FileSystem.readdir errors. FOLLOW still uses FileSystem.readdir. The existing InMemoryFileSystemTest regression covers unreadable, empty non-executable, and nonempty non-executable directories.

In the original fixture restoring 256 cached repositories and 131,072 files, median analysis time improved from 3.356 to 2.923 s and command time from 9.258 to 8.807 s. These measurements precede the review changes to list capacity and permission checking. The complete PR diff passes git diff --check.

@fmeum

Diff: 155 lines (+49 / -106) across 5 files Reviewer: fmeum Review duration: 52962m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.46) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.46 · 155 lines diff across 5 files
0.46
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.302
Real PR Conversation Timeline
6 items
fmeum fmeum commented on code on Oct 25, 2026 22:29 UTC
Code Review
src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java:1
@@ -560,6 +561,32 @@ public synchronized Collection<String> getDirectoryEntries(PathFragment path) th
     return result;
   }
 
+  @Override
+  public Collection<Dirent> readdir(PathFragment path, boolean followSymlinks) throws IOException {
+    if (followSymlinks) {
+      return super.readdir(path, true);
+    }
+    synchronized (this) {
+      InMemoryDirectoryInfo dirInfo = getDirectory(path);
+      if (!dirInfo.isReadable()) {
+        throw Errno.EACCES.exception(path);
+      }
+      // The entries already carry their types; avoid walking from the root for each child.
+      Collection<String> children = dirInfo.getAllChildren();
+      List<Dirent> result = new ArrayList<>(children.size());

Are children expected to contain . and ..? If so, would presizing with -2 be better?

fmeum fmeum commented on code on Oct 25, 2026 22:30 UTC
Code Review
src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java:1
@@ -560,6 +561,32 @@ public synchronized Collection<String> getDirectoryEntries(PathFragment path) th
     return result;
   }
 
+  @Override
+  public Collection<Dirent> readdir(PathFragment path, boolean followSymlinks) throws IOException {
+    if (followSymlinks) {
+      return super.readdir(path, true);
+    }
+    synchronized (this) {
+      InMemoryDirectoryInfo dirInfo = getDirectory(path);
+      if (!dirInfo.isReadable()) {
+        throw Errno.EACCES.exception(path);
+      }
+      // The entries already carry their types; avoid walking from the root for each child.
+      Collection<String> children = dirInfo.getAllChildren();
+      List<Dirent> result = new ArrayList<>(children.size());
+      for (String child : children) {
+        if (child.equals(".") || child.equals("..")) {
+          continue;
+        }
+        if (!dirInfo.isExecutable()) {

Is it worth pulling this out with a size check?

dzbarsky dzbarsky commented on code on Oct 26, 2026 00:02 UTC
Code Review
src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java:1
@@ -560,6 +561,32 @@ public synchronized Collection<String> getDirectoryEntries(PathFragment path) th
     return result;
   }
 
+  @Override
+  public Collection<Dirent> readdir(PathFragment path, boolean followSymlinks) throws IOException {
+    if (followSymlinks) {
+      return super.readdir(path, true);
+    }
+    synchronized (this) {
+      InMemoryDirectoryInfo dirInfo = getDirectory(path);
+      if (!dirInfo.isReadable()) {
+        throw Errno.EACCES.exception(path);
+      }
+      // The entries already carry their types; avoid walking from the root for each child.
+      Collection<String> children = dirInfo.getAllChildren();
+      List<Dirent> result = new ArrayList<>(children.size());

Yes. newRootInode inserts both entries for the root, and insertChildDirectory inserts both before publishing every other directory. I changed the capacity to children.size() - 2 in ad82a753a4.

dzbarsky dzbarsky commented on code on Oct 26, 2026 00:02 UTC
Code Review
src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java:1
@@ -560,6 +561,32 @@ public synchronized Collection<String> getDirectoryEntries(PathFragment path) th
     return result;
   }
 
+  @Override
+  public Collection<Dirent> readdir(PathFragment path, boolean followSymlinks) throws IOException {
+    if (followSymlinks) {
+      return super.readdir(path, true);
+    }
+    synchronized (this) {
+      InMemoryDirectoryInfo dirInfo = getDirectory(path);
+      if (!dirInfo.isReadable()) {
+        throw Errno.EACCES.exception(path);
+      }
+      // The entries already carry their types; avoid walking from the root for each child.
+      Collection<String> children = dirInfo.getAllChildren();
+      List<Dirent> result = new ArrayList<>(children.size());
+      for (String child : children) {
+        if (child.equals(".") || child.equals("..")) {
+          continue;
+        }
+        if (!dirInfo.isExecutable()) {

Done in ad82a753a4: childCount > 0 && !dirInfo.isExecutable() is now checked once before the loop. On that error case I use FileSystem.readdir so the exception still names the first real child; an empty readable directory still succeeds without execute permission. The ordinary NOFOLLOW loop no longer checks permissions per entry.

fmeum fmeum commented on Oct 26, 2026 12:17 UTC
Contributor

CI fails in a way that may be related

Reviewers
Assignees
dzbarsky dzbarsky
Labels
risk: low confidence: 89.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 46.1% (MEDIUM) · Higher = larger blast radius
46.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.2% · Lower = rushed or shallow review
89.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.0% · Safe to merge
5.0%