From 2543f2ee9403c0fd657f5056e86c98ee3e518d8f Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Sun, 7 Jun 2026 05:34:54 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=EC=84=B1=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0]=20=ED=8C=8C=EC=9D=BC=20=EC=8B=9C=EC=8A=A4?= =?UTF-8?q?=ED=85=9C=20=ED=83=90=EC=83=89=20=EC=B5=9C=EC=A0=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `fs.readdirSync`에 `{ withFileTypes: true }` 옵션을 추가하여 불필요한 `fs.statSync` 호출 제거 - 성능 향상 및 시스템 호출 감소 --- .jules/bolt.md | 3 +++ src/files.ts | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..2bc648d --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-14 - Optimize file system traversal +**Learning:** `fs.readdirSync(..., { withFileTypes: true })` is significantly faster than combining `fs.readdirSync` with `fs.statSync` in Node.js because it avoids extra syscalls for statting files that can be returned directly by the directory read. This was a critical bottleneck when recursively reading large sets of spec markdown files in this local-first CLI. +**Action:** Always prefer `withFileTypes: true` when doing recursive directory reads in Node.js, unless we specifically need full stat info. diff --git a/src/files.ts b/src/files.ts index b1e1e43..07462ea 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,4 +1,4 @@ -import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"; +import { existsSync, readdirSync, readFileSync } from "node:fs"; import { basename, dirname, join, relative, resolve } from "node:path"; export type VspecConfig = { vspec_format: 1; key_prefix: string; spec_language?: "ko" | "en" | "match-input" }; @@ -27,10 +27,9 @@ export function projectKey(start = process.cwd()): string | null { export function walkFiles(root: string, predicate: (path: string) => boolean): string[] { if (!existsSync(root)) return []; const files: string[] = []; - for (const entry of readdirSync(root)) { - const path = join(root, entry); - const stat = statSync(path); - if (stat.isDirectory()) files.push(...walkFiles(path, predicate)); + for (const entry of readdirSync(root, { withFileTypes: true })) { + const path = join(root, entry.name); + if (entry.isDirectory()) files.push(...walkFiles(path, predicate)); else if (predicate(path)) files.push(path); } return files.sort();