From 0bb1f0afc088be2522a1136577bc2745c26dea4a Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 22:17:32 +0900 Subject: [PATCH] =?UTF-8?q?perf(coverage):=20=EC=9C=84=ED=97=98=20?= =?UTF-8?q?=EC=BB=A4=EB=B2=84=EB=A6=AC=EC=A7=80=20=EB=AA=A8=EB=93=88=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=EC=9D=84=20Babel=20=ED=8C=8C=EC=84=9C?= =?UTF-8?q?=EC=99=80=20=EB=8F=99=EA=B8=B0=20fs=EB=A1=9C=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A0=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: `buildProductionModuleInventory`가 `src/` 모듈 375개를 하나씩 `@babel/eslint-parser`로 읽고, 파일마다 lstat→realpath→open→stat→ read→close 여섯 비동기 호출을 차례로 기다린다. runner에서 이 목록을 만드는 시험(「historical 14-file summary」)이 unit 8.2초·coverage 9.8초였다(run 113). check:risk-coverage도 같은 목록을 만든다. 고른 방법: - 파서: @babel/core `parseSync`(babelrc·configFile 끔)로 Babel AST를 읽는다. eslint-parser는 같은 파서를 돈 뒤 트리와 토큰 전체를 ESTree로 바꾼다. 여기서 보는 최상위 문장의 type·declare·const·importKind· declaration·body는 두 트리에서 같다. repo 모듈 375개 전부에서 두 파서의 문장 모양을 대조해 차이 0을 확인했다. 시간 845 → 305ms. `scripts/check-architecture.ts`가 같은 방식을 이미 쓴다. - fs: 기본 구현이 같은 호출을 같은 순서로 동기로 한다. 부하가 큰 기계에서는 호출마다 thread pool을 거쳐 event loop 차례를 기다리는 시간이 일보다 컸다. 주입하는 fs(경쟁 시험용)는 그대로 async다. symlink·파일 바꿔치기 검사와 오류 순서는 바뀌지 않는다. - 버린 대안: 파일을 병렬로 처리하기. 대기는 줄지만 여러 파일이 잘못됐을 때 어느 오류를 내는지가 달라진다. 시험 timeout 늘리기는 규칙 밖이다. 위험·한계: 목록을 만드는 동안 event loop를 잡는다(desktop 약 0.7초). gate CLI와 시험만 부른다. 검증(desktop, 같은 세션에서 고치기 전·후를 번갈아 3회): - 부하 11~12: 그 시험 1,913/1,420/1,938 → 715/642/742ms. - coverage + CPU 소모 프로세스 12개(부하 6~16): 8,033/8,493/10,347 → 4,703/5,039/6,121ms. - risk-coverage·risk-coverage-files 시험 47개 통과, check:coverage:fixture는 전처럼 기준 미달로 rc=1, check:types·eslint rc=0. 로그 /tmp/claude-gates/w2fe2-slow3·5·6/, w2fe2-parse1/. --- scripts/lib/risk-coverage.ts | 70 ++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/scripts/lib/risk-coverage.ts b/scripts/lib/risk-coverage.ts index 9a12908a..719c5890 100644 --- a/scripts/lib/risk-coverage.ts +++ b/scripts/lib/risk-coverage.ts @@ -1,12 +1,17 @@ -import { constants, type Dirent, type Stats } from "node:fs"; import { - lstat, - open, - readdir, - realpath, -} from "node:fs/promises"; + closeSync, + constants, + type Dirent, + fstatSync, + lstatSync, + openSync, + readdirSync, + readFileSync, + realpathSync, + type Stats, +} from "node:fs"; import path from "node:path"; -import babelParser from "@babel/eslint-parser"; +import { parseSync } from "@babel/core"; import { COVERAGE_SUMMARY_METRICS as coverageMetrics, @@ -426,19 +431,24 @@ export function hasCoverageCounterBearingStatements( source: string, relativePath: string, ): boolean { + // Babel's own parser and AST. @babel/eslint-parser ran the same parser and + // then converted the whole tree and its tokens to ESTree, about 2.8 times + // the cost over the repository, and the top-level statement types and + // flags read here are the same in both trees. Every production module is + // parsed once per inventory, and the inventory is built in the gate and in + // its tests. let parsed: unknown; try { - parsed = babelParser.parse(source, { + parsed = parseSync(source, { + filename: relativePath, + babelrc: false, + configFile: false, sourceType: "module", - requireConfigFile: false, - filePath: relativePath, - babelOptions: { - parserOpts: { - plugins: [ - "typescript", - ...(relativePath.endsWith(".tsx") ? ["jsx"] : []), - ], - }, + parserOpts: { + plugins: [ + "typescript", + ...(relativePath.endsWith(".tsx") ? (["jsx"] as const) : []), + ], }, }); } catch (error) { @@ -446,10 +456,11 @@ export function hasCoverageCounterBearingStatements( cause: error, }); } - if (!isRecord(parsed) || !Array.isArray(parsed.body)) { + const program = isRecord(parsed) ? parsed.program : undefined; + if (!isRecord(program) || !Array.isArray(program.body)) { throw new TypeError("TypeScript parser returned an invalid program"); } - return parsed.body.some(statementIsCoverageCounterBearing); + return program.body.some(statementIsCoverageCounterBearing); } export async function buildProductionModuleInventory( @@ -457,12 +468,25 @@ export async function buildProductionModuleInventory( ): Promise { const repositoryRoot = path.resolve(options.repositoryRoot ?? process.cwd()); const sourceRoot = path.join(repositoryRoot, "src"); + // The default file system makes the same calls in the same order as the + // fs/promises one, synchronously. The walk awaits about six calls per + // module one after another; through the thread pool each waited for an + // event-loop turn, and on a loaded CI machine that wait, not the work, made + // the inventory take seconds. Injected file systems keep the async shape. const readDirectory = options.readDirectory ?? - ((target: string) => readdir(target, { withFileTypes: true })); - const lstatPath = options.lstatPath ?? lstat; - const realpathPath = options.realpathPath ?? realpath; + (async (target: string) => readdirSync(target, { withFileTypes: true })); + const lstatPath = options.lstatPath ?? (async (target: string) => lstatSync(target)); + const realpathPath = options.realpathPath ?? + (async (target: string) => realpathSync.native(target)); const openFile = options.openFile ?? - ((target: string, flags: number) => open(target, flags)); + (async (target: string, flags: number): Promise => { + const descriptor = openSync(target, flags); + return { + stat: async () => fstatSync(descriptor), + readFile: async (encoding) => readFileSync(descriptor, encoding), + close: async () => closeSync(descriptor), + }; + }); const generatedPaths = uniquePaths(options.generatedPaths ?? [], "generatedPaths"); const generated = new Set(generatedPaths); const repositoryRealpath = await realpathPath(repositoryRoot);