mirror of
https://github.com/2234839/web-font.git
synced 2026-12-04 04:20:54 +08:00
perf(otf): calcTableChecksum 改 DataView.getUint32 批量读,assemble -83%
原逐字节 bytes[i]<<24|bytes[i+1]<<16|... 手工拼装 uint32,对思源 742KB CFF 表是 assembleSfnt 主要开销(assembleSfnt min 0.643ms 中绝大部分)。 改 DataView.getUint32 一次读 4 字节大端 uint32(V8 对 DataView 专门内联, [[dataview-getuint32-fastest]] 验证其快于 Uint32Array+swap 与手工位拼装), 尾部 1~3 字节补 0 单独处理保持规范语义。 assembleSfnt(思源8字): min 0.643→0.110ms(-83%); subsetOTF: min 1.321→0.725ms(-45%)、p50 1.720→1.079ms(-37%); (叠加上一 commit wholeSum 复用,相对原版 min 1.836→0.725 累计 -60%) 字节级零回归(cmp 优化前后输出逐字节一致,含 checksum/checkSumAdjustment); 基准测试全字体 SSIM 与优化前完全一致。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
98b56b4f35
commit
7cd960617a
@ -360,17 +360,23 @@ function passthroughHead(srcDv: DataView, off: number, len: number): Uint8Array
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 计算表 4 字节对齐后的 checksum(OpenType 规范:表长度按 4 字节边界补齐算 sum) */
|
||||
/** 计算表 4 字节对齐后的 checksum(OpenType 规范:表长度按 4 字节边界补齐算 sum)。
|
||||
* DataView.getUint32 批量读大端 uint32(V8 对 DataView 有专门内联,[[dataview-getuint32-fastest]]
|
||||
* 验证其快于 Uint32Array+swap 与逐字节位拼装),尾部 1~3 字节补 0 单独处理。 */
|
||||
function calcTableChecksum(bytes: Uint8Array): number {
|
||||
const n = bytes.length;
|
||||
const padded = (n + 3) & ~3;
|
||||
const dv = new DataView(bytes.buffer, bytes.byteOffset, n);
|
||||
const fullQuads = n >>> 2;
|
||||
let sum = 0;
|
||||
for (let i = 0; i < padded; i += 4) {
|
||||
const b0 = bytes[i] || 0;
|
||||
const b1 = bytes[i + 1] || 0;
|
||||
const b2 = bytes[i + 2] || 0;
|
||||
const b3 = bytes[i + 3] || 0;
|
||||
sum = (sum + ((b0 << 24) | (b1 << 16) | (b2 << 8) | b3)) >>> 0;
|
||||
for (let i = 0; i < fullQuads; i++) sum = (sum + dv.getUint32(i * 4, false)) >>> 0;
|
||||
/** 尾部 1~3 字节(n 非 4 倍数时),按规范右侧补 0 到 4 字节参与累加 */
|
||||
const tail = n & 3;
|
||||
if (tail) {
|
||||
const base = fullQuads * 4;
|
||||
let last = 0;
|
||||
for (let j = 0; j < tail; j++) last = (last << 8) | bytes[base + j];
|
||||
last = (last << ((4 - tail) * 8)) >>> 0;
|
||||
sum = (sum + last) >>> 0;
|
||||
}
|
||||
return sum >>> 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user