From 491141bf94ea4a2d17bde08d5a2503f1db85ff0e Mon Sep 17 00:00:00 2001 From: a92126 <182859762@qq.com> Date: Mon, 14 Oct 2024 09:57:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=A0=E4=B8=AA=E6=97=A0=E5=85=B3=E7=B4=A7?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E4=BC=98=E5=8C=96=201=20format=E9=A1=B5?= =?UTF-8?q?=E7=94=A8=E4=B8=8A=E4=BA=86=E5=88=86=E7=A6=BB=E7=9A=84size=202?= =?UTF-8?q?=20parser=E4=B8=8D=E4=BD=BF=E7=94=A8charcode=E8=80=8C=E6=98=AFc?= =?UTF-8?q?har=E6=9C=AC=E8=BA=AB=203=20=E5=8E=BB=E6=8E=89access=E7=9A=84?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=EF=BC=8C=E5=8F=8D=E6=AD=A3=E4=B9=9F=E6=98=AF?= =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/cheat-builder/cht.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tools/cheat-builder/cht.js b/tools/cheat-builder/cht.js index ea98d2c..6dc8abc 100644 --- a/tools/cheat-builder/cht.js +++ b/tools/cheat-builder/cht.js @@ -18,7 +18,18 @@ const nine = "9".charCodeAt(); const a = "a".charCodeAt(); const f = "f".charCodeAt(); const conv = new TextEncoder(); +const fastly = process.env.OAFCHT_NO_REUSE==='1'; +/** + * 这个函数做了一些数据复用处理,能省下来一点文件容量 + * + * 具体来说,字符串表里面,如果有两个字符串'1'和'11' + * 那么在字符串表里面,只会有一个索引指向'11',而'1'会 + * 复用'11'的数据,方案是索引的位置放到'11'的最后一个1前面 + * + * 当然,这样做有点吃cpu,如果不希望使用这个方式,可以配置 + * 环境变量 OAFCHT_NO_REUSE=1 将会产生一个更快的过程 + */ const findIndex = (t, tr, tlen) => { const rlen = tr.length, limit = tlen - rlen; for (let i = 0; i < limit; ++i) { @@ -183,7 +194,8 @@ const read = (cht, info, order) => { } }; - /*return new Promise( ok => { + /** 注释掉的这个代码是用流的方式处理,但是比不过readFileSync的速度 + return new Promise( ok => { cht.on("data", data => { const size = data.length; for( let i=0; i < size; ++i ) { @@ -196,7 +208,8 @@ const read = (cht, info, order) => { }); });*/ const size = cht.length; - for(let i=0; i < size; ++i ) incr( cht.charAt(i) ); + for(let i=0; i < size; ++i ) // 这样比for(let c of cht)要快! + incr( cht.charAt(i) ); incr(""); return retval; } @@ -307,7 +320,7 @@ const lade = (list, info, order) => { let strtable = new Uint8Array(1024); // 保存字符串常量 let idxtable = new Uint16Array(128); // 从字符串常量索引出来的字符串列表 let cmdtable = new Uint32Array(4096); // 保存选项对应指令 - const size = {"str": 0, "cmd": 0, idx: 0}; + const size = {"str": 0, "cmd": 0, "idx": 0}; const addString = list => { const ut_pushtr = tr => { const off = size.str; @@ -324,7 +337,18 @@ const lade = (list, info, order) => { size.str = len; return off; }; - const ut_addtr = tr => { + const ut_addtr = fastly ? (() => { + const cache = {}; + return tr => {// 不要直接ut_pushtr,因为有些秘籍会撑爆字符串表 + if (cache[tr] != undefined) return cache[tr]; + else { + const code = conv.encode(tr); + const off = findIndex(strtable, code, size.str); + if( off >= 0 ) cache[tr] = off; + return off < 0 ? ut_pushtr(code) : off; + } + } + })() : tr => { const code = conv.encode(tr); const off = findIndex(strtable, code, size.str); return off < 0 ? ut_pushtr(code) : off;