small optimization
Massimo Melina committed
May 4, 2025 at 20:53 UTC
877395a79789e8ef179f8c650e87b8452ec46a35
1 file changed
+3
-18
src/vfs.ts
+3
-18
@@ -297,7 +297,6 @@ export async function* walkNode(parent: VfsNode, {
297
const { children, source } = parent
298
const taken = prefixPath ? undefined : new Set()
299
const maskApplier = parentMaskApplier(parent)
300
- const parentsCache = new Map() // we use this only if depth > 0
300
const visitLater: any = []
301
if (children) for (const child of children) {
302
const nodeName = getNodeName(child)
@@ -312,7 +311,7 @@ export async function* walkNode(parent: VfsNode, {
311
if (onlyFiles ? !isFolder : (!onlyFolders || isFolder))
312
stream.push(item)
313
if (!depth || !isFolder || cantRecur(item)) continue
315
- parentsCache.set(name, item)
314
+ inheritMasks(item, parent)
315
visitLater.push([item, name]) // prioritize siblings
316
}
317
@@ -325,9 +324,6 @@ export async function* walkNode(parent: VfsNode, {
324
return
325
326
try {
328
- let lastDir = prefixPath.slice(0, -1) || '.'
329
- parentsCache.set(lastDir, parent)
330
- const root = parent
327
await walkDir(source, { depth, ctx, hidden: showHiddenFiles.get(), parallelizeRecursion }, async entry => {
328
if (ctx?.isAborted()) {
329
stream.push(null)
@@ -337,7 +333,7 @@ export async function* walkNode(parent: VfsNode, {
333
return
334
const {path} = entry
335
const isFolder = entry.isDirectory()
340
- let renamed = root.rename?.[path]
336
+ let renamed = parent.rename?.[path]
337
if (renamed) {
338
const dir = dirname(path) // if `path` isn't just the name, copy its dir in renamed
339
if (dir !== '.')
@@ -346,11 +342,6 @@ export async function* walkNode(parent: VfsNode, {
342
const name = prefixPath + (renamed || path)
343
if (taken?.has(normalizeFilename(name))) // taken by vfs node above
344
return false // false just in case it's a folder
349
- if (depth) {
350
- const dir = dirname(name)
351
- if (dir !== lastDir)
352
- parent = parentsCache.get(lastDir = dir)
353
- }
345
346
const item: VfsNode = { name, isFolder, source: join(source, path) }
347
if (await cantSee(item)) // can't see: don't produce and don't recur
@@ -359,16 +350,11 @@ export async function* walkNode(parent: VfsNode, {
350
stream.push(item)
351
if (cantRecur(item))
352
return false
362
- if (isFolder)
363
- parentsCache.set(name, item)
364
- entry.closingBranch?.then(p =>
365
- parentsCache.delete(p || '.'))
353
})
354
}
355
catch(e) {
356
console.debug('walkNode', source, e) // ENOTDIR, or lacking permissions
357
}
371
- parentsCache.clear() // hoping for faster GC
358
}
359
finally {
360
for (const [item, name] of visitLater)
@@ -378,8 +364,7 @@ export async function* walkNode(parent: VfsNode, {
364
}
365
366
function cantRecur(item: VfsNode) {
381
- if (ctx && !hasPermission(item, 'can_list', ctx)) return true
382
- inheritMasks(item, parent)
367
+ return ctx && !hasPermission(item, 'can_list', ctx)
368
}
369
370
// item will be changed, so be sure to pass a temp node