Skip to content

Commit eee3b89

Browse files
committed
Trim redundant statSync calls in FileEntryCache
1 parent 6c34287 commit eee3b89

1 file changed

Lines changed: 14 additions & 54 deletions

File tree

packages/knip/src/util/file-entry-cache.ts

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,15 @@ export class FileEntryCache<T> {
4040
constructor(cacheId: string, _path: string) {
4141
this.filePath = path.resolve(_path, cacheId);
4242
if (isFile(this.filePath)) this.cache = create(this.filePath);
43-
this.removeNotFoundFiles();
44-
}
45-
46-
removeNotFoundFiles() {
47-
for (const filePath of this.normalizedEntries.keys()) {
48-
try {
49-
fs.statSync(filePath);
50-
} catch (error) {
51-
// @ts-expect-error
52-
if (error.code === 'ENOENT') this.cache.delete(filePath);
53-
}
54-
}
5543
}
5644

5745
getFileDescriptor(filePath: string): FileDescriptor<T> {
58-
let fstat: fs.Stats;
46+
if (!isAbsolute(filePath)) filePath = resolve(filePath);
47+
const existing = this.normalizedEntries.get(filePath);
48+
if (existing) return existing;
5949

50+
let fstat: fs.Stats;
6051
try {
61-
if (!isAbsolute(filePath)) filePath = resolve(filePath);
6252
fstat = fs.statSync(filePath);
6353
} catch (error: unknown) {
6454
this.removeEntry(filePath);
@@ -70,29 +60,25 @@ export class FileEntryCache<T> {
7060

7161
_getFileDescriptorUsingMtimeAndSize(filePath: string, fstat: fs.Stats) {
7262
let meta = this.cache.get(filePath);
73-
const cacheExists = Boolean(meta);
74-
7563
const cSize = fstat.size;
7664
const cTime = fstat.mtime.getTime();
7765

78-
let isDifferentDate: undefined | boolean;
79-
let isDifferentSize: undefined | boolean;
80-
66+
let changed = false;
8167
if (meta) {
82-
isDifferentDate = cTime !== meta.mtime;
83-
isDifferentSize = cSize !== meta.size;
68+
if (cTime !== meta.mtime || cSize !== meta.size) {
69+
changed = true;
70+
meta.data = undefined;
71+
}
72+
meta.mtime = cTime;
73+
meta.size = cSize;
8474
} else {
75+
changed = true;
8576
meta = { size: cSize, mtime: cTime };
77+
this.cache.set(filePath, meta);
8678
}
8779

88-
const fd: FileDescriptor<T> = {
89-
key: filePath,
90-
changed: !cacheExists || isDifferentDate || isDifferentSize,
91-
meta,
92-
};
93-
80+
const fd: FileDescriptor<T> = { key: filePath, changed, meta };
9481
this.normalizedEntries.set(filePath, fd);
95-
9682
return fd;
9783
}
9884

@@ -102,33 +88,7 @@ export class FileEntryCache<T> {
10288
this.cache.delete(entryName);
10389
}
10490

105-
_getMetaForFileUsingMtimeAndSize(cacheEntry: FileDescriptor<T>) {
106-
const stat = fs.statSync(cacheEntry.key);
107-
const meta = Object.assign(cacheEntry.meta ?? {}, {
108-
size: stat.size,
109-
mtime: stat.mtime.getTime(),
110-
});
111-
return meta;
112-
}
113-
11491
reconcile() {
115-
for (const [entryName, cacheEntry] of this.normalizedEntries) {
116-
try {
117-
const stat = fs.statSync(entryName);
118-
const meta = Object.assign(cacheEntry.meta ?? {}, {
119-
size: stat.size,
120-
mtime: stat.mtime.getTime(),
121-
});
122-
this.cache.set(entryName, meta);
123-
} catch (error) {
124-
// @ts-expect-error
125-
if (error.code === 'ENOENT') {
126-
this.normalizedEntries.delete(entryName);
127-
this.cache.delete(entryName);
128-
} else throw error;
129-
}
130-
}
131-
13292
try {
13393
const dir = dirname(this.filePath);
13494
if (!isDirectory(dir)) fs.mkdirSync(dir, { recursive: true });

0 commit comments

Comments
 (0)