web-font/_prof_woff2.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

34 lines
1.6 KiB
TypeScript

import { performance } from 'perf_hooks';
import { readFileSync } from 'fs';
import * as zlib from 'zlib';
const woff2enc = require('./vendor/fonteditor-core/woff2/woff2-encode.js');
const mod = woff2enc.__esModule ? woff2enc.default || woff2enc : woff2enc;
const tTransform = {total:0};
const tBrotli = {total:0};
// patch transformGlyfAndLoca
const origTransform = mod.transformGlyfAndLoca;
mod.transformGlyfAndLoca = function(...args:any[]) {
const t=performance.now();
const r = origTransform.apply(this,args);
tTransform.total += performance.now()-t;
return r;
};
// patch brotliCompressSync at module level not possible (local const), instead time encodeTTFToWOFF2 minus transform
const { fontSubset } = require('./backend/font_util/font.js');
const buf = readFileSync('font/令东齐伋复刻体.ttf');
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
const text = '天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔';
const N=200;
// warmup
fontSubset(ab, text, {sourceType:'ttf',outType:'woff2'});
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: ${(tTransform.total/N).toFixed(3)}ms (${(tTransform.total/total*100).toFixed(1)}%)`);
console.log(` 其余(brotli+组装): ${((total-tTransform.total)/N).toFixed(3)}ms (${((total-tTransform.total)/total*100).toFixed(1)}%)`);