Skip to content

Commit b023c32

Browse files
committed
perf(fmt): use empty cache hash sentinel
1 parent dc604f3 commit b023c32

8 files changed

Lines changed: 16 additions & 20 deletions

File tree

packages/rstack/src/fmt/cacheStore.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ const fmtCacheStateIds = {
2020
unsupported: 2,
2121
} as const satisfies Record<FmtCacheState, FmtCacheStateId>;
2222

23-
type FmtCacheFileValue = string | number | null;
24-
type FmtCacheEntry =
25-
| readonly [contentHash: string, optionsHash: string, state: 'clean' | 'dirty']
26-
| readonly [contentHash: string | null, optionsHash: string, state: 'unsupported'];
23+
type FmtCacheFileValue = string | number;
24+
type FmtCacheEntry = readonly [contentHash: string, optionsHash: string, state: FmtCacheState];
2725

2826
interface FmtCacheFile {
2927
version: typeof fmtCacheVersion;
@@ -147,12 +145,10 @@ class FmtCacheStoreImpl implements FmtCacheStore {
147145
}
148146

149147
const { files, options } = this.#cache;
150-
const contentHash = files[offset + contentHashOffset] as string | null;
148+
const contentHash = files[offset + contentHashOffset] as string;
151149
const optionsHash = options[files[offset + optionsIndexOffset] as number];
152150
const state = fmtCacheStates[files[offset + stateOffset] as FmtCacheStateId];
153-
return state === 'unsupported'
154-
? [contentHash, optionsHash, state]
155-
: [contentHash as string, optionsHash, state];
151+
return [contentHash, optionsHash, state];
156152
}
157153

158154
set(filePath: string, entry: FmtCacheEntry): void {

packages/rstack/src/fmt/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const isCachedUnsupported = ({ file, cache }: FmtFileRunTask): boolean => {
116116
return false;
117117
}
118118
return (
119-
cache.entry[0] === null &&
119+
cache.entry[0] === '' &&
120120
cache.entry[1] === cache.optionsHash &&
121121
cache.entry[2] === 'unsupported' &&
122122
hasDottedBasename(file.path)

packages/rstack/src/fmt/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const formatFile = async ({
4242
if (cache?.entry && cache.entry[1] === cache.optionsHash) {
4343
const { entry } = cache;
4444
if (entry[2] === 'unsupported') {
45-
if (entry[0] === null) {
45+
if (entry[0] === '') {
4646
if (hasDottedBasename(file.path)) {
4747
return { status: 'unsupported' };
4848
}
@@ -70,7 +70,7 @@ const formatFile = async ({
7070
status: 'unsupported',
7171
cacheEntry: [
7272
hasDottedBasename(file.path)
73-
? null
73+
? ''
7474
: (contentHash ?? createCacheHash(sourceBuffer ?? readFileSync(file.path))),
7575
cache.optionsHash,
7676
'unsupported',

packages/rstack/tests/cli/fmt/cache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface SerializedFmtCache {
88
version: number;
99
namespace: string;
1010
options: string[];
11-
files: (string | number | null)[];
11+
files: (string | number)[];
1212
}
1313

1414
const readFmtCache = (filePath: string): SerializedFmtCache =>

packages/rstack/tests/fmt/cacheStore.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const optionsB = createCacheHash('options-b');
1919
const optionsC = createCacheHash('options-c');
2020
const firstEntry = [contentA, optionsA, 'clean'] as const;
2121
const secondEntry = [contentB, optionsB, 'dirty'] as const;
22-
const unsupportedEntry = [null, optionsC, 'unsupported'] as const;
22+
const unsupportedEntry = ['', optionsC, 'unsupported'] as const;
2323
const hashedUnsupportedEntry = [contentC, optionsC, 'unsupported'] as const;
2424

2525
const readCache = (filePath: string): FmtCacheFile =>
@@ -48,7 +48,7 @@ test('writes flat entries that can be loaded by another store', async () => {
4848
0,
4949
0,
5050
'src/unknown.fixture',
51-
null,
51+
'',
5252
1,
5353
2,
5454
'script',

packages/rstack/tests/fmt/runnerCache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test('caches unsupported parser results until final options change', async () =>
126126
processedFileCount: 0,
127127
});
128128
expect((await loadFmtCacheStore(cache.filePath, cacheNamespace)).get('data.unknown')).toEqual([
129-
null,
129+
'',
130130
createOptionsHasher()(unsupported.options),
131131
'unsupported',
132132
]);

packages/rstack/tests/fmt/runnerWorkerPreflight.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const createCachedUnsupportedFile = async (rootPath: string, fileName: string) =
3434
}
3535

3636
const store = await loadFmtCacheStore(cache.filePath, cacheNamespace);
37-
store.set(fileName, [null, optionsHash, 'unsupported']);
37+
store.set(fileName, ['', optionsHash, 'unsupported']);
3838
await expect(store.save()).resolves.toBe(true);
3939

4040
return { cache, file };

packages/rstack/tests/fmt/worker.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ test('returns cached states before resolving the parser', async () => {
2020
[[contentHash, optionsHash, 'clean'], filePath, true, 'unchanged'],
2121
[[contentHash, optionsHash, 'unsupported'], noExtensionPath, false, 'unsupported'],
2222
[[contentHash, optionsHash, 'unsupported'], noExtensionPath, true, 'unsupported'],
23-
[[null, optionsHash, 'unsupported'], missingPath, false, 'unsupported'],
24-
[[null, optionsHash, 'unsupported'], missingPath, true, 'unsupported'],
23+
[['', optionsHash, 'unsupported'], missingPath, false, 'unsupported'],
24+
[['', optionsHash, 'unsupported'], missingPath, true, 'unsupported'],
2525
] as const) {
2626
await expect(
2727
formatFile({
@@ -54,7 +54,7 @@ test('does not trust path-only unsupported entries for files without extensions'
5454
},
5555
shouldWrite: false,
5656
cache: {
57-
entry: [null, 'options', 'unsupported'],
57+
entry: ['', 'options', 'unsupported'],
5858
optionsHash: 'options',
5959
},
6060
}),
@@ -81,7 +81,7 @@ test('resolves parser support before reading on a cache miss', async () => {
8181
}),
8282
).resolves.toEqual({
8383
status: 'unsupported',
84-
cacheEntry: [null, 'options', 'unsupported'],
84+
cacheEntry: ['', 'options', 'unsupported'],
8585
});
8686
});
8787
});

0 commit comments

Comments
 (0)