mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-07-18 01:42:49 +00:00
Revert attempt at optimising filter execution
At the moment the optimiser returns a list of chainable functions, it would be simpler to just return a single function
This commit is contained in:
parent
9493084f95
commit
02f3065e4f
@ -8,23 +8,23 @@ Adds tiddler filtering methods to the $tw.Wiki object.
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var widgetClass = require("$:/core/modules/widgets/widget.js").widget;
|
||||
var widgetClass = require("$:/core/modules/widgets/widget.js").widget;
|
||||
|
||||
/* Maximum permitted filter recursion depth */
|
||||
var MAX_FILTER_DEPTH = 300;
|
||||
/* Maximum permitted filter recursion depth */
|
||||
var MAX_FILTER_DEPTH = 300;
|
||||
|
||||
/*
|
||||
Parses an operation (i.e. a run) within a filter string
|
||||
/*
|
||||
Parses an operation (i.e. a run) within a filter string
|
||||
operators: Array of array of operator nodes into which results should be inserted
|
||||
filterString: filter string
|
||||
p: start position within the string
|
||||
Returns the new start position, after the parsed operation
|
||||
*/
|
||||
function parseFilterOperation(operators,filterString,p) {
|
||||
Returns the new start position, after the parsed operation
|
||||
*/
|
||||
function parseFilterOperation(operators,filterString,p) {
|
||||
var nextBracketPos, operator;
|
||||
// Skip the starting square bracket
|
||||
if(filterString.charAt(p++) !== "[") {
|
||||
@ -133,12 +133,12 @@ function parseFilterOperation(operators,filterString,p) {
|
||||
}
|
||||
// Return the parsing position
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Parse a filter string
|
||||
*/
|
||||
exports.parseFilter = function(filterString) {
|
||||
/*
|
||||
Parse a filter string
|
||||
*/
|
||||
exports.parseFilter = function(filterString) {
|
||||
filterString = filterString || "";
|
||||
var results = [], // Array of arrays of operator nodes {operator:,operand:}
|
||||
p = 0, // Current position in the filter string
|
||||
@ -196,37 +196,35 @@ exports.parseFilter = function(filterString) {
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
};
|
||||
|
||||
exports.getFilterOperators = function() {
|
||||
exports.getFilterOperators = function() {
|
||||
if(!this.filterOperators) {
|
||||
$tw.Wiki.prototype.filterOperators = {};
|
||||
$tw.modules.applyMethods("filteroperator",this.filterOperators);
|
||||
}
|
||||
return this.filterOperators;
|
||||
};
|
||||
};
|
||||
|
||||
exports.getFilterRunPrefixes = function() {
|
||||
exports.getFilterRunPrefixes = function() {
|
||||
if(!this.filterRunPrefixes) {
|
||||
$tw.Wiki.prototype.filterRunPrefixes = {};
|
||||
$tw.modules.applyMethods("filterrunprefix",this.filterRunPrefixes);
|
||||
}
|
||||
return this.filterRunPrefixes;
|
||||
}
|
||||
}
|
||||
|
||||
exports.filterTiddlers = function(filterString,widget,source) {
|
||||
exports.filterTiddlers = function(filterString,widget,source) {
|
||||
var fn = this.compileFilter(filterString);
|
||||
return fn.call(this,source,widget);
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
Compile a filter into a function with the signature fn(source,widget) where:
|
||||
source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title)
|
||||
widget: an optional widget node for retrieving the current tiddler etc.
|
||||
*/
|
||||
exports.compileFilter = function(filterString) {
|
||||
var self = this;
|
||||
// Use cached filter function if already present
|
||||
/*
|
||||
Compile a filter into a function with the signature fn(source,widget) where:
|
||||
source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title)
|
||||
widget: an optional widget node for retrieving the current tiddler etc.
|
||||
*/
|
||||
exports.compileFilter = function(filterString) {
|
||||
if(!this.filterCache) {
|
||||
this.filterCache = Object.create(null);
|
||||
this.filterCacheCount = 0;
|
||||
@ -234,11 +232,6 @@ exports.compileFilter = function(filterString) {
|
||||
if(this.filterCache[filterString] !== undefined) {
|
||||
return this.filterCache[filterString];
|
||||
}
|
||||
// Attempt to optimise the filter into a single query
|
||||
var operationFunctions = this.optimiseFilter && this.optimiseFilter(filterString);
|
||||
// Otherwise compile the filter step by step
|
||||
if(!operationFunctions) {
|
||||
// Parse filter
|
||||
var filterParseTree;
|
||||
try {
|
||||
filterParseTree = this.parseFilter(filterString);
|
||||
@ -248,50 +241,12 @@ exports.compileFilter = function(filterString) {
|
||||
return [$tw.language.getString("Error/Filter") + ": " + e];
|
||||
};
|
||||
}
|
||||
// Compile the filter operators into functions
|
||||
operationFunctions = this.compileFilterOperations(filterParseTree);
|
||||
}
|
||||
// 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) {
|
||||
source = self.each;
|
||||
} else if(typeof source === "object") { // Array or hashmap
|
||||
source = self.makeTiddlerIterator(source);
|
||||
}
|
||||
if(!widget) {
|
||||
widget = $tw.rootWidget;
|
||||
}
|
||||
var results = new $tw.utils.LinkedList();
|
||||
self.filterRecursionCount = (self.filterRecursionCount || 0) + 1;
|
||||
if(self.filterRecursionCount < MAX_FILTER_DEPTH) {
|
||||
$tw.utils.each(operationFunctions,function(operationFunction) {
|
||||
operationFunction(results,source,widget);
|
||||
});
|
||||
} else {
|
||||
results.push("/**-- Excessive filter recursion --**/");
|
||||
}
|
||||
self.filterRecursionCount = self.filterRecursionCount - 1;
|
||||
return results.toArray();
|
||||
});
|
||||
if(this.filterCacheCount >= 2000) {
|
||||
// To prevent memory leak, we maintain an upper limit for cache size.
|
||||
// Reset if exceeded. This should give us 95% of the benefit
|
||||
// that no cache limit would give us.
|
||||
this.filterCache = Object.create(null);
|
||||
this.filterCacheCount = 0;
|
||||
}
|
||||
this.filterCache[filterString] = fnMeasured;
|
||||
this.filterCacheCount++;
|
||||
return fnMeasured;
|
||||
};
|
||||
|
||||
exports.compileFilterOperations = function(filterParseTree) {
|
||||
var self = this;
|
||||
// Get the hashmap of filter operator functions
|
||||
var filterOperators = this.getFilterOperators();
|
||||
// Assemble array of functions, one for each operation
|
||||
var operationFunctions = [];
|
||||
// Step through the operations
|
||||
var self = this;
|
||||
$tw.utils.each(filterParseTree,function(operation) {
|
||||
// Create a function for the chain of operators in the operation
|
||||
var operationSubFunction = function(source,widget) {
|
||||
@ -379,7 +334,39 @@ exports.compileFilterOperations = function(filterParseTree) {
|
||||
}
|
||||
})());
|
||||
});
|
||||
return operationFunctions;
|
||||
};
|
||||
// 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) {
|
||||
source = self.each;
|
||||
} else if(typeof source === "object") { // Array or hashmap
|
||||
source = self.makeTiddlerIterator(source);
|
||||
}
|
||||
if(!widget) {
|
||||
widget = $tw.rootWidget;
|
||||
}
|
||||
var results = new $tw.utils.LinkedList();
|
||||
self.filterRecursionCount = (self.filterRecursionCount || 0) + 1;
|
||||
if(self.filterRecursionCount < MAX_FILTER_DEPTH) {
|
||||
$tw.utils.each(operationFunctions,function(operationFunction) {
|
||||
operationFunction(results,source,widget);
|
||||
});
|
||||
} else {
|
||||
results.push("/**-- Excessive filter recursion --**/");
|
||||
}
|
||||
self.filterRecursionCount = self.filterRecursionCount - 1;
|
||||
return results.toArray();
|
||||
});
|
||||
if(this.filterCacheCount >= 2000) {
|
||||
// To prevent memory leak, we maintain an upper limit for cache size.
|
||||
// Reset if exceeded. This should give us 95% of the benefit
|
||||
// that no cache limit would give us.
|
||||
this.filterCache = Object.create(null);
|
||||
this.filterCacheCount = 0;
|
||||
}
|
||||
this.filterCache[filterString] = fnMeasured;
|
||||
this.filterCacheCount++;
|
||||
return fnMeasured;
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user