1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Further optimisations to wiki store

These changes give us a minor performance improvement for adding and deleting tiddlers
This commit is contained in:
Jermolene 2017-12-21 22:01:52 +00:00
parent e4b10d42f9
commit 358d416526

View File

@ -902,11 +902,23 @@ $tw.Wiki = function(options) {
options = options || {}; options = options || {};
var self = this, var self = this,
tiddlers = Object.create(null), // Hashmap of tiddlers tiddlers = Object.create(null), // Hashmap of tiddlers
tiddlerTitles = [], // Array of tiddler titles tiddlerTitles = null, // Array of tiddler titles
getTiddlerTitles = function() {
if(!tiddlerTitles) {
tiddlerTitles = Object.keys(tiddlers);
}
return tiddlerTitles;
},
pluginTiddlers = [], // Array of tiddlers containing registered plugins, ordered by priority pluginTiddlers = [], // Array of tiddlers containing registered plugins, ordered by priority
pluginInfo = Object.create(null), // Hashmap of parsed plugin content pluginInfo = Object.create(null), // Hashmap of parsed plugin content
shadowTiddlers = options.shadowTiddlers || Object.create(null), // Hashmap by title of {source:, tiddler:} shadowTiddlers = options.shadowTiddlers || Object.create(null), // Hashmap by title of {source:, tiddler:}
shadowTiddlerTitles = Object.keys(shadowTiddlers); shadowTiddlerTitles = null,
getShadowTiddlerTitles = function() {
if(!shadowTiddlerTitles) {
shadowTiddlerTitles = Object.keys(shadowTiddlers);
}
return shadowTiddlerTitles;
};
// Add a tiddler to the store // Add a tiddler to the store
this.addTiddler = function(tiddler) { this.addTiddler = function(tiddler) {
@ -920,7 +932,9 @@ $tw.Wiki = function(options) {
// Uncomment the following line for detailed logs of all tiddler writes // Uncomment the following line for detailed logs of all tiddler writes
// console.log("Adding",title,tiddler) // console.log("Adding",title,tiddler)
tiddlers[title] = tiddler; tiddlers[title] = tiddler;
tiddlerTitles = Object.keys(tiddlers); if(tiddlerTitles && tiddlerTitles.indexOf(title) === -1) {
tiddlerTitles.push(title);
}
this.clearCache(title); this.clearCache(title);
this.clearGlobalCache(); this.clearGlobalCache();
this.enqueueTiddlerEvent(title); this.enqueueTiddlerEvent(title);
@ -934,7 +948,12 @@ $tw.Wiki = function(options) {
// console.log("Deleting",title) // console.log("Deleting",title)
if($tw.utils.hop(tiddlers,title)) { if($tw.utils.hop(tiddlers,title)) {
delete tiddlers[title]; delete tiddlers[title];
tiddlerTitles = Object.keys(tiddlers); if(tiddlerTitles) {
var index = tiddlerTitles.indexOf(title);
if(index !== -1) {
tiddlerTitles.splice(index,1);
}
}
this.clearCache(title); this.clearCache(title);
this.clearGlobalCache(); this.clearGlobalCache();
this.enqueueTiddlerEvent(title,true); this.enqueueTiddlerEvent(title,true);
@ -947,7 +966,7 @@ $tw.Wiki = function(options) {
var t = tiddlers[title]; var t = tiddlers[title];
if(t instanceof $tw.Tiddler) { if(t instanceof $tw.Tiddler) {
return t; return t;
} else if(title !== undefined && Object.prototype.hasOwnProperty.call(shadowTiddlers,title)) { } else if(title !== undefined && shadowTiddlers[title]) {
return shadowTiddlers[title].tiddler; return shadowTiddlers[title].tiddler;
} }
return undefined; return undefined;
@ -956,28 +975,30 @@ $tw.Wiki = function(options) {
// Get an array of all tiddler titles // Get an array of all tiddler titles
this.allTitles = function() { this.allTitles = function() {
return tiddlerTitles.slice(0); return getTiddlerTitles().slice(0);
}; };
// Iterate through all tiddler titles // Iterate through all tiddler titles
this.each = function(callback) { this.each = function(callback) {
var index,titlesLength,title; var titles = getTiddlerTitles(),
for(index = 0, titlesLength = tiddlerTitles.length; index < titlesLength; index++) { index,titlesLength,title;
title = tiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
title = titles[index];
callback(tiddlers[title],title); callback(tiddlers[title],title);
} }
}; };
// Get an array of all shadow tiddler titles // Get an array of all shadow tiddler titles
this.allShadowTitles = function() { this.allShadowTitles = function() {
return shadowTiddlerTitles.slice(0); return getShadowTiddlerTitles().slice(0);
}; };
// Iterate through all shadow tiddler titles // Iterate through all shadow tiddler titles
this.eachShadow = function(callback) { this.eachShadow = function(callback) {
var index,titlesLength,title; var titles = getShadowTiddlerTitles(),
for(index = 0, titlesLength = shadowTiddlerTitles.length; index < titlesLength; index++) { index,titlesLength,title;
title = shadowTiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
title = titles[index];
var shadowInfo = shadowTiddlers[title]; var shadowInfo = shadowTiddlers[title];
callback(shadowInfo.tiddler,title); callback(shadowInfo.tiddler,title);
} }
@ -985,14 +1006,16 @@ $tw.Wiki = function(options) {
// Iterate through all tiddlers and then the shadows // Iterate through all tiddlers and then the shadows
this.eachTiddlerPlusShadows = function(callback) { this.eachTiddlerPlusShadows = function(callback) {
var index,titlesLength,title; var index,titlesLength,title,
for(index = 0, titlesLength = tiddlerTitles.length; index < titlesLength; index++) { titles = getTiddlerTitles();
title = tiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
title = titles[index];
callback(tiddlers[title],title); callback(tiddlers[title],title);
} }
for(index = 0, titlesLength = shadowTiddlerTitles.length; index < titlesLength; index++) { titles = getShadowTiddlerTitles();
title = shadowTiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
if(!Object.prototype.hasOwnProperty.call(tiddlers,title)) { title = titles[index];
if(!tiddlers[title]) {
var shadowInfo = shadowTiddlers[title]; var shadowInfo = shadowTiddlers[title];
callback(shadowInfo.tiddler,title); callback(shadowInfo.tiddler,title);
} }
@ -1001,19 +1024,21 @@ $tw.Wiki = function(options) {
// Iterate through all the shadows and then the tiddlers // Iterate through all the shadows and then the tiddlers
this.eachShadowPlusTiddlers = function(callback) { this.eachShadowPlusTiddlers = function(callback) {
var index,titlesLength,title; var index,titlesLength,title,
for(index = 0, titlesLength = shadowTiddlerTitles.length; index < titlesLength; index++) { titles = getShadowTiddlerTitles();
title = shadowTiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
if(Object.prototype.hasOwnProperty.call(tiddlers,title)) { title = titles[index];
if(tiddlers[title]) {
callback(tiddlers[title],title); callback(tiddlers[title],title);
} else { } else {
var shadowInfo = shadowTiddlers[title]; var shadowInfo = shadowTiddlers[title];
callback(shadowInfo.tiddler,title); callback(shadowInfo.tiddler,title);
} }
} }
for(index = 0, titlesLength = tiddlerTitles.length; index < titlesLength; index++) { titles = getTiddlerTitles();
title = tiddlerTitles[index]; for(index = 0, titlesLength = titles.length; index < titlesLength; index++) {
if(!Object.prototype.hasOwnProperty.call(shadowTiddlers,title)) { title = titles[index];
if(!shadowTiddlers[title]) {
callback(tiddlers[title],title); callback(tiddlers[title],title);
} }
} }
@ -1128,7 +1153,7 @@ $tw.Wiki = function(options) {
}); });
} }
}); });
shadowTiddlerTitles = Object.keys(shadowTiddlers); shadowTiddlerTitles = null;
this.clearCache(null); this.clearCache(null);
this.clearGlobalCache(); this.clearGlobalCache();
}; };