From 45c51481def466cf1d61ea27e87d422635703d2e Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 13 Nov 2013 23:33:32 +0000 Subject: [PATCH] Improve performance of tag handling with some caching --- core/modules/wiki.js | 68 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index c05098849..ad1bc7183 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -8,14 +8,12 @@ Extension methods for the $tw.Wiki object Adds the following properties to the wiki object: * `eventListeners` is a hashmap by type of arrays of listener functions -* `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were -last dispatched. Each entry is a hashmap containing two fields: +* `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were last dispatched. Each entry is a hashmap containing two fields: modified: true/false deleted: true/false -* `changeCount` is a hashmap by tiddler title containing a numerical index that starts at zero and is - incremented each time a tiddler is created changed or deleted -* `caches` is a hashmap by tiddler title containing a further hashmap of named cache objects. Caches - are automatically cleared when a tiddler is modified or deleted +* `changeCount` is a hashmap by tiddler title containing a numerical index that starts at zero and is incremented each time a tiddler is created changed or deleted +* `caches` is a hashmap by tiddler title containing a further hashmap of named cache objects. Caches are automatically cleared when a tiddler is modified or deleted +* `globalCache` is a hashmap by cache name of cache objects that are cleared whenever any tiddler change occurs \*/ (function(){ @@ -163,6 +161,7 @@ exports.getChangeCount = function(title) { exports.deleteTiddler = function(title) { delete this.tiddlers[title]; this.clearCache(title); + this.clearGlobalCache(); this.enqueueTiddlerEvent(title,true); }; @@ -207,6 +206,7 @@ exports.addTiddler = function(tiddler) { // Save the tiddler this.tiddlers[title] = tiddler; this.clearCache(title); + this.clearGlobalCache(); this.enqueueTiddlerEvent(title); }; @@ -425,15 +425,36 @@ exports.getShadowTitles = function() { Retrieves a list of the tiddler titles that are tagged with a given tag */ exports.getTiddlersWithTag = function(tag) { - // Get the list associated with the tag - var titles = []; - for(var title in this.tiddlers) { - var tiddler = this.tiddlers[title]; - if($tw.utils.isArray(tiddler.fields.tags) && tiddler.fields.tags.indexOf(tag) !== -1) { - titles.push(title); + var self = this; + return this.getGlobalCache("taglist-" + tag,function() { + var tagmap = self.getTagMap(); + return self.sortByList(tagmap[tag],tag); + }); +}; + +/* +Get a hashmap by tag of arrays of tiddler titles +*/ +exports.getTagMap = function() { + var self = this; + return this.getGlobalCache("tagmap",function() { + var tags = {}; + // Collect up all the tags + for(var title in self.tiddlers) { + var tiddler = self.tiddlers[title]; + if(tiddler.fields.tags) { + for(var index=0; index