Browse Source

Perf: speedup cache creation in StateBlock

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
a759af6f8a
  1. 76
      lib/rules_block/state_block.js

76
lib/rules_block/state_block.js

@ -6,10 +6,6 @@
function State(src, parser, tokens, options, env) {
var ch, s, start, pos, len, indent, indent_found;
// TODO: check if we can move string replaces to parser, to avoid
// unnesessary call on shadow clone creation. Or check if we can do
// cloning more effectively. Profile first.
// Prepare string to parse:
//
// - replace tabs with spaces
@ -20,7 +16,6 @@ function State(src, parser, tokens, options, env) {
// Shortcuts to simplify nested calls
this.parser = parser;
// TODO: (?) set directly for faster access.
this.options = options;
this.env = env;
@ -31,10 +26,29 @@ function State(src, parser, tokens, options, env) {
this.tokens = tokens;
this.bMarks = []; // line begin offsets for fast jumps
this.eMarks = []; // line end offsets for fast jumps
this.tShift = []; // indent for each line
this.bMarks = []; // line begin offsets for fast jumps
this.eMarks = []; // line end offsets for fast jumps
this.tShift = []; // indent for each line
this.bqMarks = []; // lines shifts in blockquotes (calculated on bq enter)
// inline parser variables
this.pos = 0; // char index in src
// block parser variables
this.blkLevel = 0;
this.blkIndent = 0;
this.line = 0; // line index in src
this.lineMax = 0; // lines count
this.tight = false; // loose/tight mode for lists
this.listMode = false; // if true, block parser stops on two newlines
this.bqLevel = 0; // blockquote nesting level
this.level = 0;
// renderer
this.result = '';
// Create caches
// Generate markers.
s = this.src;
indent = 0;
@ -43,26 +57,27 @@ function State(src, parser, tokens, options, env) {
for (start = pos = indent = 0, len = s.length; pos < len; pos++) {
ch = s.charCodeAt(pos);
// TODO: check other spaces and tabs too or keep existing regexp replace ??
if (!indent_found && ch === 0x20/* space */) {
indent++;
}
if (!indent_found && ch !== 0x20/* space */) {
this.tShift.push(indent);
indent_found = true;
if (!indent_found) {
if (ch === 0x20/* space */) {
indent++;
continue;
} else {
this.tShift.push(indent);
indent_found = true;
}
}
if (ch === 0x0D || ch === 0x0A) {
if (ch === 0x0A || ch === 0x0D) {
this.bMarks.push(start);
this.eMarks.push(pos);
indent_found = false;
indent = 0;
start = pos + 1;
}
if (ch === 0x0D && pos < len && s.charCodeAt(pos) === 0x0A) {
pos++;
start++;
if (ch === 0x0D && pos + 1 < len && s.charCodeAt(pos + 1) === 0x0A) {
pos++;
start++;
}
}
}
if (ch !== 0x0D || ch !== 0x0A) {
@ -76,28 +91,11 @@ function State(src, parser, tokens, options, env) {
this.eMarks.push(s.length);
this.tShift.push(0);
// inline parser variables
this.pos = 0; // char index in src
this.lineMax = this.bMarks.length - 1; // don't count last fake line
// block parser variables
this.blkLevel = 0;
this.blkIndent = 0;
this.line = 0; // line index in src
this.lineMax = this.bMarks.length - 1; // don't count last fake line
this.tight = false; // loose/tight mode for lists
this.listMode = false; // if true, block parser stops on two newlines
// Stuff for blockquotes
this.bqLevel = 0;
this.bqMarks = [];
for (start = 0; start < this.bMarks.length; start++) {
this.bqMarks.push(0);
}
this.level = 0;
// renderer
this.result = '';
}

Loading…
Cancel
Save