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) { function State(src, parser, tokens, options, env) {
var ch, s, start, pos, len, indent, indent_found; 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: // Prepare string to parse:
// //
// - replace tabs with spaces // - replace tabs with spaces
@ -20,7 +16,6 @@ function State(src, parser, tokens, options, env) {
// Shortcuts to simplify nested calls // Shortcuts to simplify nested calls
this.parser = parser; this.parser = parser;
// TODO: (?) set directly for faster access.
this.options = options; this.options = options;
this.env = env; this.env = env;
@ -31,10 +26,29 @@ function State(src, parser, tokens, options, env) {
this.tokens = tokens; this.tokens = tokens;
this.bMarks = []; // line begin offsets for fast jumps this.bMarks = []; // line begin offsets for fast jumps
this.eMarks = []; // line end offsets for fast jumps this.eMarks = []; // line end offsets for fast jumps
this.tShift = []; // indent for each line 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. // Generate markers.
s = this.src; s = this.src;
indent = 0; indent = 0;
@ -43,26 +57,27 @@ function State(src, parser, tokens, options, env) {
for (start = pos = indent = 0, len = s.length; pos < len; pos++) { for (start = pos = indent = 0, len = s.length; pos < len; pos++) {
ch = s.charCodeAt(pos); ch = s.charCodeAt(pos);
// TODO: check other spaces and tabs too or keep existing regexp replace ?? if (!indent_found) {
if (!indent_found && ch === 0x20/* space */) { if (ch === 0x20/* space */) {
indent++; indent++;
} continue;
if (!indent_found && ch !== 0x20/* space */) { } else {
this.tShift.push(indent); this.tShift.push(indent);
indent_found = true; indent_found = true;
}
} }
if (ch === 0x0A || ch === 0x0D) {
if (ch === 0x0D || ch === 0x0A) {
this.bMarks.push(start); this.bMarks.push(start);
this.eMarks.push(pos); this.eMarks.push(pos);
indent_found = false; indent_found = false;
indent = 0; indent = 0;
start = pos + 1; start = pos + 1;
}
if (ch === 0x0D && pos < len && s.charCodeAt(pos) === 0x0A) { if (ch === 0x0D && pos + 1 < len && s.charCodeAt(pos + 1) === 0x0A) {
pos++; pos++;
start++; start++;
}
} }
} }
if (ch !== 0x0D || ch !== 0x0A) { if (ch !== 0x0D || ch !== 0x0A) {
@ -76,28 +91,11 @@ function State(src, parser, tokens, options, env) {
this.eMarks.push(s.length); this.eMarks.push(s.length);
this.tShift.push(0); this.tShift.push(0);
// inline parser variables this.lineMax = this.bMarks.length - 1; // don't count last fake line
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 = 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++) { for (start = 0; start < this.bMarks.length; start++) {
this.bqMarks.push(0); this.bqMarks.push(0);
} }
this.level = 0;
// renderer
this.result = '';
} }

Loading…
Cancel
Save