Skip to content

Commit 540ee5e

Browse files
committed
perf: walk dir, limit concurrency to 8 (was 32)
1 parent 786250c commit 540ee5e

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/asarUtil.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,48 @@ const pickle = require ("chromium-pickle-js")
1616
const Filesystem = require("asar-electron-builder/lib/filesystem")
1717
const UINT64 = require("cuint").UINT64
1818

19-
const MAX_FILE_REQUESTS = 32
19+
const MAX_FILE_REQUESTS = 8
2020
const concurrency = {concurrency: MAX_FILE_REQUESTS}
2121
const NODE_MODULES_PATTERN = path.sep + "node_modules" + path.sep
2222

2323
export async function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: Filter, addRootToResult?: boolean): Promise<Array<string>> {
2424
const childNames = await readdir(dirPath)
25-
const list = await BluebirdPromise.map(childNames, name => lstat(dirPath + path.sep + name), concurrency)
26-
.then(stats => BluebirdPromise.map(stats, (stat, index): any => {
27-
const filePath = dirPath + path.sep + childNames[index]
28-
if (filter != null && !filter(filePath, stat)) {
29-
return null
30-
}
31-
32-
if (consumer != null) {
33-
consumer(filePath, stat)
34-
}
35-
if (stat.isDirectory()) {
36-
return walk(filePath, consumer, filter, true)
37-
}
38-
return filePath
39-
}, concurrency))
40-
41-
list.sort((a, b) => {
42-
// files before directories
43-
if (Array.isArray(a) && Array.isArray(b)) {
44-
return 0
25+
const stats = await BluebirdPromise.map(childNames, name => lstat(dirPath + path.sep + name), concurrency)
26+
const dirs: Array<string> = []
27+
const files: Array<string> = addRootToResult ? [dirPath] : []
28+
await BluebirdPromise.map(stats, (stat, index): any => {
29+
const filePath = dirPath + path.sep + childNames[index]
30+
if (filter != null && !filter(filePath, stat)) {
31+
return null
4532
}
46-
else if (a == null || Array.isArray(a)) {
47-
return 1
33+
34+
if (consumer != null) {
35+
consumer(filePath, stat)
4836
}
49-
else if (b == null || Array.isArray(b)) {
50-
return -1
37+
38+
if (stat.isDirectory()) {
39+
dirs.push(filePath)
5140
}
5241
else {
53-
return a.localeCompare(b)
42+
files.push(filePath)
5443
}
55-
})
44+
}, concurrency)
5645

57-
const result: Array<string> = addRootToResult ? [dirPath] : []
58-
for (let item of list) {
59-
if (item != null) {
60-
if (Array.isArray(item)) {
61-
result.push.apply(result, item)
62-
}
63-
else {
64-
result.push(item)
65-
}
46+
files.sort()
47+
48+
if (dirs.length === 0) {
49+
return files
50+
}
51+
52+
dirs.sort()
53+
const list = await BluebirdPromise.map(dirs, dir => walk(dir, consumer, filter, true), concurrency)
54+
for (let subList of list) {
55+
for (let file of subList) {
56+
files.push(file)
6657
}
6758
}
68-
return result
59+
60+
return files
6961
}
7062

7163
export async function createAsarArchive(src: string, resourcesPath: string, options: AsarOptions, filter: Filter): Promise<any> {

0 commit comments

Comments
 (0)