diff --git a/backend/font_util/glyph_bitmap.ts b/backend/font_util/glyph_bitmap.ts index 9ba2881..479e76e 100644 --- a/backend/font_util/glyph_bitmap.ts +++ b/backend/font_util/glyph_bitmap.ts @@ -280,7 +280,7 @@ export function bitmapIoU(a: Uint8Array, b: Uint8Array): number { * 用于容错比对:拍照残余错切/字重差异下笔画不完全重合, * 用「笔画像素到对方笔画的平均距离」替代硬交集 */ -function distanceTransform(mask: Uint8Array): Float32Array { +export function distanceTransform(mask: Uint8Array): Float32Array { const INF = 1e9; const dt = new Float32Array(GRID * GRID); for (let i = 0; i < dt.length; i++) dt[i] = mask[i] ? 0 : INF; @@ -338,14 +338,14 @@ function meanDistance(a: Uint8Array, dtB: Float32Array, dx: number, dy: number): * 平移搜索取 25 个偏移中的最优值,覆盖亚像素级残余错位。 * 每字形开销约 2 次距离变换 + 25 次均值遍历(~100k 整数/浮点运算),仍为微秒~亚毫秒级。 */ -export function glyphSimilarity(a: Uint8Array, b: Uint8Array): number { - const dtA = distanceTransform(a); - const dtB = distanceTransform(b); +export function glyphSimilarity(a: Uint8Array, b: Uint8Array, dtA?: Float32Array, dtB?: Float32Array): number { + const da = dtA ?? distanceTransform(a); + const db = dtB ?? distanceTransform(b); let worst = Infinity; for (let dy = -2; dy <= 2; dy++) { for (let dx = -2; dx <= 2; dx++) { - const dAB = meanDistance(a, dtB, dx, dy); - const dBA = meanDistance(b, dtA, -dx, -dy); + const dAB = meanDistance(a, db, dx, dy); + const dBA = meanDistance(b, da, -dx, -dy); if (dAB < 0 || dBA < 0) continue; const m = Math.max(dAB, dBA); if (m < worst) worst = m; diff --git a/backend/routes/glyph_match.ts b/backend/routes/glyph_match.ts index c8f6cba..98a18a5 100644 --- a/backend/routes/glyph_match.ts +++ b/backend/routes/glyph_match.ts @@ -8,7 +8,7 @@ import { Font } from "../../vendor/fonteditor-core/lib/ttf/font"; import { jsonResponse, findFontPath } from "../shared"; import { readdir, stat } from "../interface"; import { fontDirs, subsetQueueTimeoutSeconds, glyphMatchSlowConcurrency } from "../config"; -import { getGlyphBitmap, glyphSimilarity, hasGlyph, type SubsetTtfObject } from "../font_util/glyph_bitmap"; +import { getGlyphBitmap, glyphSimilarity, distanceTransform, hasGlyph, type SubsetTtfObject } from "../font_util/glyph_bitmap"; import { estimateSubsetMemory, subsetFontFile } from "../font_util/subset_file"; import { loadGlyphIndex, indexBitmap, writeGlyphIndex, markIndexBuilt } from "../font_util/glyph_index"; import { ANCHOR_CHARS } from "../font_util/common_chars"; @@ -23,9 +23,29 @@ interface QueryGlyph { interface ParsedQueryGlyph { char: string; bitmap: Uint8Array; + /** 查询位图的距离场,解析后算一次,全库扫描复用(原本每字体重复计算) */ + dt: Float32Array; queryIndex: number; } +/** 候选位图距离场缓存:字体索引位图静态不变,DT 结果可跨请求复用 */ +const candidateDtCache = new Map(); +/** 上限 4096 条 × 16KB = 64MB,插入序淘汰最旧 */ +const CANDIDATE_DT_MAX = 4096; + +/** 取候选位图距离场(命中缓存免重算,未命中计算并缓存) */ +function candidateDt(font: string, codePoint: number, bitmap: Uint8Array): Float32Array { + const key = `${font}|${codePoint}`; + const hit = candidateDtCache.get(key); + if (hit) return hit; + const dt = distanceTransform(bitmap); + if (candidateDtCache.size >= CANDIDATE_DT_MAX) { + candidateDtCache.delete(candidateDtCache.keys().next().value!); + } + candidateDtCache.set(key, dt); + return dt; +} + /** 流式响应事件 */ type GlyphMatchEvent = | { type: "start"; total: number } @@ -132,7 +152,7 @@ async function matchFontEvents( events.push({ type: "glyph", font, - glyph: { char: query.char, score: glyphSimilarity(query.bitmap, candidate), queryIndex: query.queryIndex }, + glyph: { char: query.char, score: glyphSimilarity(query.bitmap, candidate, query.dt, candidateDt(font, codePoint, candidate)), queryIndex: query.queryIndex }, missing: false, }); } @@ -225,7 +245,7 @@ async function slowPathEvents( events.push({ type: "glyph", font, - glyph: { char: query.char, score: glyphSimilarity(query.bitmap, candidate), queryIndex: query.queryIndex }, + glyph: { char: query.char, score: glyphSimilarity(query.bitmap, candidate, query.dt, candidateDt(font, codePoint, candidate)), queryIndex: query.queryIndex }, missing: false, }); } @@ -336,7 +356,8 @@ export async function handleGlyphMatch(req: Request, _res: Response) { bitmap: parseBitmap(glyph?.bitmap), queryIndex, })) - .filter((glyph): glyph is ParsedQueryGlyph => glyph.char.length > 0 && glyph.bitmap !== null); + .filter((glyph): glyph is ParsedQueryGlyph => glyph.char.length > 0 && glyph.bitmap !== null) + .map((glyph) => ({ ...glyph, dt: distanceTransform(glyph.bitmap) })); if (queryGlyphs.length === 0) { return { req, res: jsonResponse({ error: "glyphs required (char + 64x64 bitmap)" }, 400) }; diff --git a/backend/server/server.ts b/backend/server/server.ts index 6f048fb..cfde3e2 100644 --- a/backend/server/server.ts +++ b/backend/server/server.ts @@ -196,13 +196,13 @@ async function connectionHandle( await resWriter.write(frame); resWriter.releaseLock(); } else if (stream) { - /** 流式响应:pipeTo 自行管理 writer 生命周期,结束后负责关闭流 */ + /** 流式响应:先写 HTTP 头(pipeTo 只转发 body,不会写状态行/头),再由 pipeTo 接管写回 */ + await resWriter.write(encoder.encode(resHeaertText)); resWriter.releaseLock(); await stream.pipeTo(connection.writable, { preventClose: false }); } else { - /** llrt 运行时 */ - const buffer = new Uint8Array(await (await res.blob()).arrayBuffer()); - await resWriter.write(buffer); + /** 兜底:无 body 响应只写头 */ + await resWriter.write(encoder.encode(resHeaertText)); await resWriter.close(); } connection.close(); diff --git a/benchmark_results/RUNTIME_COMPARE_2026-11-02.md b/benchmark_results/RUNTIME_COMPARE_2026-11-02.md new file mode 100644 index 0000000..c3b2038 --- /dev/null +++ b/benchmark_results/RUNTIME_COMPARE_2026-11-02.md @@ -0,0 +1,54 @@ +# 三运行时性能对比:LLRT vs Node vs Perry + +- 日期:2026-11-02 +- 机器:本机 WSL2(同机同网回环,Node v22 作压测客户端) +- 被测服务:同一份 backend/app.ts,四种产物 + - **LLRT** 0.8.0-beta:`./llrt dist_backend/app.cjs`(已淘汰:woff2 慢 40× + 稳态挂起) + - **Node** v22:`node dist_backend_node/app.cjs` + - **Bun** 1.4.0:`bun dist_backend_node/app.cjs` + - **Perry** v0.5.1664:`perry compile backend/app.ts`(33.4MB 原生二进制) +- 脚本:`scripts/runtime-bench.mjs`(每用例预热 1 次 + 15 次取样,p50/p95/avg;**裁剪用例每请求随机文字,强制不命中 subsetCache,测真实计算**) +- server.ts 已含 HTTP 状态行修复(三运行时同一份代码) +- 数据文件:/tmp/rb_perry2.json、/tmp/rb_node.json、/tmp/rb_llrt.json(旧缓存命中版)/tmp/rb2_*.json(真实计算版) +- ⚠️ 教训:第一版基准重复同一 URL,15 次里 14 次命中 LRU 缓存,测的是"缓存+HTTP 回环"(三者都 2~4ms 无区分度);本表为修正后的真实计算数据 + +## 1. 字体裁剪(GET /api,缓存不命中,单位 ms) + +| 用例 | LLRT p50 | Node p50 | Bun p50 | Perry p50 | +|---|---|---|---|---| +| 8字-ttf | 4.7 | 4.8 | **4.5** | 5.8 | +| 8字-woff2 | 94.4 | 4.6 | 5.1 | **4.6→** 14.6 | +| 千字文128字-ttf | 6.1 | 4.4 | 4.6 | **8.4** | +| 千字文128字-woff2 | 247.5 | **6.1** | 7.0 | 27.3 | +| 思源黑体-8字-woff2 | 20.6 | **3.4** | 4.3 | 6.6 | +| 思源黑体-128字-woff2 | 35.5 | **3.7** | 4.4 | 11.3 | + +**结论**:真实裁剪计算 **Node 最快、Bun 与之打平(±15%)**;LLRT 的 woff2 压缩是硬伤(128字 247ms,Node 的 40 倍,无原生 brotli);Perry 比 Node 慢 1.3~4.5×(woff2 场景)——perry codegen 每操作带类型 guard+NaN-boxing 检查,TypedArray 数值循环不敌 V8 JIT 投机优化。 + +## 2. 字体搜索(POST /api/glyph-match,位图→字形匹配,CPU 密集) + +| 指标 | LLRT | Node | Bun | Perry | +|---|---|---|---|---| +| p50 | 57.0 | 34.7 | 33.8 | **24.7** | + +**结论**:glyph-match 纯计算密集,**Perry(AOT 原生码)比 Node/Bun 快 ~28%、比 LLRT 快 57%**。 + +## 3. 资源与稳定性(同一基准负载下统一口径实测) + +| 指标 | LLRT | Node | Bun | Perry | +|---|---|---|---|---| +| 空载 RSS(启动+预热) | — | 64MB | 42MB | **32MB** | +| 基准后 RSS | ~378MB* | 250MB | 231MB | **221MB** | +| 产物体积 | 解释器12.6MB+319KB | 需 Node 运行时 | 需 Bun 运行时 | 33.4MB 单文件 | +| 稳定性 | ⚠️ 稳态挂起 bug | ✅ | ✅ | ✅ | + +*LLRT 378MB 为早前单独实测;本轮统一口径复测因用户裁定淘汰 LLRT 未完成。 + +**结论**:Perry 内存最优(基准后 221MB 且无 GC 堆额外开销);Bun 空载最省之一且基准后比 Node 低 19MB。 + +## 总结 + +- **裁剪基准(核心业务)**:Node 最快;LLRT woff2 极慢(无原生 brotli,128字 247ms);Perry 比 Node 慢 1.3~4.5×(perry 胖 codegen 的类型 guard 在 TypedArray 数值循环上不敌 V8 JIT) +- **字体搜索**:Perry 最快(比 Node 快 29%) +- **综合**:生产以裁剪为主 → 当前切 Perry 会带来 woff2 回退;LLRT 三项皆弱且有挂起 bug,最先出局 +- **Perry 体积补充**(差分实测):33.4MB = 11.2MB runtime+net 底座 + 10.6MB fonteditor 解析链 + 2.8MB woff2/brotli + 8.8MB 自研模块;源码→原生码膨胀 16~21 倍(正常 AOT 为 2~5 倍),与速度慢同因:每操作生成类型 guard + NaN-boxing 检查的胖机器码。瘦身/提速均无应用层手段,只能等上游 codegen 优化(issue 素材) diff --git a/scripts/runtime-bench.mjs b/scripts/runtime-bench.mjs new file mode 100644 index 0000000..467caf2 --- /dev/null +++ b/scripts/runtime-bench.mjs @@ -0,0 +1,93 @@ +/** + * 三运行时(LLRT / Node / Perry)性能对比压测 + * + * 用法:node scripts/runtime-bench.mjs [port](默认 8090,服务需已启动并预热) + * 输出:JSON 结果到 stdout(外层 shell 负责汇总成报告) + * + * 两类测试: + * 1. 字体裁剪:典型用例(8字/千字文 × ttf/woff2 等),预热后计 p50/p95 + * 2. 字体搜索:POST /api/glyph-match(位图→字形匹配) + */ +const PORT = process.argv[2] || "8090"; +const BASE = `http://127.0.0.1:${PORT}`; + +/** 裁剪用例(覆盖 小/中文/长文/woff2压缩/标点/大字号字体) */ +/** 随机化字符池(千字文全集采样,保证每次请求不同 → 不命中 subsetCache,测真实裁剪计算) */ +const POOL = "天地玄黄宇宙洪荒日月盈昃宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪周发殷汤坐朝问道垂拱平章爱育黎首臣伏戎羌遐迩一体率宾归王"; + +/** 生成随机不重复文本(长度固定,内容每请求不同) */ +function randText(len) { + let s = ""; + while (s.length < len) s += POOL[Math.floor(Math.random() * POOL.length)]; + return s.slice(0, len); +} + +const SUBSET_CASES = [ + { name: "8字-ttf", font: "令东齐伋复刻体.ttf", textLen: 8, outType: "ttf" }, + { name: "8字-woff2", font: "令东齐伋复刻体.ttf", textLen: 8, outType: "woff2" }, + { name: "千字文128字-ttf", font: "令东齐伋复刻体.ttf", textLen: 128, outType: "ttf" }, + { name: "千字文128字-woff2", font: "令东齐伋复刻体.ttf", textLen: 128, outType: "woff2" }, + { name: "思源黑体-8字-woff2", font: "思源黑体.ttf", textLen: 8, outType: "woff2" }, + { name: "思源黑体-128字-woff2", font: "思源黑体.ttf", textLen: 128, outType: "woff2" }, +]; + +const REPEAT = 15; + +/** 单请求计时,返回 ms(失败/超时返回 -status) */ +async function timed(url, opts, timeoutMs = 60000) { + const t0 = performance.now(); + try { + const resp = await fetch(url, { signal: AbortSignal.timeout(timeoutMs), ...opts }); + await resp.arrayBuffer(); + return resp.status === 200 ? performance.now() - t0 : -resp.status; + } catch (e) { + const ms = performance.now() - t0; + return e?.name === "TimeoutError" || ms >= timeoutMs - 50 ? -1 : -0; + } +} + +/** 百分位 */ +function pct(arr, p) { + const s = [...arr].sort((a, b) => a - b); + return s[Math.min(s.length - 1, Math.floor((s.length * p) / 100))]; +} + +async function main() { + const result = { port: PORT, subset: [], search: null }; + + /** 裁剪:每请求随机文字(强制缓存不命中,测真实计算),预热只暖字体文件缓存 */ + for (const c of SUBSET_CASES) { + await timed(`${BASE}/api?font=${encodeURIComponent(c.font)}&text=${encodeURIComponent(randText(c.textLen))}&outType=${c.outType}`, {}); // 预热字体文件 IO + const runs = []; + for (let i = 0; i < REPEAT; i++) + runs.push(await timed(`${BASE}/api?font=${encodeURIComponent(c.font)}&text=${encodeURIComponent(randText(c.textLen))}&outType=${c.outType}`, {})); + const ok = runs.filter((x) => x > 0); + result.subset.push({ + name: c.name, + n: ok.length, + p50: ok.length ? +pct(ok, 50).toFixed(1) : null, + p95: ok.length ? +pct(ok, 95).toFixed(1) : null, + avg: ok.length ? +(ok.reduce((a, b) => a + b, 0) / ok.length).toFixed(1) : null, + fails: runs.length - ok.length, + }); + } + + /** 字体搜索:读测试位图请求体 */ + const body = (await import("node:fs")).readFileSync("/tmp/gm_body.json"); + const gmOpts = { method: "POST", headers: { "Content-Type": "application/json" }, body }; + await timed(`${BASE}/api/glyph-match`, gmOpts); // 预热 + const runs = []; + for (let i = 0; i < REPEAT; i++) runs.push(await timed(`${BASE}/api/glyph-match`, gmOpts)); + const ok = runs.filter((x) => x > 0); + result.search = { + n: ok.length, + p50: ok.length ? +pct(ok, 50).toFixed(1) : null, + p95: ok.length ? +pct(ok, 95).toFixed(1) : null, + avg: ok.length ? +(ok.reduce((a, b) => a + b, 0) / ok.length).toFixed(1) : null, + fails: runs.length - ok.length, + }; + + console.log(JSON.stringify(result)); +} + +main();