Skip to content

Commit

Permalink
optimize invalidations
Browse files Browse the repository at this point in the history
  • Loading branch information
habdelra committed Jan 8, 2025
1 parent 805bd24 commit d7d1562
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
39 changes: 29 additions & 10 deletions packages/host/app/lib/current-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ export class CurrentRun {
`completed getting index mtimes in ${Date.now() - mtimesStart} ms`,
);
let invalidateStart = Date.now();
await current.discoverInvalidations(current.realmURL, mtimes);
let invalidations = (
await current.discoverInvalidations(current.realmURL, mtimes)
).map((href) => new URL(href));
console.log(
`completed invalidations in ${Date.now() - invalidateStart} ms`,
);
let invalidations = current.batch.invalidations.map(
(href) => new URL(href),
);

await current.whileIndexing(async () => {
let visitStart = Date.now();
Expand Down Expand Up @@ -257,7 +256,7 @@ export class CurrentRun {
private async discoverInvalidations(
url: URL,
indexMtimes: LastModifiedTimes,
): Promise<void> {
): Promise<string[]> {
log.debug(`discovering invalidations in dir ${url.href}`);
console.log(`discovering invalidations in dir ${url.href}`);
let ignoreStart = Date.now();
Expand All @@ -275,6 +274,8 @@ export class CurrentRun {
console.log(
`time to get file system mtimes ${Date.now() - mtimesStart} ms`,
);
let invalidationList: string[] = [];
let skipList: string[] = [];
for (let [url, lastModified] of Object.entries(filesystemMtimes)) {
if (!url.endsWith('.json') && !hasExecutableExtension(url)) {
// Only allow json and executable files to be invalidated so that we
Expand All @@ -290,13 +291,31 @@ export class CurrentRun {
indexEntry.lastModified == null ||
lastModified !== indexEntry.lastModified
) {
let invalidationStart = Date.now();
await this.batch.invalidate(new URL(url));
console.log(
`time to invalidate ${url} ${Date.now() - invalidationStart} ms`,
);
invalidationList.push(url);
} else {
skipList.push(url);
}
}
if (skipList.length === 0) {
// the whole realm needs to be visited, no need to calculate
// invalidations--it's everything
console.log(
`entire realm ${this.realmURL.href} is invalidated--skipping invalidation calculation`,
);
return invalidationList;
}

console.log(
`unable to use optimized invalidation for realm ${
this.realmURL.href
}, skipped these files ${JSON.stringify(skipList)}`,
);
let invalidationStart = Date.now();
await this.batch.invalidate(new URL(url));
console.log(
`time to invalidate ${url} ${Date.now() - invalidationStart} ms`,
);
return this.batch.invalidations;
}

private async visitFile(
Expand Down
19 changes: 8 additions & 11 deletions packages/runtime-common/index-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ export class Batch {
// css is a subset of modules, so there won't by any references that
// are css entries that aren't already represented by a module entry
[`i.type != 'css'`],
// probably need to reevaluate this condition when we get to cross
// realm invalidation
[`i.realm_url =`, param(this.realmURL.href)],
]),
'ORDER BY i.url COLLATE "POSIX"',
`LIMIT ${pageSize} OFFSET ${pageNumber * pageSize}`,
Expand Down Expand Up @@ -528,17 +531,11 @@ export class Batch {
return [];
}
visited.add(resolvedPath);
let childInvalidations = await this.itemsThatReference(resolvedPath);
let realmPath = new RealmPaths(this.realmURL);
let invalidationsInThisRealm = childInvalidations.filter((c) =>
realmPath.inRealm(new URL(c.url)),
);

let invalidations = invalidationsInThisRealm.map(({ url }) => url);
let aliases = invalidationsInThisRealm.map(
({ alias: moduleAlias, type, url }) =>
// for instances we expect that the deps for an entry always includes .json extension
type === 'instance' ? url : moduleAlias,
let items = await this.itemsThatReference(resolvedPath);
let invalidations = items.map(({ url }) => url);
let aliases = items.map(({ alias: moduleAlias, type, url }) =>
// for instances we expect that the deps for an entry always includes .json extension
type === 'instance' ? url : moduleAlias,
);
let results = [
...invalidations,
Expand Down

0 comments on commit d7d1562

Please sign in to comment.