几个无关紧要的优化

1 format页用上了分离的size
2 parser不使用charcode而是char本身
3 去掉access的检测,反正也是基于异常处理的
This commit is contained in:
a92126 2024-10-14 09:57:01 +08:00
parent effc264d08
commit 491141bf94

View File

@ -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;