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;