Skip to content

Commit

Permalink
Merge pull request #1208 from OpenGeoscience/identity-function-calls
Browse files Browse the repository at this point in the history
perf: Reduce polygon and position function calls
  • Loading branch information
manthey authored May 18, 2022
2 parents 7b38197 + 11df13e commit e4988e0
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/graphFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
3 changes: 2 additions & 1 deletion src/heatmapFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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])
},
Expand Down
2 changes: 1 addition & 1 deletion src/pixelmapFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ var pixelmapFeature = function (arg) {
a: 1
};
},
position: function (d) { return d; }
position: util.identityFunction
},
arg.style === undefined ? {} : arg.style
);
Expand Down
6 changes: 3 additions & 3 deletions src/polygonFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/quadFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions src/trackFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/;

/**
Expand Down
11 changes: 11 additions & 0 deletions src/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/vectorFeature.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var inherit = require('./inherit');
var feature = require('./feature');
var util = require('./util');

/**
* Object specification for a graph feature.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/webgl/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/webgl/polygonFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e4988e0

Please sign in to comment.