fs: watch directories, not files, in recursive fs.watch fallback · nodejs/node@ac4cf54 · GitHub
Skip to content

Commit ac4cf54

Browse files
codebytereaduh95
authored andcommitted
fs: watch directories, not files, in recursive fs.watch fallback
The JavaScript recursive watcher used on platforms without a native one (notably Linux) armed an fs.watch() handle and a stat() for every file in the tree, and answered every event by stat()ing and re-reading the whole directory it happened in. A 13k-entry tree cost 13.5k inotify watches, ~150 ms and ~50 MB to set up, and appending to one file in a 3900-entry directory cost ~9 ms of CPU per event. A file replaced by rename() (the usual editor save) also stopped being reported, since its watch stayed on the old inode. inotify reports changes to the entries of a watched directory, with their names, so on Linux watch each directory once (symbolic links keep their own watcher, as before), keep the set of known paths, and resolve an event with a single stat() of the named entry: unknown names are added and reported as 'rename', vanished ones are dropped and reported as 'rename', file changes are reported as 'change'. kqueue and event ports only report that a directory changed, so on the other platforms served by this fallback every file keeps its own watcher and a directory event rescans that directory, as before. unref() and ref() now reach the underlying handles. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65486 Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 7804bfb commit ac4cf54

3 files changed

Lines changed: 282 additions & 108 deletions

File tree

Lines changed: 23 additions & 0 deletions

‎lib/internal/fs/recursive_watch.js‎

Lines changed: 151 additions & 108 deletions

0 commit comments

Comments
 (0)