1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 15:30:47 +00:00

Re-establish caching of results of parsing a tiddler

I switched this optimisation off back in
ed35d91be6, in October 2013, as part of a
big refactoring of the parsing and widget mechanism. I’ve been meaning
to switch it back on for some time.

My rough measurements suggest that this optimisation can reduce
rendering time by 5-10%.
This commit is contained in:
Jermolene 2015-07-05 17:48:18 +01:00
parent c1005b9d12
commit b0cb17cd83
3 changed files with 35 additions and 63 deletions

View File

@ -78,10 +78,13 @@ exports.parse = function() {
} }
// Save the macro definition // Save the macro definition
return [{ return [{
type: "macrodef", type: "set",
name: this.match[1], attributes: {
params: params, name: {type: "string", value: this.match[1]},
text: text value: {type: "string", value: text}
},
children: [],
params: params
}]; }];
}; };

View File

@ -48,12 +48,13 @@ var WikiParser = function(type,text,options) {
this.blockRules = this.instantiateRules(this.blockRuleClasses,"block",0); this.blockRules = this.instantiateRules(this.blockRuleClasses,"block",0);
this.inlineRules = this.instantiateRules(this.inlineRuleClasses,"inline",0); this.inlineRules = this.instantiateRules(this.inlineRuleClasses,"inline",0);
// Parse any pragmas // Parse any pragmas
this.tree = this.parsePragmas(); this.tree = [];
var topBranch = this.parsePragmas();
// Parse the text into inline runs or blocks // Parse the text into inline runs or blocks
if(options.parseAsInline) { if(options.parseAsInline) {
this.tree.push.apply(this.tree,this.parseInlineRun()); topBranch.push.apply(topBranch,this.parseInlineRun());
} else { } else {
this.tree.push.apply(this.tree,this.parseBlocks()); topBranch.push.apply(topBranch,this.parseBlocks());
} }
// Return the parse tree // Return the parse tree
}; };
@ -122,7 +123,7 @@ WikiParser.prototype.findNextMatch = function(rules,startPos) {
Parse any pragmas at the beginning of a block of parse text Parse any pragmas at the beginning of a block of parse text
*/ */
WikiParser.prototype.parsePragmas = function() { WikiParser.prototype.parsePragmas = function() {
var tree = []; var currentTreeBranch = this.tree;
while(true) { while(true) {
// Skip whitespace // Skip whitespace
this.skipWhitespace(); this.skipWhitespace();
@ -137,9 +138,15 @@ WikiParser.prototype.parsePragmas = function() {
break; break;
} }
// Process the pragma rule // Process the pragma rule
tree.push.apply(tree,nextMatch.rule.parse()); var subTree = nextMatch.rule.parse();
if(subTree.length > 0) {
// Quick hack; we only cope with a single parse tree node being returned, which is true at the moment
currentTreeBranch.push.apply(currentTreeBranch,subTree);
subTree[0].children = [];
currentTreeBranch = subTree[0].children;
}
} }
return tree; return currentTreeBranch;
}; };
/* /*

View File

@ -705,22 +705,18 @@ exports.clearGlobalCache = function() {
// Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it // Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it
exports.getCacheForTiddler = function(title,cacheName,initializer) { exports.getCacheForTiddler = function(title,cacheName,initializer) {
this.caches = this.caches || Object.create(null);
// Temporarily disable caching so that tweakParseTreeNode() works var caches = this.caches[title];
return initializer(); if(caches && caches[cacheName]) {
return caches[cacheName];
// this.caches = this.caches || Object.create(null); } else {
// var caches = this.caches[title]; if(!caches) {
// if(caches && caches[cacheName]) { caches = Object.create(null);
// return caches[cacheName]; this.caches[title] = caches;
// } else { }
// if(!caches) { caches[cacheName] = initializer();
// caches = Object.create(null); return caches[cacheName];
// this.caches[title] = caches; }
// }
// caches[cacheName] = initializer();
// return caches[cacheName];
// }
}; };
// Clear all caches associated with a particular tiddler // Clear all caches associated with a particular tiddler
@ -753,7 +749,7 @@ Options include:
parseAsInline: if true, the text of the tiddler will be parsed as an inline run parseAsInline: if true, the text of the tiddler will be parsed as an inline run
_canonical_uri: optional string of the canonical URI of this content _canonical_uri: optional string of the canonical URI of this content
*/ */
exports.old_parseText = function(type,text,options) { exports.parseText = function(type,text,options) {
options = options || {}; options = options || {};
// Select a parser // Select a parser
var Parser = $tw.Wiki.parsers[type]; var Parser = $tw.Wiki.parsers[type];
@ -777,7 +773,7 @@ exports.old_parseText = function(type,text,options) {
/* /*
Parse a tiddler according to its MIME type Parse a tiddler according to its MIME type
*/ */
exports.old_parseTiddler = function(title,options) { exports.parseTiddler = function(title,options) {
options = $tw.utils.extend({},options); options = $tw.utils.extend({},options);
var cacheType = options.parseAsInline ? "newInlineParseTree" : "newBlockParseTree", var cacheType = options.parseAsInline ? "newInlineParseTree" : "newBlockParseTree",
tiddler = this.getTiddler(title), tiddler = this.getTiddler(title),
@ -786,44 +782,10 @@ exports.old_parseTiddler = function(title,options) {
if(tiddler.hasField("_canonical_uri")) { if(tiddler.hasField("_canonical_uri")) {
options._canonical_uri = tiddler.fields._canonical_uri; options._canonical_uri = tiddler.fields._canonical_uri;
} }
return self.old_parseText(tiddler.fields.type,tiddler.fields.text,options); return self.parseText(tiddler.fields.type,tiddler.fields.text,options);
}) : null; }) : null;
}; };
var tweakMacroDefinition = function(nodeList) {
if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") {
nodeList[0].type = "set";
nodeList[0].attributes = {
name: {type: "string", value: nodeList[0].name},
value: {type: "string", value: nodeList[0].text}
};
nodeList[0].children = nodeList.slice(1);
nodeList.splice(1,nodeList.length-1);
tweakMacroDefinition(nodeList[0].children);
}
};
var tweakParser = function(parser) {
// Move any macro definitions to contain the body tree
tweakMacroDefinition(parser.tree);
};
exports.parseText = function(type,text,options) {
var parser = this.old_parseText(type,text,options);
if(parser) {
tweakParser(parser);
}
return parser;
};
exports.parseTiddler = function(title,options) {
var parser = this.old_parseTiddler(title,options);
if(parser) {
tweakParser(parser);
}
return parser;
};
exports.parseTextReference = function(title,field,index,options) { exports.parseTextReference = function(title,field,index,options) {
var tiddler,text; var tiddler,text;
if(options.subTiddler) { if(options.subTiddler) {