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

与 collectSubrRefs 同构的字节分派,operand(b0>=32)提前到首判定。思源千字文
subsetCFF 0.4920→0.4819ms(-2.1%),CFF 输出 sha 一致。

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

View File

@ -361,12 +361,35 @@ export function rewriteCharstring(
let p = start;
while (p < end) {
const b0 = b[p++];
if (b0 === 255) {
lastStart = wp; lastVal = NaN;
out[wp] = 255; out[wp + 1] = b[p]; out[wp + 2] = b[p + 1]; out[wp + 3] = b[p + 2]; out[wp + 4] = b[p + 3];
wp += 5;
p += 4;
stackLen++;
/** 分支按热度重排operandb0>=32占绝大多数字节首判定与 collectSubrRefs 同构)。 */
if (b0 >= 32) {
if (b0 <= 246) {
/** 单字节 operand最热 */
lastStart = wp;
lastVal = b0 - 139;
out[wp++] = b0;
stackLen++;
} else if (b0 === 255) {
lastStart = wp; lastVal = NaN;
out[wp] = 255; out[wp + 1] = b[p]; out[wp + 2] = b[p + 1]; out[wp + 3] = b[p + 2]; out[wp + 4] = b[p + 3];
wp += 5;
p += 4;
stackLen++;
} else if (b0 <= 250) {
lastStart = wp;
lastVal = (b0 - 247) * 256 + b[p] + 108;
out[wp] = b0; out[wp + 1] = b[p];
wp += 2;
p += 1;
stackLen++;
} else {
lastStart = wp;
lastVal = -(b0 - 251) * 256 - b[p] - 108;
out[wp] = b0; out[wp + 1] = b[p];
wp += 2;
p += 1;
stackLen++;
}
} else if (b0 === 28) {
lastStart = wp;
lastVal = ((b[p] << 24) | (b[p + 1] << 16)) >> 16;
@ -381,25 +404,6 @@ export function rewriteCharstring(
wp += 5;
p += 4;
stackLen++;
} else if (b0 >= 32 && b0 <= 246) {
lastStart = wp;
lastVal = b0 - 139;
out[wp++] = b0;
stackLen++;
} else if (b0 >= 247 && b0 <= 250) {
lastStart = wp;
lastVal = (b0 - 247) * 256 + b[p] + 108;
out[wp] = b0; out[wp + 1] = b[p];
wp += 2;
p += 1;
stackLen++;
} else if (b0 >= 251 && b0 <= 254) {
lastStart = wp;
lastVal = -(b0 - 251) * 256 - b[p] - 108;
out[wp] = b0; out[wp + 1] = b[p];
wp += 2;
p += 1;
stackLen++;
} else {
/** 操作码 */
if (b0 === 12) {