From 998b5ca5ac3467fcf6d83920507bfa6fb5a11f41 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 26 Jun 2026 15:26:08 +0800 Subject: [PATCH] =?UTF-8?q?perf(config/findings):=20getOpenCodeHttpsAgent?= =?UTF-8?q?=20=E6=94=B9=E6=A8=A1=E7=B5=84=E5=96=AE=E4=BE=8B=E3=80=81normal?= =?UTF-8?q?izeText=20=E5=8A=A0=20memoization=20=E5=BF=AB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 另含 comments.js bySeverity 改用預建 LEVEL_RANK Map(隨 fix commit 一併提交)。 Co-Authored-By: Claude Opus 4.8 (1M context) --- app/config.js | 8 +++++--- app/findings.js | 8 +++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/config.js b/app/config.js index 9ecc17b..3f205d1 100644 --- a/app/config.js +++ b/app/config.js @@ -16,12 +16,14 @@ export const EXCLUSIONS_PATH = '.gitea/ai-review/exclusions.json'; * 建立一個停用 TLS 憑證驗證(`rejectUnauthorized: false`)的 HTTPS Agent, * 供連接使用自簽或無效憑證的 OpenCode 服務時使用。 * - * @remarks 每次呼叫都會回傳全新的 Agent 實例(不快取),建議呼叫端重用以共用連線池。 + * @remarks 首次呼叫時建立,之後快取為模組層級單例(singleton)重複使用, + * 避免每次都新建 Agent 與連線池、浪費 TCP 三次握手。 * 停用憑證驗證有中間人攻擊風險,僅限受信任的內部環境使用。 - * @returns {import('https').Agent} 已關閉憑證驗證的 HTTPS Agent 實例。 + * @returns {import('https').Agent} 已關閉憑證驗證的 HTTPS Agent 單例。 */ +let _openCodeHttpsAgent = null; export function getOpenCodeHttpsAgent() { - return new https.Agent({ rejectUnauthorized: false }); + return (_openCodeHttpsAgent ??= new https.Agent({ rejectUnauthorized: false })); } /** diff --git a/app/findings.js b/app/findings.js index 896aec4..3b51c9b 100644 --- a/app/findings.js +++ b/app/findings.js @@ -107,14 +107,20 @@ function cleanText(value) { * @param {*} value - 任意值;非字串會先經 cleanText 轉為空字串。 * @returns {string} 正規化後、以單一空白分隔的字串(可能為空字串)。 * @remarks 用於 finding 與排除條目文字的雙向「包含」比對(applyExclusions、appendExclusions)。 + * 因為比對常對同一段文字重複呼叫(findings × exclusions 笛卡爾積), + * 以模組層級 Map 對「字串輸入」做 memoization,避免重複跑 NFKC/正則替換。 */ +const _normalizeTextCache = new Map(); export function normalizeText(value) { - return cleanText(value) + if (typeof value === 'string' && _normalizeTextCache.has(value)) return _normalizeTextCache.get(value); + const result = cleanText(value) .normalize('NFKC') .toLowerCase() .replace(/[\p{P}\p{S}\s]+/gu, ' ') .replace(/\s+/g, ' ') .trim(); + if (typeof value === 'string') _normalizeTextCache.set(value, result); + return result; } /**