aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-09-07 15:20:41 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2014-09-11 11:17:39 -0700
commit6d7d6cf150dedb53b7f0972b79313df3364ebbed (patch)
tree2e6e664b6320170a7033557248ab72f548d0fb51
parent23e31a709cb390770bcdee8808ca4265707e7722 (diff)
stmd.js: Added memoization of inline parsing.
-rwxr-xr-xjs/stmd.js17
1 files changed, 16 insertions, 1 deletions
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,