Skip to content

Commit 64e5072

Browse files
committed
Tighten cache module callsites
1 parent 7ffdc2f commit 64e5072

5 files changed

Lines changed: 16 additions & 18 deletions

File tree

packages/knip/src/CacheConsultant.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class CacheConsultant<T> {
2222
if (this.isEnabled && this.cache) return this.cache.getFileDescriptor(filePath);
2323
return dummyFileDescriptor;
2424
}
25+
public getCachedFile(filePath: string): T | undefined {
26+
if (!this.isEnabled || !this.cache) return undefined;
27+
const fd = this.cache.getFileDescriptor(filePath);
28+
return !fd.changed ? fd.meta?.data : undefined;
29+
}
2530
public reconcile() {
2631
if (this.isEnabled && this.cache) this.cache.reconcile();
2732
}

packages/knip/src/ProjectPrincipal.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ export class ProjectPrincipal {
194194
const isProjectPath = this.projectPaths.has(filePath);
195195

196196
// Cached project files: skip read+parse and pass the cached FileNode through.
197-
let cachedFile: FileNode | undefined;
198-
if (isProjectPath) {
199-
const fd = this.cache.getFileDescriptor(filePath);
200-
if (!fd.changed && fd.meta?.data) cachedFile = fd.meta.data;
201-
}
197+
const cachedFile = isProjectPath ? this.cache.getCachedFile(filePath) : undefined;
202198

203199
if (cachedFile) {
204200
const internalPaths = analyzeFile(filePath, undefined, '', cachedFile);
@@ -277,8 +273,8 @@ export class ProjectPrincipal {
277273
) {
278274
if (cachedFile) return cachedFile;
279275

280-
const fd = this.cache.getFileDescriptor(filePath);
281-
if (!fd.changed && fd.meta?.data) return fd.meta.data;
276+
const cached = this.cache.getCachedFile(filePath);
277+
if (cached) return cached;
282278

283279
sourceText ??= this.fileManager.readFile(filePath);
284280

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export class FileEntryCache<T> {
5555
return { key: filePath, notFound: true, err: error };
5656
}
5757

58-
return this._getFileDescriptorUsingMtimeAndSize(filePath, fstat);
59-
}
60-
61-
_getFileDescriptorUsingMtimeAndSize(filePath: string, fstat: fs.Stats) {
6258
let meta = this.cache.get(filePath);
6359
const cSize = fstat.size;
6460
const cTime = fstat.mtime.getTime();

packages/knip/src/util/gitignore-cache.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { deserialize, serialize } from 'node:v8';
55
import { version } from '../version.ts';
66
import { debugLog } from './debug.ts';
77
import { isDirectory, isFile } from './fs.ts';
8+
import type { Gitignores } from './glob-core.ts';
89
import { dirname } from './path.ts';
910

10-
interface PerDirIgnores {
11+
interface SerializedGitignores {
1112
ignores: string[];
1213
unignores: string[];
1314
}
@@ -20,7 +21,7 @@ interface GitignoreCacheEntry {
2021
ignores: string[];
2122
unignores: string[];
2223
/** Absolute dir path → cached ignores/unignores for that dir */
23-
perDirIgnores: Record<string, PerDirIgnores>;
24+
perDirIgnores: Record<string, SerializedGitignores>;
2425
/** Sorted workspace dirs, joined with \0 — invalidates when workspace set changes */
2526
workspaceDirsKey: string;
2627
}
@@ -29,7 +30,7 @@ export interface CachedGitignoreResult {
2930
ignores: Set<string>;
3031
unignores: Set<string>;
3132
gitignoreFiles: string[];
32-
perDirIgnores: Map<string, { ignores: Set<string>; unignores: Set<string> }>;
33+
perDirIgnores: Map<string, Gitignores>;
3334
}
3435

3536
const CACHE_FILENAME = `gitignore-${version}.cache`;
@@ -84,7 +85,7 @@ export const getCachedGitignore = (cwd: string, workspaceDirs?: Set<string>): Ca
8485
isDirty = true;
8586
return undefined;
8687
}
87-
const perDirIgnores = new Map<string, { ignores: Set<string>; unignores: Set<string> }>();
88+
const perDirIgnores = new Map<string, Gitignores>();
8889
for (const dir in entry.perDirIgnores) {
8990
const data = entry.perDirIgnores[dir];
9091
perDirIgnores.set(dir, { ignores: new Set(data.ignores), unignores: new Set(data.unignores) });
@@ -103,7 +104,7 @@ export const setCachedGitignore = (
103104
gitignoreFiles: string[],
104105
ignores: Set<string>,
105106
unignores: Set<string>,
106-
perDirIgnores: Map<string, { ignores: Set<string>; unignores: Set<string> }>
107+
perDirIgnores: Map<string, Gitignores>
107108
): void => {
108109
if (!cache) return;
109110
const mtimes: number[] = [];
@@ -117,7 +118,7 @@ export const setCachedGitignore = (
117118
// File was removed between read and stat; skip rather than poison cache
118119
}
119120
}
120-
const perDir: Record<string, PerDirIgnores> = {};
121+
const perDir: Record<string, SerializedGitignores> = {};
121122
for (const [dir, data] of perDirIgnores) {
122123
perDir[dir] = { ignores: [...data.ignores], unignores: [...data.unignores] };
123124
}

packages/knip/src/util/glob-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface GlobOptions extends TinyGlobOptions {
2222
label?: string;
2323
}
2424

25-
type Gitignores = { ignores: Set<string>; unignores: Set<string> };
25+
export type Gitignores = { ignores: Set<string>; unignores: Set<string> };
2626

2727
// ignore patterns are cached per gitignore file
2828
const cachedGitIgnores = new Map<string, Gitignores>();

0 commit comments

Comments
 (0)