perf: Reduce polygon and position function calls.
If we are using a standard identity function, we can skip some function
calls.
MODIFY
CHANGELOG.md
CHANGELOG.md
### 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

MODIFY
src/graphFeature.js
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;
};

MODIFY
src/heatmapFeature.js
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;

MODIFY
src/lineFeature.js
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])
},

MODIFY
src/pixelmapFeature.js
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
);

MODIFY
src/polygonFeature.js
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) {

MODIFY
src/quadFeature.js
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
);

MODIFY
src/trackFeature.js
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

MODIFY
src/transform.js
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/;
/**

MODIFY
src/util/common.js
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.

MODIFY
src/vectorFeature.js
src/vectorFeature.js
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

MODIFY
src/webgl/lineFeature.js
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);

MODIFY
src/webgl/polygonFeature.js
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

perf: Reduce polygon and position function calls.

If we are using a standard identity function, we can skip some function
calls.