diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-09-07 15:20:41 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-09-11 11:17:39 -0700 |
commit | 6d7d6cf150dedb53b7f0972b79313df3364ebbed (patch) | |
tree | 2e6e664b6320170a7033557248ab72f548d0fb51 | |
parent | 23e31a709cb390770bcdee8808ca4265707e7722 (diff) |
stmd.js: Added memoization of inline parsing.
-rwxr-xr-x | js/stmd.js | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -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, |