Skip to content

perf(index): make parent lookup O(1) and stop atime-only snapshot rewrites - #27

Merged
tofarr merged 1 commit into
mainfrom
fix/readdir-parent-map-and-atime-persist
Nov 24, 2026
Merged

tofarr merged 1 commit into
mainfrom
fix/readdir-parent-map-and-atime-persist

Conversation

@tofarr

@tofarr tofarr commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two file-count scaling problems in the in-memory index. Both are measured against the real internal/index code on a real 25,181-file npm tree (3,069 dirs, 28,319 directory entries, 2.9 MB CBOR snapshot).

1. GetParentIno scanned the whole filesystem

Readdir calls GetParentIno once per directory to fill in ... The old implementation walked every entry of every directory to find one inode's parent, so listing a tree cost O(dirs × entries). On the 25k tree that is ~87 million map iterations.

// before — full scan per call
for parentIno, children := range idx.dirIndex {
    for _, childIno := range children {
        if childIno == ino { return parentIno }
    }
}

// after — child → parent map
if p, ok := idx.parent[ino]; ok { return p }

A full readdir sweep over all 3,070 directories:

before after
readdir sweep (3,070 dirs) 705 ms 2.5 ms (~282×)
of which GetParentIno 713 ms (100%) 0.07 ms (2.5%)

The map is maintained at every site that can change a child's parent — AddDirEntry, MakeFile, MakeDir, MakeSymlink, RemoveDirEntry, Rename — and rebuilt in FromSnapshot inside the loop that already rebuilds dirIndex, so mount cost is unchanged (54.6 → 54.8 ms).

Rename only drops the mapping when the overwritten inode is actually freed, so a hardlinked inode that survives an overwrite keeps its parent pointer.

2. TouchAtime forced a full snapshot every persist interval

Reads touch atime; atime set the persist-dirty flag; the ticker then rewrote and re-marshalled the entire index every FUSEY_PERSIST_INTERVAL. Over 120 ticks (one hour of pod uptime):

before after
full snapshot saves 120 1
time in Save() 5,443 ms 55 ms
per hour of uptime 5.4 s 0.1 s

Atime changes now set atimeDirty instead. The periodic ticker still gates on IsDirty()/IsRemoteDirty(), but the final flush on unmount includes IsAtimeDirty(), so atime precision is deferred rather than lost.

Cost

~27 bytes per directory entry (7.77 → 8.48 MB for 28,319 entries, +9%). Not persisted; rebuilt at mount.

Compatibility

The CBOR wire format is unchanged — cborSnapshot/cborInode gain no fields and the snapshot is byte-identical in size — so old and new binaries read each other's snapshots. go test ./... and go vet ./... pass.

New tests in internal/index/atime_parent_test.go cover the contracts the existing suite missed:

  • TestAtimeDoesNotDirtyIndex — atime alone must not set IsDirty, and MarkClean clears both flags
  • TestStructuralChangeStillDirty — structural mutations still set IsDirty
  • TestParentMapConsistency — parent pointer survives directory rename, cross-parent move, RemoveDirEntry, and a snapshot round-trip

Scope note

These fixes make tree walks (ls -R, find, tar, rsync, editor file trees) and steady-state read workloads much cheaper. They do not speed up npm install: strace shows npm reads only 12–51 directories per install because it resolves paths directly instead of scanning them, and index operations are under 0.2% of install wall time. The persist-tick saving is the part that applies broadly, since it hits any read-heavy workload.

The 282× readdir figure comes from a synthetic sweep over the real tree's directory structure, replayed against the real index code. No /dev/fuse was available in the test environment, so this is not an end-to-end mount measurement.

This PR was created by an AI agent (OpenHands) on behalf of the user.

@tofarr can click here to continue refining the PR

GetParentIno scanned every directory entry in the filesystem to resolve a
single inode's parent. Readdir calls it once per directory to fill in "..",
so listing a tree cost O(dirs x entries). On a 25k-file npm tree a full
readdir sweep took 705 ms, of which GetParentIno was 100%; with a
child->parent map it takes 2.5 ms.

Maintain the map at every mutation site (AddDirEntry, MakeFile, MakeDir,
MakeSymlink, RemoveDirEntry, Rename) and rebuild it in FromSnapshot, in the
same loop that already rebuilds dirIndex. Rename only drops the mapping when
the overwritten inode is actually freed, so hardlinked inodes keep theirs.

TouchAtime also set the persist-dirty flag, so a pure-read workload rewrote
the entire index snapshot every FUSEY_PERSIST_INTERVAL. Track atime changes
separately: over 120 ticks (1h of uptime) this drops full snapshot saves from
120 to 1 and time in Save() from 5.4 s to 0.1 s. The final flush on unmount
still persists atime-only changes, so precision is only deferred, not lost.

Memory cost is ~27 B per directory entry (0.7 MB for 28k entries), not
persisted, and rebuilt at mount. The CBOR wire format is unchanged, so old
and new binaries can read each other's snapshots.

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr marked this pull request as ready for review November 24, 2026 17:54
@tofarr
tofarr merged commit 46f718a into main Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants