Skip to content

Commit

Permalink
Defer creation of cache directory
Browse files Browse the repository at this point in the history
We've been redundantly telling the system to re-create the cache
directory for transformed files, even when just looking up
already-transformed ones.
This syscall has a light impact when occurring in large codebases.

Improve this by deferring `createDirectory` until right before
we save the "transform result".

Signed-off-by: Mitchell Hentges <[email protected]>
  • Loading branch information
mitchhentgesspotify committed Nov 8, 2022
1 parent a570638 commit 46cc735
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ class ScriptTransformer {
);
}

private _createFolderFromCacheKey(
filename: string,
cacheKey: string,
): string {
private _createCachedFilename(filename: string, cacheKey: string): string {
const HasteMapClass = HasteMap.getStatic(this._config);
const baseCacheDir = HasteMapClass.getCacheFilePath(
this._config.cacheDirectory,
Expand All @@ -223,12 +220,7 @@ class ScriptTransformer {
const cacheFilenamePrefix = path
.basename(filename, path.extname(filename))
.replace(/\W/g, '');
const cachePath = slash(
path.join(cacheDir, `${cacheFilenamePrefix}_${cacheKey}`),
);
createDirectory(cacheDir);

return cachePath;
return slash(path.join(cacheDir, `${cacheFilenamePrefix}_${cacheKey}`));
}

private _getFileCachePath(
Expand All @@ -238,7 +230,7 @@ class ScriptTransformer {
): string {
const cacheKey = this._getCacheKey(content, filename, options);

return this._createFolderFromCacheKey(filename, cacheKey);
return this._createCachedFilename(filename, cacheKey);
}

private async _getFileCachePathAsync(
Expand All @@ -248,7 +240,7 @@ class ScriptTransformer {
): Promise<string> {
const cacheKey = await this._getCacheKeyAsync(content, filename, options);

return this._createFolderFromCacheKey(filename, cacheKey);
return this._createCachedFilename(filename, cacheKey);
}

private _getTransformPath(filename: string) {
Expand Down Expand Up @@ -504,6 +496,7 @@ class ScriptTransformer {
});
}

createDirectory(path.dirname(cacheFilePath));
return this._buildTransformResult(
filename,
cacheFilePath,
Expand Down Expand Up @@ -568,6 +561,7 @@ class ScriptTransformer {
});
}

createDirectory(path.dirname(cacheFilePath));
return this._buildTransformResult(
filename,
cacheFilePath,
Expand Down

0 comments on commit 46cc735

Please sign in to comment.