From 6d7d6cf150dedb53b7f0972b79313df3364ebbed Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 7 Sep 2014 15:20:41 -0700 Subject: stmd.js: Added memoization of inline parsing. --- js/stmd.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/stmd.js b/js/stmd.js index 15d7345..63234f6 100755 --- a/js/stmd.js +++ b/js/stmd.js @@ -672,6 +672,13 @@ var parseReference = function(s, refmap) { // Parse the next inline element in subject, advancing subject position // and adding the result to 'inlines'. var parseInline = function(inlines) { + var startpos = this.pos; + var memoized = this.memo[startpos]; + if (memoized) { + inlines.push(memoized.inlines); + this.pos += memoized.len; + return memoized.len; + } var c = this.peek(); var res; switch(c) { @@ -703,7 +710,13 @@ var parseInline = function(inlines) { break; default: } - return res || this.parseString(inlines); + if (!res) { + res = this.parseString(inlines); + } + if (res > 0) { + this.memo[startpos] = { inlines: inlines[inlines.length - 1], len: res }; + } + return res; }; // Parse s as a list of inlines, using refmap to resolve references. @@ -711,6 +724,7 @@ var parseInlines = function(s, refmap) { this.subject = s; this.pos = 0; this.refmap = refmap || {}; + this.memo = {}; var inlines = []; while (this.parseInline(inlines)) ; return inlines; @@ -723,6 +737,7 @@ function InlineParser(){ label_nest_level: 0, // used by parseLinkLabel method pos: 0, refmap: {}, + memo: {}, match: match, peek: peek, spnl: spnl, -- cgit v1.2.3