web-font/_prof_w2.entry.ts
崮生(子虚) 308df8ef4d perf(woff2): transformGlyfAndLoca 合并 y 解码与 triplet 编码为单循环
原 3 次遍历(x 解码→y 解码→triplet 编码)+ yCoords Int32Array 中转;
现 2 次遍历(x 解码→y 解码+triplet 编码合并)。

关键洞察:triplet delta 语义 = TTF delta(都是相对前一点的差),
所以解码 y 的同时用已存 xCoords[i] 配对编码 triplet,无需 yCoords 数组。
bbox_y 的 min/max 在合并循环同步计算,bbox bitmap 判定拆为 x/y 两段。

千字文 woff2 2.87ms→2.59ms(-9.6%),字节输出与 clean 版 cmp 完全一致,
SSIM 全部不变。删除 _reuseYCoords 模块级缓冲区。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-24 09:23:06 +08:00

19 lines
1.3 KiB
TypeScript

import { fontSubset } from './backend/font_util/font.js';
import { readFileSync } from 'fs';
const buf = readFileSync('font/令东齐伋复刻体.ttf');
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
const text = '天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔';
const g:any = globalThis;
const N=200;
fontSubset(ab, text, {sourceType:'ttf',outType:'woff2'}); // warmup
g.__t.transform=0; g.__t.brotli=0;
import('perf_hooks').then(({performance})=>{
const t0=performance.now();
for(let i=0;i<N;i++) fontSubset(ab, text, {sourceType:'ttf',outType:'woff2'});
const total=performance.now()-t0;
console.log(`woff2 total avg=${(total/N).toFixed(3)}ms`);
console.log(` transformGlyfAndLoca: ${(g.__t.transform/N).toFixed(3)}ms (${(g.__t.transform/total*100).toFixed(1)}%)`);
console.log(` brotli: ${(g.__t.brotli/N).toFixed(3)}ms (${(g.__t.brotli/total*100).toFixed(1)}%)`);
console.log(` 其余(Font.create+ttf write+组装): ${((total-g.__t.transform-g.__t.brotli)/N).toFixed(3)}ms (${((total-g.__t.transform-g.__t.brotli)/total*100).toFixed(1)}%)`);
});