Refine heap performance. Switch back to underscore since lodash does npm

all wrong.
This commit is contained in:
Tom MacWright 2012-12-21 16:53:38 -05:00
parent 22f5d0cc45
commit f6c07afee6
12 changed files with 56 additions and 63 deletions

View File

@ -4,7 +4,7 @@ var path = require('path'),
fs = require('fs'), fs = require('fs'),
carto = require('carto'), carto = require('carto'),
url = require('url'), url = require('url'),
_ = require('lodash'); _ = require('underscore');
var existsSync = require('fs').existsSync || require('path').existsSync var existsSync = require('fs').existsSync || require('path').existsSync

View File

@ -2,6 +2,7 @@ var util = require('util'),
fs = require('fs'), fs = require('fs'),
path = require('path'); path = require('path');
function getVersion() { function getVersion() {
if (parseInt(process.version.split('.')[1], 10) > 4) { if (parseInt(process.version.split('.')[1], 10) > 4) {
return require('../../package.json').version.split('.'); return require('../../package.json').version.split('.');

View File

@ -3,7 +3,7 @@ var carto, tree, _;
if (typeof(process) !== 'undefined') { if (typeof(process) !== 'undefined') {
carto = exports; carto = exports;
tree = require('./tree'); tree = require('./tree');
_ = require('lodash'); _ = require('underscore');
} else { } else {
if (typeof(window.carto) === 'undefined') { window.carto = {}; } if (typeof(window.carto) === 'undefined') { window.carto = {}; }
carto = window.carto; carto = window.carto;

View File

@ -1,4 +1,4 @@
var _ = require('lodash'); var _ = require('underscore');
var carto = require('./index'); var carto = require('./index');
var tree = require('./tree'); var tree = require('./tree');
@ -29,7 +29,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
var output = []; var output = [];
// Transform stylesheets into rulesets. // Transform stylesheets into rulesets.
var rulesets = _(m.Stylesheet) var rulesets = _(m.Stylesheet).chain()
.map(function(s) { .map(function(s) {
if (typeof s == 'string') { if (typeof s == 'string') {
throw new Error("Stylesheet object is expected not a string: '" + s + "'"); throw new Error("Stylesheet object is expected not a string: '" + s + "'");
@ -50,31 +50,36 @@ carto.Renderer.prototype.render = function render(m, callback) {
// Iterate through layers and create styles custom-built // Iterate through layers and create styles custom-built
// for each of them, and apply those styles to the layers. // for each of them, and apply those styles to the layers.
m.Layer.forEach(function(l) { var styles, l, classIndex, rules, sorted, matching;
for (var i = 0; i < m.Layer.length; i++) {
l = m.Layer[i];
styles = [];
classIndex = {};
if (env.benchmark) console.warn('processing layer: ' + l.id); if (env.benchmark) console.warn('processing layer: ' + l.id);
l.styles = [];
// Classes are given as space-separated alphanumeric strings. // Classes are given as space-separated alphanumeric strings.
var classes = (l['class'] || '').split(/\s+/g); var classes = (l['class'] || '').split(/\s+/g);
var matching = rulesets.filter(function(definition) { for (var j = 0; j < classes.length; j++) {
return definition.appliesTo(l.name, classes); classIndex[classes[j]] = true;
});
var rules = inheritRules(matching, env);
var sorted = sortStyles(rules, env);
_(sorted).each(function(rule) {
var style = new tree.Style(l.name, rule.attachment, rule);
if (style) {
l.styles.push(style.name);
var xml = style.toXML(env);
// env.effects can be modified by this call
output.push(xml);
}
});
if (l.styles.length) {
output.push((new carto.tree.Layer(l)).toXML());
} }
}); matching = rulesets.filter(function(definition) {
return definition.appliesTo(l.name, classIndex);
});
rules = inheritRules(matching, env);
sorted = sortStyles(rules, env);
for (var k = 0, rule, style_name; k < sorted.length; k++) {
rule = sorted[k];
style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');
styles.push(style_name);
// env.effects can be modified by this call
output.push(tree.Style.toXML(style_name, rule.attachment, rule, env));
}
if (styles.length) {
output.push(carto.tree.Layer.toXML(l, styles));
}
}
output.unshift(env.effects.map(function(e) { output.unshift(env.effects.map(function(e) {
return e.toXML(env); return e.toXML(env);

View File

@ -11,11 +11,11 @@ tree.Definition = function Definition(selector, rules) {
this.elements = selector.elements; this.elements = selector.elements;
assert.ok(selector.filters instanceof tree.Filterset); assert.ok(selector.filters instanceof tree.Filterset);
this.rules = rules; this.rules = rules;
this.ruleIndex = []; this.ruleIndex = {};
for (var i = 0; i < this.rules.length; i++) { for (var i = 0; i < this.rules.length; i++) {
if ('zoom' in this.rules[i]) this.rules[i] = this.rules[i].clone(); if ('zoom' in this.rules[i]) this.rules[i] = this.rules[i].clone();
this.rules[i].zoom = selector.zoom; this.rules[i].zoom = selector.zoom;
this.ruleIndex.push(this.rules[i].updateID()); this.ruleIndex[this.rules[i].updateID()] = true;
} }
this.filters = selector.filters; this.filters = selector.filters;
this.zoom = selector.zoom; this.zoom = selector.zoom;
@ -35,7 +35,7 @@ tree.Definition.prototype.clone = function(filters) {
if (filters) assert.ok(filters instanceof tree.Filterset); if (filters) assert.ok(filters instanceof tree.Filterset);
var clone = Object.create(tree.Definition.prototype); var clone = Object.create(tree.Definition.prototype);
clone.rules = this.rules.slice(); clone.rules = this.rules.slice();
clone.ruleIndex = this.ruleIndex.slice(); clone.ruleIndex = Object.create(this.ruleIndex);
clone.filters = filters ? filters : this.filters.clone(); clone.filters = filters ? filters : this.filters.clone();
clone.attachment = this.attachment; clone.attachment = this.attachment;
return clone; return clone;
@ -46,9 +46,9 @@ tree.Definition.prototype.addRules = function(rules) {
// Add only unique rules. // Add only unique rules.
for (var i = 0; i < rules.length; i++) { for (var i = 0; i < rules.length; i++) {
if (this.ruleIndex.indexOf(rules[i].id) < 0) { if (!this.ruleIndex[rules[i].id]) {
this.rules.push(rules[i]); this.rules.push(rules[i]);
this.ruleIndex.push(rules[i].id); this.ruleIndex[rules[i].id] = true;
added++; added++;
} }
} }
@ -60,7 +60,7 @@ tree.Definition.prototype.addRules = function(rules) {
// and array of classes, by determining whether // and array of classes, by determining whether
// all elements it contains match. // all elements it contains match.
tree.Definition.prototype.appliesTo = function(id, classes) { tree.Definition.prototype.appliesTo = function(id, classes) {
for (var i = 0; i < this.elements.length; i++) { for (var i = 0, l = this.elements.length; i < l; i++) {
if (!this.elements[i].matches(id, classes)) { if (!this.elements[i].matches(id, classes)) {
return false; return false;
} }

View File

@ -12,7 +12,7 @@ tree.Dimension = function Dimension(value, unit, index) {
tree.Dimension.prototype = { tree.Dimension.prototype = {
is: 'float', is: 'float',
'eval': function (env) { 'eval': function (env) {
if (this.unit && ['px', '%'].indexOf(this.unit) === -1) { if (this.unit && (this.unit !== 'px' && this.unit !== '%')) {
env.error({ env.error({
message: "Invalid unit: '" + this.unit + "'", message: "Invalid unit: '" + this.unit + "'",
index: this.index index: this.index

View File

@ -25,7 +25,7 @@ tree.Element.prototype.toString = function() {
// Takes a plain string for id and plain strings in the array of // Takes a plain string for id and plain strings in the array of
// classes. // classes.
tree.Element.prototype.matches = function(id, classes) { tree.Element.prototype.matches = function(id, classes) {
return (classes.indexOf(this.value.replace(/^\./, '')) !== -1) || return (classes[this.value.replace(/^\./, '')]) ||
(this.value.replace(/^#/, '') === id) || (this.value.replace(/^#/, '') === id) ||
(this.value === '*'); (this.value === '*');
}; };

View File

@ -65,9 +65,7 @@ tree.Filterset.prototype.cloneWith = function(other) {
// We can add the rules that are already present without going through the // We can add the rules that are already present without going through the
// add function as a Filterset is always in it's simplest canonical form. // add function as a Filterset is always in it's simplest canonical form.
for (id in this.filters) { clone.filters = Object.create(this.filters);
clone.filters[id] = this.filters[id];
}
// Only add new filters that actually change the filter. // Only add new filters that actually change the filter.
while (id = additions.shift()) { while (id = additions.shift()) {

View File

@ -1,32 +1,26 @@
(function(tree) { (function(tree) {
tree.Layer = function Layer(obj) { tree.Layer = function Layer(obj, styles) {
this.name = obj.name;
this.status = obj.status;
this.styles = obj.styles;
this.properties = obj.properties || {};
this.srs = obj.srs;
this.datasource = obj.Datasource;
}; };
tree.Layer.prototype.toXML = function() { tree.Layer.toXML = function(obj, styles) {
var dsoptions = []; var dsoptions = [];
for (var i in this.datasource) { for (var i in obj.Datasource) {
dsoptions.push('<Parameter name="' + i + '"><![CDATA[' + dsoptions.push('<Parameter name="' + i + '"><![CDATA[' +
this.datasource[i] + ']]></Parameter>'); obj.Datasource[i] + ']]></Parameter>');
} }
var prop_string = ''; var prop_string = '';
for (var prop in this.properties) { for (var prop in obj.properties) {
prop_string += ' ' + prop + '="' + this.properties[prop] + '"\n'; prop_string += ' ' + prop + '="' + obj.properties[prop] + '"\n';
} }
return '<Layer' + return '<Layer' +
' name="' + this.name + '"\n' + ' name="' + obj.name + '"\n' +
prop_string + prop_string +
((typeof this.status === 'undefined') ? '' : ' status="' + this.status + '"\n') + ((typeof obj.status === 'undefined') ? '' : ' status="' + obj.status + '"\n') +
' srs="' + this.srs + '">\n ' + ' srs="' + obj.srs + '">\n ' +
this.styles.reverse().map(function(s) { styles.reverse().map(function(s) {
return '<StyleName>' + s + '</StyleName>'; return '<StyleName>' + s + '</StyleName>';
}).join('\n ') + }).join('\n ') +
'\n <Datasource>\n ' + '\n <Datasource>\n ' +

View File

@ -4,7 +4,7 @@
// combinations. // combinations.
(function(tree) { (function(tree) {
var _ = require('lodash'); var _ = require('underscore');
var mapnik_reference = require('mapnik-reference'); var mapnik_reference = require('mapnik-reference');
tree.Reference = { tree.Reference = {

View File

@ -1,16 +1,12 @@
(function(tree) { (function(tree) {
var _ = require('lodash'); var _ = require('underscore');
tree.Style = function Style(name, attachment, definitions) { tree.Style = function Style(name, attachment, definitions) {
this.attachment = attachment;
this.definitions = definitions;
this.name = name + (attachment !== '__default__' ? '-' + attachment : '');
}; };
tree.Style.prototype.toXML = function(env) { tree.Style.toXML = function(name, attachment, definitions, env) {
var existing = {}; var existing = {};
var definitions = this.definitions;
function byName(name) { function byName(name) {
return _.flatten(definitions.map(function(definition) { return _.flatten(definitions.map(function(definition) {
return definition.rules.filter(function(rule) { return definition.rules.filter(function(rule) {
@ -23,7 +19,7 @@ tree.Style.prototype.toXML = function(env) {
var comp_op = byName('comp-op'); var comp_op = byName('comp-op');
var opacity = byName('opacity'); var opacity = byName('opacity');
var rules = this.definitions.map(function(definition) { var rules = definitions.map(function(definition) {
return definition.toXML(env, existing); return definition.toXML(env, existing);
}); });
@ -43,7 +39,7 @@ tree.Style.prototype.toXML = function(env) {
attrs_xml += ' opacity="' + opacity[0].value.eval(env).toString() + '" '; attrs_xml += ' opacity="' + opacity[0].value.eval(env).toString() + '" ';
} }
return '<Style name="' + this.name + '" filter-mode="first" ' + attrs_xml + '>\n' + rules.join('') + '</Style>'; return '<Style name="' + name + '" filter-mode="first" ' + attrs_xml + '>\n' + rules.join('') + '</Style>';
}; };
})(require('../tree')); })(require('../tree'));

View File

@ -37,8 +37,7 @@
"dependencies": { "dependencies": {
"underscore": "~1.3.3", "underscore": "~1.3.3",
"mapnik-reference": "~5.0.0", "mapnik-reference": "~5.0.0",
"xml2js": "~0.1.13", "xml2js": "~0.1.13"
"lodash": "~1.0.0-rc.3"
}, },
"devDependencies": { "devDependencies": {
"mocha": "1.3.x", "mocha": "1.3.x",