-
Notifications
You must be signed in to change notification settings - Fork 103
perf(cache-utils): use native fs traversal #7104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
203e3cb
397c422
f6010b6
42d4d52
b9cbdbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| import { createHash } from 'crypto' | ||
| import { createReadStream } from 'fs' | ||
| import { createReadStream, promises as fs } from 'fs' | ||
|
|
||
| import getStream from 'get-stream' | ||
| import { locatePath } from 'locate-path' | ||
|
|
||
| // We need a hashing algorithm that's as fast as possible. | ||
| // Userland CRC32 implementations are actually slower than Node.js SHA1. | ||
|
|
@@ -11,13 +10,26 @@ const HASH_ALGO = 'sha1' | |
| // Caching a big directory like `node_modules` is slow. However, those can | ||
| // sometime be represented by a digest file such as `package-lock.json`. If this | ||
| // has not changed, we don't need to save cache again. | ||
| export const getHash = async function (digests, move) { | ||
| export const getHash = async function (digests: string[], move: boolean) { | ||
| // Moving files is faster than computing hashes | ||
| if (move || digests.length === 0) { | ||
| return | ||
| } | ||
|
|
||
| const digestPath = await locatePath(digests) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like there are two behavioural changes here (see
Maybe we can just steal the logic from its source here. Not sure if these are important (e.g. maybe directories aren't possible? maybe the paths are always absolute? no idea). Maybe add some test coverage for this too?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel like im missing something because this is same as my other comment:
just like all |
||
| let digestPath: string | undefined = undefined | ||
|
|
||
| for (const digest of digests) { | ||
| try { | ||
| const stat = await fs.stat(digest) | ||
| if (stat.isFile()) { | ||
| digestPath = digest | ||
| break | ||
| } | ||
| } catch { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| if (digestPath === undefined) { | ||
| return | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { join } from 'path' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { readdirpPromise } from 'readdirp' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { join, relative } from 'path' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { readdir } from 'node:fs/promises' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Dirent } from 'node:fs' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getCacheDir } from './dir.js' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { isManifest } from './manifest.js' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,11 +36,33 @@ const listBase = async function ({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cacheDir: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| depth?: number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const files = await readdirpPromise(`${cacheDir}/${name}`, { fileFilter, depth, type: 'files_directories' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filesA = files.map(({ path }) => join(base, path)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return filesA | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let queue = [{ path: `${cacheDir}/${name}`, depth: 0 }] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const results: string[] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let queueEntry: { path: string; depth: number } | undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while ((queueEntry = queue.pop())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { path: currentPath, depth: currentDepth } = queueEntry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let children: Dirent[] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| children = await readdir(currentPath, { withFileTypes: true }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((error as NodeJS.ErrnoException).code === 'ENOENT') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const child of children) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const childPath = join(child.parentPath, child.name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((child.isDirectory() || child.isFile()) && fileFilter(child.name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| results.push(join(base, relative(`${cacheDir}/${name}`, childPath))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((depth === undefined || currentDepth < depth) && child.isDirectory()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queue.push({ path: childPath, depth: currentDepth + 1 }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return results | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+63
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm reading all this right, I think this complexity isn't worthwhile, since the only reason we aren't using a simple
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i agree. will update 👍
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👁️ bump!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dug into this a bit and its actually because we need depth control. if we don't care about that anymore, we can collapse it down to what you suggested. i need to remind myself why we have a depth... |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fileFilter = function ({ basename }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fileFilter = function (basename) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return !isManifest(basename) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.