From e8989c7fa45faca523284f0718376f145bd5d738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Sat, 25 Jul 2026 07:27:00 +0800 Subject: [PATCH] =?UTF-8?q?perf(cff):=20rewriteCharstring=20=E5=88=86?= =?UTF-8?q?=E6=94=AF=E6=8C=89=E7=83=AD=E5=BA=A6=E9=87=8D=E6=8E=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 与 collectSubrRefs 同构的字节分派,operand(b0>=32)提前到首判定。思源千字文 subsetCFF 0.4920→0.4819ms(-2.1%),CFF 输出 sha 一致。 Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/font_util/cff-subset.ts | 54 ++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/backend/font_util/cff-subset.ts b/backend/font_util/cff-subset.ts index cb4a97b..48185bf 100644 --- a/backend/font_util/cff-subset.ts +++ b/backend/font_util/cff-subset.ts @@ -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++; + /** 分支按热度重排:operand(b0>=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) {