diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae9089dfb..ac8a9c2a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ ### Improvements +- Reduce polygon and position function calls ([#1208](../../pull/1208)) + +## Version 1.8.7 + +### Improvements + - Add some code paths to reduce transform calls ([#1207](../../pull/1207)) ## Version 1.8.6 diff --git a/src/graphFeature.js b/src/graphFeature.js index 43ed02be9a..32ff9b09c3 100644 --- a/src/graphFeature.js +++ b/src/graphFeature.js @@ -80,7 +80,7 @@ var graphFeature = function (arg) { arg.style === undefined ? {} : arg.style); m_this.style(defaultStyle); - m_this.nodes(function (d) { return d; }); + m_this.nodes(util.identityFunction); return m_this; }; diff --git a/src/heatmapFeature.js b/src/heatmapFeature.js index 48d5011a3e..251d46911d 100644 --- a/src/heatmapFeature.js +++ b/src/heatmapFeature.js @@ -2,6 +2,7 @@ var $ = require('jquery'); var inherit = require('./inherit'); var feature = require('./feature'); var transform = require('./transform'); +var util = require('./util'); /** * Heatmap feature specification. @@ -79,7 +80,7 @@ var heatmapFeature = function (arg) { this.featureType = 'heatmap'; - m_position = arg.position || function (d) { return d; }; + m_position = arg.position || util.identityFunction; m_intensity = arg.intensity || function (d) { return 1; }; m_maxIntensity = arg.maxIntensity !== undefined ? arg.maxIntensity : null; m_minIntensity = arg.minIntensity !== undefined ? arg.minIntensity : null; diff --git a/src/lineFeature.js b/src/lineFeature.js index fd90fea833..62e4cbef68 100644 --- a/src/lineFeature.js +++ b/src/lineFeature.js @@ -438,8 +438,8 @@ var lineFeature = function (arg) { }); /* Set the reduced lines as the data and use simple accessors. */ - m_this.style('position', function (d) { return d; }); - m_this.style('line', function (d) { return d; }); + m_this.style('position', util.identityFunction); + m_this.style('line', util.identityFunction); m_this.data(data); return m_this; }; @@ -465,7 +465,7 @@ var lineFeature = function (arg) { // Values of 2 and above appear smoothest. antialiasing: 2.0, closed: false, - line: function (d) { return d; }, + line: util.identityFunction, position: (d) => Array.isArray(d) ? {x: d[0], y: d[1], z: d[2] || 0} : d, origin: (p) => (p.length >= 3 ? p.slice(0, 3) : [0, 0, 0]) }, diff --git a/src/pixelmapFeature.js b/src/pixelmapFeature.js index 24be0dee0e..5553fca09c 100644 --- a/src/pixelmapFeature.js +++ b/src/pixelmapFeature.js @@ -285,7 +285,7 @@ var pixelmapFeature = function (arg) { a: 1 }; }, - position: function (d) { return d; } + position: util.identityFunction }, arg.style === undefined ? {} : arg.style ); diff --git a/src/polygonFeature.js b/src/polygonFeature.js index 5ec51875ab..b85fbd76f8 100644 --- a/src/polygonFeature.js +++ b/src/polygonFeature.js @@ -634,8 +634,8 @@ var polygonFeature = function (arg) { }); /* Set the reduced polgons as the data and use simple accessors. */ - m_this.style('position', function (d) { return d; }); - m_this.style('polygon', function (d) { return d; }); + m_this.style('position', util.identityFunction); + m_this.style('polygon', util.identityFunction); m_this.data(data); return m_this; }; @@ -719,7 +719,7 @@ var polygonFeature = function (arg) { strokeStyle: 'solid', strokeColor: {r: 0.0, g: 1.0, b: 1.0}, strokeOpacity: 1.0, - polygon: function (d) { return d; }, + polygon: util.identityFunction, position: (d) => Array.isArray(d) ? {x: d[0], y: d[1], z: d[2] || 0} : d, origin: (items) => { for (let i = 0; i < items.length; i += 1) { diff --git a/src/quadFeature.js b/src/quadFeature.js index 9b357226c9..f8a135bebd 100644 --- a/src/quadFeature.js +++ b/src/quadFeature.js @@ -699,7 +699,7 @@ var quadFeature = function (arg) { previewImage: null, image: function (d) { return d.image; }, video: function (d) { return d.video; }, - position: function (d) { return d; } + position: util.identityFunction }, arg.style === undefined ? {} : arg.style ); diff --git a/src/trackFeature.js b/src/trackFeature.js index 83f809451d..ed7e2f33df 100644 --- a/src/trackFeature.js +++ b/src/trackFeature.js @@ -814,8 +814,8 @@ var trackFeature = function (arg) { true, {}, { - track: (d) => d, - position: (d) => d, + track: util.identityFunction, + position: util.identityFunction, time: (d, i) => (d.t !== undefined ? d.t : i) }, arg.style === undefined ? {} : arg.style diff --git a/src/transform.js b/src/transform.js index de91959751..ecbd06e4b8 100644 --- a/src/transform.js +++ b/src/transform.js @@ -19,7 +19,7 @@ var maxTransformCacheSize = 10; /* A RegExp to detect if two transforms only different by the middle axis's * direction. */ -var axisPattern = /^(.* |)\\+axis=e(n|s)u(| .*)$/; +var axisPattern = /^(.* |)\+axis=e(n|s)u(| .*)$/; var affinePattern = /(^|\s)\+(s[1-3][1-3]|[xyz]off)=\S/; /** diff --git a/src/util/common.js b/src/util/common.js index 52faa0f6ff..065c5067e0 100644 --- a/src/util/common.js +++ b/src/util/common.js @@ -1443,6 +1443,17 @@ var util = { return value !== null && value !== undefined && (type === 'object' || type === 'function'); }, + /** + * Return the first value passed to the function. Using this function for + * identity calls can allow some code to bypass making such a call entirely. + * + * @param {*} d Any value. + * @returns {*} The passed value. + */ + identityFunction: function (d) { + return d; + }, + /////////////////////////////////////////////////////////////////////////// /* * Utility member properties. diff --git a/src/vectorFeature.js b/src/vectorFeature.js index c110cc49ce..a27ba27167 100644 --- a/src/vectorFeature.js +++ b/src/vectorFeature.js @@ -1,5 +1,6 @@ var inherit = require('./inherit'); var feature = require('./feature'); +var util = require('./util'); /** * Object specification for a graph feature. @@ -120,7 +121,7 @@ var vectorFeature = function (arg) { originStyle: 'none', endStyle: 'arrow', origin: {x: 0, y: 0, z: 0}, - delta: function (d) { return d; }, + delta: util.identityFunction, scale: null // size scaling factor (null -> renderer decides) }, arg.style === undefined ? {} : arg.style diff --git a/src/webgl/lineFeature.js b/src/webgl/lineFeature.js index e59e0c62f6..8dd5c1a7c8 100644 --- a/src/webgl/lineFeature.js +++ b/src/webgl/lineFeature.js @@ -178,6 +178,7 @@ var webgl_lineFeature = function (arg) { if (!onlyStyle) { var position = [], posFunc = m_this.position(); + posFunc = posFunc === util.identityFunction ? null : posFunc; lineItemList = new Array(data.length); closed = new Array(data.length); for (i = 0; i < data.length; i += 1) { @@ -189,7 +190,7 @@ var webgl_lineFeature = function (arg) { } numSegments += lineItem.length - 1; for (j = 0; j < lineItem.length; j += 1) { - pos = posFunc(lineItem[j], j, d, i); + pos = posFunc ? posFunc(lineItem[j], j, d, i) : lineItem[j]; position.push(pos.x); position.push(simpleInverse ? -pos.y : pos.y); position.push(pos.z || 0.0); diff --git a/src/webgl/polygonFeature.js b/src/webgl/polygonFeature.js index 8ae07ea838..1f7a1972e8 100644 --- a/src/webgl/polygonFeature.js +++ b/src/webgl/polygonFeature.js @@ -98,11 +98,13 @@ var webgl_polygonFeature = function (arg) { if (!onlyStyle) { posFunc = m_this.style.get('position'); + posFunc = posFunc === util.identityFunction ? null : posFunc; polyFunc = m_this.style.get('polygon'); + polyFunc = polyFunc === util.identityFunction ? null : polyFunc; m_this.data().forEach(function (item, itemIndex) { var polygon, outer, geometry, c; - polygon = polyFunc(item, itemIndex); + polygon = polyFunc ? polyFunc(item, itemIndex) : item; if (!polygon) { return; } @@ -116,7 +118,7 @@ var webgl_polygonFeature = function (arg) { * test). */ geometry = new Array(outer.length * 3); for (i = d3 = 0; i < outer.length; i += 1, d3 += 3) { - c = posFunc(outer[i], i, item, itemIndex); + c = posFunc ? posFunc(outer[i], i, item, itemIndex) : outer[i]; geometry[d3] = c.x; geometry[d3 + 1] = simpleInverse ? -c.y : c.y; // ignore the z values until we support them @@ -133,7 +135,7 @@ var webgl_polygonFeature = function (arg) { original = original.concat(hole); geometry.holes.push(d3 / 3); for (i = 0; i < hole.length; i += 1, d3 += 3) { - c = posFunc(hole[i], i, item, itemIndex); + c = posFunc ? posFunc(hole[i], i, item, itemIndex) : hole[i]; geometry.vertices[d3] = c.x; geometry.vertices[d3 + 1] = simpleInverse ? -c.y : c.y; // ignore the z values until we support them