perf(cff): collectSubrRefs 分支按热度重排

operand(b0>=32)占 CJK charstring 绝大多数字节,提前到首判定。旧顺序按字节值
升序(255→28→29→32-246),最热的 32-246 单字节 operand 要先 3 次比较不命中才进入;
现 b0>=32 一次比较命中热路径。

思源千字文 subsetCFF 0.5137→0.4884ms(-4.9%),CFF 输出 sha 一致(纯分支顺序调整)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
崮生(子虚) 2026-07-25 07:25:37 +08:00
parent d2b3b1939f
commit ca34a9e648

View File

@ -243,11 +243,32 @@ export function collectSubrRefs(
let hasSubr = false;
while (p < end) {
const b0 = b[p++];
if (b0 === 255) {
/** fixed point坐标4 字节,不入栈编号判定 */
lastVal = NaN;
stackLen++;
p += 4;
/**
* operandb0>=32/ CJK charstring
* 255282932-246 32-246 operand 3
* b0>=32 operand <32 走操作码分支
*/
if (b0 >= 32) {
if (b0 <= 246) {
/** 单字节 operand值 -107~107最热 */
lastVal = b0 - 139;
stackLen++;
} else if (b0 === 255) {
/** fixed point坐标4 字节,不入栈编号判定 */
lastVal = NaN;
stackLen++;
p += 4;
} else if (b0 <= 250) {
/** 2 字节正 operand247~250 */
lastVal = (b0 - 247) * 256 + b[p] + 108;
stackLen++;
p += 1;
} else {
/** 2 字节负 operand251~254 */
lastVal = -(b0 - 251) * 256 - b[p] - 108;
stackLen++;
p += 1;
}
} else if (b0 === 28) {
lastVal = ((b[p] << 24) | (b[p + 1] << 16)) >> 16;
stackLen++;
@ -256,17 +277,6 @@ export function collectSubrRefs(
lastVal = ((b[p] << 24) | (b[p + 1] << 16) | (b[p + 2] << 8) | b[p + 3]) | 0;
stackLen++;
p += 4;
} else if (b0 >= 32 && b0 <= 246) {
lastVal = b0 - 139;
stackLen++;
} else if (b0 >= 247 && b0 <= 250) {
lastVal = (b0 - 247) * 256 + b[p] + 108;
stackLen++;
p += 1;
} else if (b0 >= 251 && b0 <= 254) {
lastVal = -(b0 - 251) * 256 - b[p] - 108;
stackLen++;
p += 1;
} else {
/** 操作码b0 <= 27含 12 双字节) */
if (b0 === 12) {