Skip to content

Commit b4972af

Browse files
author
Ben Newman
committed
Optimize watch.isUpToDate for rebuilds.
Another ~400ms saved.
1 parent 4b9ef82 commit b4972af

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

tools/fs/watch.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,39 @@ export class Watcher {
697697
}
698698
}
699699

700-
// Given a WatchSet, returns true if it currently describes the state of the
701-
// disk.
702-
export function isUpToDate(watchSet) {
700+
// Given a WatchSet, returns true if it currently describes the state of
701+
// the disk. If mtimeWindowMs is a number, return true if none of the
702+
// watched files were touched since that many milliseconds ago.
703+
export function isUpToDate(watchSet, mtimeWindowMs) {
704+
if (typeof mtimeWindowMs === "number") {
705+
const now = +new Date;
706+
let recentlyChanged = false;
707+
708+
for (var file in watchSet.files) {
709+
var stat = files.statOrNull(file);
710+
var hash = watchSet.files[file];
711+
712+
if (typeof hash === "string") {
713+
if (! stat || stat.isDirectory()) {
714+
recentlyChanged = true;
715+
break;
716+
}
717+
} else if (stat) {
718+
recentlyChanged = true;
719+
break;
720+
}
721+
722+
if (stat && (now - stat.mtime < mtimeWindowMs)) {
723+
recentlyChanged = true;
724+
break;
725+
}
726+
}
727+
728+
if (! recentlyChanged) {
729+
return true;
730+
}
731+
}
732+
703733
return Profile.time('watch.isUpToDate', () => {
704734
var upToDate = true;
705735
var watcher = new Watcher({

tools/isobuild/isopack-cache.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,11 @@ _.extend(exports.IsopackCache.prototype, {
410410
// Merge in the watchsets for all unibuilds and plugins in the package, then
411411
// check it once.
412412
var watchSet = previousIsopack.getMergedWatchSet();
413-
return watch.isUpToDate(watchSet);
413+
414+
// Since we've checked this isopack previously, take a shortcut by not
415+
// considering it out of date unless any of its files have mtimes
416+
// within the last 30 seconds.
417+
return watch.isUpToDate(watchSet, 30 * 1000);
414418
},
415419

416420
_isopackDir: function (packageName) {

0 commit comments

Comments
 (0)