@yolk_vat-y/dsh-project-memory 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/watch.js CHANGED
@@ -1,10 +1,31 @@
1
- import { existsSync, statSync } from 'node:fs'
1
+ import { existsSync } from 'node:fs'
2
+ import { stat } from 'node:fs/promises'
2
3
  import path from 'node:path'
3
4
  import { listProjectFiles, memoryDirName, memoryRootFor, relativePath, rootRejection, scanLimits, storeKey } from './util/fs.js'
4
5
  import { OVERSIZE, UNCHANGED, commitFileUpdates, fileKind, planFileIndex, toFileUpdate } from './index-pipeline.js'
5
6
  import { ProjectMemoryStore } from './store.js'
6
7
  import { onFileChanged, isTypeScriptFile } from './enhancer.js'
7
8
 
9
+ /**
10
+ * 有界并发 map。watch 轮询要对每个文件 stat;同步 `statSync` 循环在 Windows(尤其 WSL 挂
11
+ * `/mnt/c`)会把事件循环卡住:400 文件实测 ~0.9s、12k 文件按同一比例是几十秒,期间面板、
12
+ * 其它会话、工具调用全部停摆。改成 `fs/promises.stat` + 16 并发:总量不变,但主线程在等 I/O
13
+ * 时可以让出去,Windows 的线程池也能并行吃掉多次元数据查询。
14
+ */
15
+ async function mapLimit(items, limit, fn) {
16
+ const out = new Array(items.length)
17
+ let next = 0
18
+ const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => {
19
+ for (;;) {
20
+ const index = next++
21
+ if (index >= items.length) return
22
+ out[index] = await fn(items[index], index)
23
+ }
24
+ })
25
+ await Promise.all(workers)
26
+ return out
27
+ }
28
+
8
29
  export class WatchManager {
9
30
  constructor(ctx, config) {
10
31
  this.ctx = ctx
@@ -243,18 +264,23 @@ export class WatchManager {
243
264
  )
244
265
  }
245
266
 
246
- for (const filePath of files) {
267
+ // 先把所有文件的元数据并发取回来;下面的解析仍按顺序(真正的解析量只与"变了的文件"有关)。
268
+ const statsByIndex = await mapLimit(files, 16, async (filePath) => {
269
+ try {
270
+ return await stat(filePath)
271
+ } catch {
272
+ return null
273
+ }
274
+ })
275
+ for (let fileIndex = 0; fileIndex < files.length; fileIndex++) {
276
+ const filePath = files[fileIndex]
247
277
  const rel = storeKey(relativePath(root, filePath))
248
278
  seen.add(rel)
249
279
  const kind = fileKind(path.extname(filePath).toLowerCase())
250
280
  if (!kind) continue
251
281
 
252
- let stats
253
- try {
254
- stats = statSync(filePath)
255
- } catch {
256
- continue
257
- }
282
+ const stats = statsByIndex[fileIndex]
283
+ if (!stats) continue
258
284
  const sig = `${stats.mtimeMs}:${stats.size}`
259
285
  if (state.snapshot[rel] === sig) continue
260
286