Skip to content

Commit

Permalink
perf: optimize joins
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Dec 9, 2023
1 parent b35aefa commit a98c9b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/vite/src/node/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ function ensureFileMaybeSymlinkIsResolved(
isSymlink === undefined ? 'error' : isSymlink ? 'symlink' : 'file'
}

function pathUntilPart(root: string, parts: string[], i: number): string {
let p = root
for (let k = 0; k < i; k++) p += '/' + parts[k]
return p
}

export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
const { root } = config
const rootDirPath = `${root}/`
Expand All @@ -135,7 +141,7 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
if (direntCache.type === 'directory') {
let dirPath
if (!direntCache.dirents) {
dirPath = path.posix.join(root, ...parts.slice(0, i))
dirPath = pathUntilPart(root, parts, i)
const dirents = readDirCacheSync(dirPath)
if (!dirents) {
direntCache.type = 'error'
Expand All @@ -148,7 +154,7 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
return
}
if (nextDirentCache.type === 'directory_maybe_symlink') {
dirPath ??= path.posix.join(root, ...parts.slice(0, i))
dirPath ??= pathUntilPart(root, parts, i)
const isSymlink = fs
.lstatSync(dirPath, { throwIfNoEntry: false })
?.isSymbolicLink()
Expand All @@ -167,7 +173,7 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
if (direntCache.type === 'file_maybe_symlink') {
ensureFileMaybeSymlinkIsResolved(
direntCache,
path.posix.join(root, ...parts.slice(0, i)),
pathUntilPart(root, parts, i),
)
return direntCache
} else if (direntCache.type === 'file') {
Expand Down Expand Up @@ -306,7 +312,7 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
const fileName = base + ext
const fileDirentCache = direntCache.dirents.get(fileName)
if (fileDirentCache) {
const filePath = path.posix.join(dirPath, fileName)
const filePath = dirPath + '/' + fileName
ensureFileMaybeSymlinkIsResolved(fileDirentCache, filePath)
if (fileDirentCache.type === 'symlink') {
// fallback to built-in fs for symlinked files
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ export function resolveServerOptions(
strict: server.fs?.strict ?? true,
allow: allowDirs,
deny,
cachedChecks: server.fs?.cachedChecks ?? false,
cachedChecks: server.fs?.cachedChecks ?? true,
}

if (server.origin?.endsWith('/')) {
Expand Down

0 comments on commit a98c9b4

Please sign in to comment.