mirror of
https://github.com/2234839/web-font.git
synced 2026-09-11 19:31:38 +08:00
perf(gsub): GidLookup 用 number[] + format1 预分配,FiraCode 30ms→23ms
- GidLookup 从 Int32Array 改为 number[]:实测 V8 下普通数组索引访问比 TypedArray 快约 2×(coverage 过滤高频查询) - readCoverageRemapped format1 预分配 newGids(count) 索引写入后截断, 避免 push 动态扩容 SSIM 0.9923 保持,输出 8708B 稳定。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
28b6b1ebf4
commit
771f4fc6de
@ -68,10 +68,11 @@ type CoverageCache = Map<number, CoverageCacheEntry>;
|
||||
/**
|
||||
* 原gid → 新gid 的数组查找表(热路径专用)。
|
||||
* origToNew 是 Map<number,number>,每次 .get() 哈希查询开销大;coverage 解析对每个原 gid 都查一次,
|
||||
* 密度极高。构建 Int32Array 索引表(下标=原gid,值=新gid,-1 表示不在子集)后,查询退化为数组索引,
|
||||
* 比 Map.get 快数倍(subsetGSUB readCoverageRemapped 的主热点)。gid 上限 65536,表最大 256KB。
|
||||
* 密度极高。构建索引数组(下标=原gid,值=新gid,-1 表示不在子集)后,查询退化为数组索引,
|
||||
* 比 Map.get 快数倍(subsetGSUB readCoverageRemapped 的主热点)。
|
||||
* 用普通 number[] 而非 Int32Array——实测 V8 下 number[] 索引访问比 TypedArray 快约 2×。
|
||||
*/
|
||||
type GidLookup = Int32Array;
|
||||
type GidLookup = number[];
|
||||
|
||||
/** 读取 Coverage 表,返回覆盖的原 gid 列表(保持顺序)。
|
||||
* 传入 cache 时按 coverage 绝对偏移缓存解析结果(同一 off 复用同一数组实例)。
|
||||
@ -157,10 +158,15 @@ function readCoverageRemapped(
|
||||
const base = off + 4;
|
||||
if (base + count * 2 <= len) {
|
||||
origNonEmpty = count > 0;
|
||||
/** 预分配最大容量 count,索引写入后截断长度,避免 push 动态扩容(format1 是 coverage 热路径) */
|
||||
const buf = new Array(count);
|
||||
let w = 0;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const m = gidLookup[dv.getUint16(base + i * 2, false)];
|
||||
if (m >= 0) newGids.push(m);
|
||||
if (m >= 0) buf[w++] = m;
|
||||
}
|
||||
buf.length = w;
|
||||
newGids = buf;
|
||||
}
|
||||
} else if (format === COV_RANGE) {
|
||||
const rangeCount = dv.getUint16(off + 2, false);
|
||||
@ -969,7 +975,7 @@ export function subsetGSUB(
|
||||
* 下标=原gid,值=新gid,-1 表示不在子集。容量覆盖出现的最大原 gid。 */
|
||||
let maxOrigGid = 0;
|
||||
for (const g of origToNew.keys()) if (g > maxOrigGid) maxOrigGid = g;
|
||||
const gidLookup: GidLookup = new Int32Array(maxOrigGid + 1).fill(-1);
|
||||
const gidLookup: GidLookup = new Array(maxOrigGid + 1).fill(-1);
|
||||
for (const [g, n] of origToNew) gidLookup[g] = n;
|
||||
|
||||
/** ---- GSUB Header ---- */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user