Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions boot/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var _boot = (function($tw) {

"use strict";

if(typeof performance !== "undefined") { performance.mark("tw-boot-start"); }

// Include bootprefix if we're not given module data
if(!$tw) {
$tw = require("./bootprefix.js").bootprefix();
Expand Down Expand Up @@ -1158,6 +1160,30 @@ $tw.Wiki = function(options) {
pluginTiddlers = [], // Array of tiddlers containing registered plugins, ordered by priority
pluginInfo = Object.create(null), // Hashmap of parsed plugin content
shadowTiddlers = Object.create(null), // Hashmap by title of {source:, tiddler:}
systemTiddlerTitles = null, // Array of system tiddler titles (starting with "$:/")
nonSystemTiddlerTitles = null, // Array of non-system tiddler titles
partitionTiddlerTitles = function() {
if(systemTiddlerTitles === null) {
systemTiddlerTitles = [];
nonSystemTiddlerTitles = [];
var titles = getTiddlerTitles();
for(var i = 0, length = titles.length; i < length; i++) {
if(titles[i].indexOf("$:/") === 0) {
systemTiddlerTitles.push(titles[i]);
} else {
nonSystemTiddlerTitles.push(titles[i]);
}
}
}
},
getSystemTiddlerTitles = function() {
partitionTiddlerTitles();
return systemTiddlerTitles;
},
getNonSystemTiddlerTitles = function() {
partitionTiddlerTitles();
return nonSystemTiddlerTitles;
},
shadowTiddlerTitles = null,
getShadowTiddlerTitles = function() {
if(!shadowTiddlerTitles) {
Expand Down Expand Up @@ -1206,6 +1232,14 @@ $tw.Wiki = function(options) {
tiddlers[title] = tiddler;
// Check we've got the title
tiddlerTitles = $tw.utils.insertSortedArray(tiddlerTitles || [],title);
// Maintain system/non-system partitions
if(systemTiddlerTitles !== null) {
if(title.indexOf("$:/") === 0) {
$tw.utils.insertSortedArray(systemTiddlerTitles,title);
} else {
$tw.utils.insertSortedArray(nonSystemTiddlerTitles,title);
}
}
// Record the new tiddler state
updateDescriptor["new"] = {
tiddler: tiddler,
Expand Down Expand Up @@ -1246,6 +1280,14 @@ $tw.Wiki = function(options) {
tiddlerTitles.splice(index,1);
}
}
// Delete from system/non-system partitions
if(systemTiddlerTitles !== null) {
var partitionArray = title.indexOf("$:/") === 0 ? systemTiddlerTitles : nonSystemTiddlerTitles;
var partitionIndex = partitionArray.indexOf(title);
if(partitionIndex !== -1) {
partitionArray.splice(partitionIndex,1);
}
}
// Record the new tiddler state
updateDescriptor["new"] = {
tiddler: this.getTiddler(title),
Expand Down Expand Up @@ -1284,6 +1326,16 @@ $tw.Wiki = function(options) {
return getTiddlerTitles().slice(0);
};

// Get an array of all system tiddler titles (returns cached array; do not mutate)
this.allSystemTitles = function() {
return getSystemTiddlerTitles();
};

// Get an array of all non-system tiddler titles (returns cached array; do not mutate)
this.allNonSystemTitles = function() {
return getNonSystemTiddlerTitles();
};

// Iterate through all tiddler titles
this.each = function(callback) {
var titles = getTiddlerTitles(),
Expand Down Expand Up @@ -2539,7 +2591,9 @@ $tw.boot.loadStartup = function(options){

// Load tiddlers
if($tw.boot.tasks.readBrowserTiddlers) {
if(typeof performance !== "undefined") { performance.mark("tw-boot-store-read-start"); }
$tw.loadTiddlersBrowser();
if(typeof performance !== "undefined") { performance.mark("tw-boot-store-read-end"); }
} else {
$tw.loadTiddlersNode();
}
Expand All @@ -2551,6 +2605,7 @@ $tw.boot.loadStartup = function(options){
$tw.hooks.invokeHook("th-boot-tiddlers-loaded");
};
$tw.boot.execStartup = function(options){
if(typeof performance !== "undefined") { performance.mark("tw-boot-exec-start"); }
// Unpack plugin tiddlers
$tw.wiki.readPluginInfo();
$tw.wiki.registerPluginTiddlers("plugin",$tw.safeMode ? ["$:/core"] : undefined);
Expand Down Expand Up @@ -2578,6 +2633,7 @@ $tw.boot.execStartup = function(options){
$tw.boot.executedStartupModules = Object.create(null);
$tw.boot.disabledStartupModules = $tw.boot.disabledStartupModules || [];
// Repeatedly execute the next eligible task
if(typeof performance !== "undefined") { performance.mark("tw-boot-startup-modules-start"); }
$tw.boot.executeNextStartupTask(options.callback);
};
/*
Expand Down Expand Up @@ -2645,6 +2701,7 @@ $tw.boot.executeNextStartupTask = function(callback) {
}
taskIndex++;
}
if(typeof performance !== "undefined") { performance.mark("tw-boot-complete"); }
if(typeof callback === "function") {
callback();
}
Expand Down
5 changes: 4 additions & 1 deletion boot/bootprefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See Boot.js for further details of the boot process.

/* eslint-disable @stylistic/indent */

if(typeof performance !== "undefined") { performance.mark("tw-bootprefix-start"); }

var _bootprefix = (function($tw) {

"use strict";
Expand All @@ -23,7 +25,7 @@ $tw.boot = $tw.boot || Object.create(null);

// Config
$tw.config = $tw.config || Object.create(null);
$tw.config.maxEditFileSize = 100 * 1024 * 1024; // 100MB
$tw.config.maxEditFileSize = 200 * 1024 * 1024; // 200MB

// Detect platforms
if(!("browser" in $tw)) {
Expand Down Expand Up @@ -121,6 +123,7 @@ return $tw;
if(typeof(exports) === "undefined") {
// Set up $tw global for the browser
window.$tw = _bootprefix(window.$tw);
if(typeof performance !== "undefined") { performance.mark("tw-bootprefix-end"); }
} else {
// Export functionality as a module
exports.bootprefix = _bootprefix;
Expand Down
51 changes: 34 additions & 17 deletions core/modules/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ exports.compileFilter = function(filterString) {
var filterOperators = this.getFilterOperators();
// Assemble array of functions, one for each operation
var operationFunctions = [];
var operationSubFunctions = []; // Unwrapped sub-functions for fast path
// Step through the operations
var self = this;
$tw.utils.each(filterParseTree,function(operation) {
Expand Down Expand Up @@ -289,20 +290,24 @@ exports.compileFilter = function(filterString) {
operand.value = self.getTextReference(operand.text,"",currTiddlerTitle);
operand.multiValue = [operand.value];
} else if(operand.variable) {
var varTree = $tw.utils.parseFilterVariable(operand.text);
operand.value = widgetClass.evaluateVariable(widget,varTree.name,{params: varTree.params, source: source})[0] || "";
if(!operand._varTree) {
operand._varTree = $tw.utils.parseFilterVariable(operand.text);
}
operand.value = widgetClass.evaluateVariable(widget,operand._varTree.name,{params: operand._varTree.params, source: source})[0] || "";
operand.multiValue = [operand.value];
} else if(operand.multiValuedVariable) {
var varTree = $tw.utils.parseFilterVariable(operand.text);
var resultList = widgetClass.evaluateVariable(widget,varTree.name,{params: varTree.params, source: source});
if(!operand._varTree) {
operand._varTree = $tw.utils.parseFilterVariable(operand.text);
}
var resultList = widgetClass.evaluateVariable(widget,operand._varTree.name,{params: operand._varTree.params, source: source});
if((resultList.length > 0 && resultList[0] !== undefined) || resultList.length === 0) {
operand.multiValue = widgetClass.evaluateVariable(widget,varTree.name,{params: varTree.params, source: source}) || [];
operand.multiValue = widgetClass.evaluateVariable(widget,operand._varTree.name,{params: operand._varTree.params, source: source}) || [];
operand.value = operand.multiValue[0] || "";
} else {
operand.value = "";
operand.multiValue = [];
}
operand.isMultiValueOperand = true;
operand.isMultiValueOperand = true;
} else {
operand.value = operand.text;
operand.multiValue = [operand.value];
Expand Down Expand Up @@ -343,6 +348,7 @@ exports.compileFilter = function(filterString) {
return resultArray;
}
};
operationSubFunctions.push(operationSubFunction);
var filterRunPrefixes = self.getFilterRunPrefixes();
// Wrap the operator functions in a wrapper function that depends on the prefix
operationFunctions.push((function() {
Expand Down Expand Up @@ -372,6 +378,10 @@ exports.compileFilter = function(filterString) {
}
})());
});
// Detect single "or" run for fast path (bypass LinkedList)
var isSingleOrRun = filterParseTree.length === 1 &&
(!filterParseTree[0].prefix || filterParseTree[0].prefix === "");
var singleOrSubFunction = isSingleOrRun ? operationSubFunctions[0] : null;
// Return a function that applies the operations to a source iterator of tiddler titles
var fnMeasured = $tw.perf.measure("filter: " + filterString,function filterFunction(source,widget) {
if(!source) {
Expand All @@ -382,23 +392,30 @@ exports.compileFilter = function(filterString) {
if(!widget) {
widget = $tw.rootWidget;
}
var results = new $tw.utils.LinkedList();
self.filterRecursionCount = (self.filterRecursionCount || 0) + 1;
var resultArray;
if(self.filterRecursionCount < MAX_FILTER_DEPTH) {
$tw.utils.each(operationFunctions,function(operationFunction) {
var operationResult = operationFunction(results,source,widget);
if(operationResult) {
if(operationResult.variables) {
// If the filter run prefix has returned variables, create a new fake widget with those variables
widget = widget.makeFakeWidgetWithVariables(operationResult.variables);
if(singleOrSubFunction) {
// Fast path: single "or" run, return array directly without LinkedList
resultArray = singleOrSubFunction(source,widget);
} else {
var results = new $tw.utils.LinkedList();
$tw.utils.each(operationFunctions,function(operationFunction) {
var operationResult = operationFunction(results,source,widget);
if(operationResult) {
if(operationResult.variables) {
// If the filter run prefix has returned variables, create a new fake widget with those variables
widget = widget.makeFakeWidgetWithVariables(operationResult.variables);
}
}
}
});
});
resultArray = results.toArray();
}
} else {
results.push("/**-- Excessive filter recursion --**/");
resultArray = ["/**-- Excessive filter recursion --**/"];
}
self.filterRecursionCount = self.filterRecursionCount - 1;
return results.toArray();
return resultArray;
});
if(this.filterCacheCount >= 2000) {
// To prevent memory leak, we maintain an upper limit for cache size.
Expand Down
12 changes: 12 additions & 0 deletions core/modules/filters/is/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ Filter function for [is[shadow]]
Export our filter function
*/
exports.shadow = function(source,prefix,options) {
// Fast path: when source is wiki.each (all real tiddlers), use shadow title list
if(source === options.wiki.each && prefix !== "!") {
// Return real tiddlers that are also shadow tiddlers (overridden shadows)
var results = [],
shadowTitles = options.wiki.allShadowTitles();
for(var i = 0, len = shadowTitles.length; i < len; i++) {
if(options.wiki.tiddlerExists(shadowTitles[i])) {
results.push(shadowTitles[i]);
}
}
return results;
}
var results = [];
if(prefix === "!") {
source(function(tiddler,title) {
Expand Down
8 changes: 8 additions & 0 deletions core/modules/filters/is/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Filter function for [is[system]]
Export our filter function
*/
exports.system = function(source,prefix,options) {
// Fast path: when iterating all tiddlers, use pre-partitioned arrays
if(source === options.wiki.each) {
if(prefix === "!") {
return options.wiki.allNonSystemTitles();
} else {
return options.wiki.allSystemTitles();
}
}
var results = [];
if(prefix === "!") {
source(function(tiddler,title) {
Expand Down
7 changes: 7 additions & 0 deletions core/modules/filters/is/tiddler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Filter function for [is[tiddler]]
Export our filter function
*/
exports.tiddler = function(source,prefix,options) {
// Fast path: wiki.each only iterates real tiddlers, all of which exist
if(source === options.wiki.each) {
if(prefix === "!") {
return []; // No real tiddler fails tiddlerExists
}
return source; // Return iterator directly; all real tiddlers pass
}
var results = [];
if(prefix === "!") {
source(function(tiddler,title) {
Expand Down
15 changes: 15 additions & 0 deletions core/modules/filters/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ Filter operator returning all the tags of the selected tiddlers
Export our filter function
*/
exports.tags = function(source,operator,options) {
// Fast path: cache result when iterating all tiddlers
if(source === options.wiki.each) {
return options.wiki.getGlobalCache("filter-tags-all-tiddlers",function() {
var tags = {};
source(function(tiddler,title) {
var t, length;
if(tiddler && tiddler.fields.tags) {
for(t=0, length=tiddler.fields.tags.length; t<length; t++) {
tags[tiddler.fields.tags[t]] = true;
}
}
});
return Object.keys(tags);
});
}
var tags = {};
source(function(tiddler,title) {
var t, length;
Expand Down
52 changes: 51 additions & 1 deletion core/modules/indexers/tag-indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,57 @@ TagSubIndexer.prototype.rebuild = function() {
};

TagSubIndexer.prototype.update = function(updateDescriptor) {
this.index = null;
// If the index hasn't been built yet, no update needed
if(this.index === null) {
return;
}
// Determine whether the old/new tiddler is visible to this iterator
var oldVisible = this._isVisible(updateDescriptor.old),
newVisible = this._isVisible(updateDescriptor["new"]),
self = this;
// Remove old tags from index
if(oldVisible && updateDescriptor.old.tiddler) {
var oldTitle = updateDescriptor.old.tiddler.fields.title,
oldTags = updateDescriptor.old.tiddler.fields.tags || [];
$tw.utils.each(oldTags,function(tag) {
if(self.index[tag]) {
var idx = self.index[tag].titles.indexOf(oldTitle);
if(idx !== -1) {
self.index[tag].titles.splice(idx,1);
if(self.index[tag].titles.length === 0) {
delete self.index[tag];
}
}
}
});
}
// Add new tags to index
if(newVisible && updateDescriptor["new"].tiddler) {
var newTitle = updateDescriptor["new"].tiddler.fields.title,
newTags = updateDescriptor["new"].tiddler.fields.tags || [];
$tw.utils.each(newTags,function(tag) {
if(!self.index[tag]) {
self.index[tag] = {isSorted: false, titles: [newTitle]};
} else if(self.index[tag].titles.indexOf(newTitle) === -1) {
self.index[tag].titles.push(newTitle);
self.index[tag].isSorted = false;
}
});
}
};

/*
Determine whether a tiddler described by a descriptor is visible to this sub-indexer's iterator
*/
TagSubIndexer.prototype._isVisible = function(descriptor) {
if(this.iteratorMethod === "each") {
return descriptor.exists;
} else if(this.iteratorMethod === "eachShadow") {
return descriptor.shadow;
} else {
// eachTiddlerPlusShadows and eachShadowPlusTiddlers both visit all tiddlers and shadows
return descriptor.exists || descriptor.shadow;
}
};

TagSubIndexer.prototype.lookup = function(tag) {
Expand Down
Loading
Loading