From cec013ac9b3af2d60adf879b7e131d4e22c6467d Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 14 Jun 2022 17:10:05 -0400 Subject: [PATCH] perf: Speed up track feature. This reduces the amount of calculations needed to construct line features. --- src/lineFeature.js | 9 ++++++--- src/trackFeature.js | 4 +++- src/webgl/lineFeature.js | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/lineFeature.js b/src/lineFeature.js index 128cd9df68..5ad90e3c6d 100644 --- a/src/lineFeature.js +++ b/src/lineFeature.js @@ -46,9 +46,12 @@ var util = require('./util'); * divided by the sine of half the angle between segments, then a bevel join * is used instead. This is a single value that applies to all lines. If a * function, it is called with `(data)`. - * @property {boolean|function} [uniformLine=false] Boolean indicating if each - * line has a uniform style (uniform stroke color, opacity, and width). Can - * vary by line. + * @property {boolean|string|function} [uniformLine=false] Boolean indicating + * if each line has a uniform style (uniform stroke color, opacity, and + * width). Can vary by line. A value of `'drop'` will modify rendered + * vertex order by dropping duplicates and setting later values to zero + * opacity. This can be faster but makes it so updating the style array + * can no longer be used. * @property {number|function} [antialiasing] Antialiasing distance in pixels. * Values must be non-negative. A value greater than 1 will produce a * visible gradient. This is a single value that applies to all lines. diff --git a/src/trackFeature.js b/src/trackFeature.js index ed7e2f33df..4bed29c3ca 100644 --- a/src/trackFeature.js +++ b/src/trackFeature.js @@ -816,7 +816,9 @@ var trackFeature = function (arg) { { track: util.identityFunction, position: util.identityFunction, - time: (d, i) => (d.t !== undefined ? d.t : i) + time: (d, i) => (d.t !== undefined ? d.t : i), + uniformLine: 'drop', + closed: false }, arg.style === undefined ? {} : arg.style ); diff --git a/src/webgl/lineFeature.js b/src/webgl/lineFeature.js index e2c7fc7303..6cb7b3cf6c 100644 --- a/src/webgl/lineFeature.js +++ b/src/webgl/lineFeature.js @@ -269,6 +269,7 @@ var webgl_lineFeature = function (arg) { closedVal = closed[i]; firstPosIdx3 = posIdx3; maxj = lineItem.length + (closedVal === 2 ? 1 : 0); + let skipped = 0; for (j = 0; j < maxj; j += 1, posIdx3 += 3) { lidx = j; if (j === lineItem.length) { @@ -329,9 +330,17 @@ var webgl_lineFeature = function (arg) { } if (j) { + if (uniform === 'drop' && j > 1 && position[vert[0].pos] === position[vert[1].pos] && position[vert[0].pos + 1] === position[vert[1].pos + 1]) { + skipped += 1; + continue; + } /* zero out the z position. This can be changed if we handle it in * the shader. */ for (k = 0; k < orderLen; k += 1, dest += 1, dest3 += 3) { + if (uniform === 'drop' && vert[0].strokeOpacity <= 0 && vert[1].strokeOpacity <= 0) { + strokeOpacityBuf[dest] = -1; + continue; + } orderk0 = order[k][0]; v1 = vert[orderk0]; v2 = vert[1 - orderk0]; @@ -370,6 +379,11 @@ var webgl_lineFeature = function (arg) { } } } + if (skipped) { + for (k = 0; k < skipped * orderLen; k += 1, dest += 1, dest3 += 3) { + strokeOpacityBuf[dest] = -1; + } + } } if (!onlyStyle) {