Repository navigation
perf(index): make parent lookup O(1) and stop atime-only snapshot rewrites - #27
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 24, 2026 17:54
dylan-openhands
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two file-count scaling problems in the in-memory index. Both are measured against the real
internal/indexcode on a real 25,181-file npm tree (3,069 dirs, 28,319 directory entries, 2.9 MB CBOR snapshot).1.
GetParentInoscanned the whole filesystemReaddircallsGetParentInoonce 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.A full readdir sweep over all 3,070 directories:
GetParentInoThe map is maintained at every site that can change a child's parent —
AddDirEntry,MakeFile,MakeDir,MakeSymlink,RemoveDirEntry,Rename— and rebuilt inFromSnapshotinside the loop that already rebuildsdirIndex, so mount cost is unchanged (54.6 → 54.8 ms).Renameonly drops the mapping when the overwritten inode is actually freed, so a hardlinked inode that survives an overwrite keeps its parent pointer.2.
TouchAtimeforced a full snapshot every persist intervalReads 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):Save()Atime changes now set
atimeDirtyinstead. The periodic ticker still gates onIsDirty()/IsRemoteDirty(), but the final flush on unmount includesIsAtimeDirty(), 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/cborInodegain no fields and the snapshot is byte-identical in size — so old and new binaries read each other's snapshots.go test ./...andgo vet ./...pass.New tests in
internal/index/atime_parent_test.gocover the contracts the existing suite missed:TestAtimeDoesNotDirtyIndex— atime alone must not setIsDirty, andMarkCleanclears both flagsTestStructuralChangeStillDirty— structural mutations still setIsDirtyTestParentMapConsistency— parent pointer survives directory rename, cross-parent move,RemoveDirEntry, and a snapshot round-tripScope 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 upnpm 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/fusewas 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