perf(finder,orphan): 第一層掃描提速——category 記憶化(S3)+orphan 葉塊預計算(S2),保行為

下游效能訴求一(掃描疑 O(n²))之第一層:無狀態、保行為、無公開 API 變更之確定收益。

S3 category 記憶化:finder.category 以 categoryCache(WeakMap<Element, InlineCategory>)記憶化,砍
除 collectRuns/jiyaAdjacency/jinze/split 跨 pass 對同一元素之重複 el.matches()(pill/isolate/
avoid 選擇器)。安全:單次 render 內一元素之分類恆定(class/skip 屬性/nodeName 不因 render 變更;
jz-* 包裹按 nodeName 分類亦恆定),且 Finder 隨 makeCtx 每次 render 重建 → 快取僅存活單次 render、
無跨 render 失效(持久化屬 C1,另議)。亲驗(正確補 Element.prototype.matches):pill/isolate 密集
頁面 el.matches() 8204→5842(約降三成);暫禁快取則 computeCategory 2322 vs 1041(命中率 55%)。

S2 orphan 葉塊預計算:以一趟上攀預計算「非葉塊」Set 取代逐塊 block.querySelector(sel) 掃整棵子樹之
O(Σ塊子樹)(深塊嵌套下 O(n²));某塊為另一塊之最近塊祖先 ⟺ 含塊級後代,scope 無涉、與原
querySelector 同口徑,行為等價。sel 亦外提出迴圈。

測試 129→130(+1 鎖定 S2 非葉塊等價:含巢狀塊之容器跳過、僅內層葉塊綁定);全綠、輸出不變。
文檔:ARCHITECTURE §4.1(category 記憶化)+§6.5.1(orphan 葉塊預計算)。

新發現(待議、非本層):剩餘 el.matches() 之最大來源為 insidePill 之「逐文本節點沿祖先鏈 pillMatches」
(eachTextNode 每文本節點一趟、未記憶化)——可作下一項提速機會。
This commit is contained in:
2026-07-11 19:56:53 +08:00
parent 3fbb2904ef
commit 168e9e73f0
7 changed files with 138 additions and 6 deletions
+18
View File
@@ -63,6 +63,7 @@ export class Finder
private readonly levelTextSelector: string | null;
private readonly featureCache: WeakMap<Element, Record<string, boolean>>;
private readonly levelCache: WeakMap<Element, FeatureLevel>;
private readonly categoryCache: WeakMap<Element, InlineCategory>;
constructor(opts: ResolvedOptions)
{
@@ -79,6 +80,7 @@ export class Finder
this.includeSelector = opts.scope.include;
this.featureCache = new WeakMap();
this.levelCache = new WeakMap();
this.categoryCache = new WeakMap();
}
/** 該元素是否匹配 pill 選擇器(三分類之「整體」:code/kbd/行內公式等)。 */
@@ -116,8 +118,24 @@ export class Finder
* (自身排除最高)。**邏輯文本流之遍歷原語**collectRunsjiyaAdjacencyadjacentLogicalChar
* 之 isTransparent)皆據此**單一分類**分派;core/split 之 canSplit SPLITTABLE 白名單 ∩
* transparent(結構切分為獨立層,故更窄)。
*
* **記憶化**S3):分類含至多三次 el.matches()pillisolateavoid 選擇器),且被
* collectRunsjiyaAdjacencyjinzeorphansplit 各 pass 逐元素重算——以 categoryCache
* 記憶化砍除跨 pass 重複。安全性:單次 render 內一元素之分類**恆定**class/skip 屬性/
* nodeName 皆不因 render 變更;jz-* 包裹為新建元素、按 nodeName 分類亦恆定),且 Finder
* 隨 makeCtx 每次 render 重建 → 快取僅存活單次 render,無跨 render 失效問題(持久化屬 C1,
* 另議)。
*/
category(el: Element): InlineCategory
{
const cached = this.categoryCache.get(el);
if (cached) { return cached; }
const result = this.computeCategory(el);
this.categoryCache.set(el, result);
return result;
}
private computeCategory(el: Element): InlineCategory
{
if (this.avoidsSelf(el)) { return "avoid"; }
if (this.pillMatches(el)) { return "pill"; }
+20 -2
View File
@@ -144,6 +144,25 @@ export const orphanPass: StandalonePass = {
const biaodian = options.ruleset.biaodian;
const chars = options.orphan.chars;
// 「非葉塊」(含塊級後代之容器)預計算(S2):取代原逐塊 block.querySelector(sel) 掃整
// 棵子樹之 O(Σ塊子樹)(深塊嵌套下 O(n²))。一趟上攀標記——某塊為另一塊之最近塊祖先 ⟺
// 該塊含塊級後代。scope 無涉(與原 querySelector 同口徑:含域外後代塊亦算非葉),故據
// root 全塊集(querySelectorAll,非 eachBlock 之 in-scope 子集)計算。sel 亦外提出迴圈。
const sel = Array.from(blockSet).map((t) => t.toLowerCase()).join(",");
const nonLeaf = new Set<Element>();
if (sel)
{
ctx.root.querySelectorAll(sel).forEach((b) =>
{
let p = b.parentElement;
while (p)
{
if (blockSet.has(p.nodeName)) { nonLeaf.add(p); break; }
p = p.parentElement;
}
});
}
finder.eachBlock(ctx.root, (block) =>
{
// 段落級閘 per-node 功能閘 avoidisAvoided 組合判定:自身或祖先命中皆
@@ -152,8 +171,7 @@ export const orphanPass: StandalonePass = {
if (!finder.featureEnabledFor(block, PASS)) { return; }
if (finder.isAvoided(block)) { return; }
// 僅處理葉段落(無塊級後代):含巢狀塊之容器,其實義字屬內層段落,另行處理。
const sel = Array.from(blockSet).map((t) => t.toLowerCase()).join(",");
if (sel && block.querySelector(sel)) { return; }
if (nonLeaf.has(block)) { return; }
processBlock(block, finder, blockSet, anyCjk, biaodian, chars);
});
},