optimize rule lookup

we have tons of bbcode rules, might as well speed them up
This commit is contained in:
Sam 2017-06-30 15:19:07 -04:00
parent 0ba39109a0
commit 0650c8dbab
3 changed files with 23 additions and 23 deletions

View File

@ -48,8 +48,24 @@ class Ruler {
return this.rules; return this.rules;
} }
getRuleForTag(tag) {
this.ensureCache();
return this.cache[tag];
}
ensureCache() {
if (this.cache) { return; }
this.cache = {};
for(let i=this.rules.length-1;i>=0;i--) {
let info = this.rules[i];
this.cache[info.rule.tag] = info;
}
}
push(name, rule) { push(name, rule) {
this.rules.push({name, rule}); this.rules.push({name, rule});
this.cache = null;
} }
} }

View File

@ -111,7 +111,7 @@ export function parseBBCodeTag(src, start, max, multiline) {
function applyBBCode(state, startLine, endLine, silent, md) { function applyBBCode(state, startLine, endLine, silent, md) {
var i, nextLine, var nextLine,
old_parent, old_line_max, rule, old_parent, old_line_max, rule,
start = state.bMarks[startLine] + state.tShift[startLine], start = state.bMarks[startLine] + state.tShift[startLine],
initial = start, initial = start,
@ -126,18 +126,10 @@ function applyBBCode(state, startLine, endLine, silent, md) {
return false; return false;
} }
let rules = md.block.bbcode_ruler.getRules(); let ruleInfo = md.block.bbcode_ruler.getRuleForTag(info.tag);
if (!ruleInfo) { return false; }
for(i=0;i<rules.length;i++) { rule = ruleInfo.rule;
let r = rules[i].rule;
if (r.tag === info.tag) {
rule = r;
break;
}
}
if (!rule) { return false; }
// Since start is found, we can report success here in validation mode // Since start is found, we can report success here in validation mode
if (silent) { return true; } if (silent) { return true; }

View File

@ -15,20 +15,12 @@ function tokanizeBBCode(state, silent, ruler) {
return false; return false;
} }
let rules = ruler.getRules();
let rule, i; let rule, i;
for (i=0; i<rules.length; i++) { let ruleInfo = ruler.getRuleForTag(tagInfo.tag);
let r = rules[i].rule; if (!ruleInfo) { return false; }
if (r.tag === tagInfo.tag) { rule = ruleInfo.rule;
rule = r;
break;
}
}
if (!rule) {
return false;
}
if (rule.replace) { if (rule.replace) {
// special handling for replace // special handling for replace