Skip to content

Commit 7555752

Browse files
authored
feat: optimize gllayer checkFeaturesVisibleChange performance (#2684)
* feat:optimize gllayer checkFeaturesVisibleChange performance * updates * updates * updates
1 parent a3597fb commit 7555752

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

packages/vt/src/layer/vector/Vector3DLayerRenderer.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ const prefix = (SYMBOL_PREFIX + '').trim();
5454
const KEY_IDX_NAME = (KEY_IDX + '').trim();
5555
let EMPTY_POSITION = new Float32Array(1);
5656
const EMPTY_ARRAY = [];
57+
const TEMP_ARRAY = [];
5758

5859
class Vector3DLayerRenderer extends CanvasCompatible(LayerAbstractRenderer) {
5960
constructor(...args) {
6061
super(...args);
6162
this.features = {};
63+
this.featuresArray = [];
64+
this.featuresChanged = false;
6265
this._geometries = {};
6366
this._counter = 0;
6467
this._allFeatures = {};
@@ -100,15 +103,23 @@ class Vector3DLayerRenderer extends CanvasCompatible(LayerAbstractRenderer) {
100103
}
101104

102105
_checkFeaturesVisibleChange() {
103-
const features = this.features;
104-
if (!features) {
106+
if (this.featuresChanged) {
107+
this.featuresArray = Object.values(this.features);
108+
this.featuresChanged = false;
109+
}
110+
const featuresArray = this.featuresArray;
111+
if (!featuresArray) {
105112
return this;
106113
}
107114
//实时检测feature的visible变化
108-
for (const id in features) {
109-
let feats = features[id] || [];
115+
for (let m = 0, len = featuresArray.length; m < len; m++) {
116+
let feats = featuresArray[m];
117+
if (!feats) {
118+
continue;
119+
}
110120
if (!Array.isArray(feats)) {
111-
feats = [feats];
121+
TEMP_ARRAY[0] = feats;
122+
feats = TEMP_ARRAY;
112123
}
113124
if (!feats.length) {
114125
continue;
@@ -1082,6 +1093,7 @@ class Vector3DLayerRenderer extends CanvasCompatible(LayerAbstractRenderer) {
10821093
this._removeFeatures(uid);
10831094
}
10841095
this.features[uid] = convertToFeature(geo, this._kidGen, this.features[uid]);
1096+
this.featuresChanged = true;
10851097
const feas = this.features[uid];
10861098
this._refreshFeatures(feas, uid);
10871099
this._geometries[uid] = geo;
@@ -1271,6 +1283,7 @@ class Vector3DLayerRenderer extends CanvasCompatible(LayerAbstractRenderer) {
12711283
delete this._geometries[uid];
12721284
this._removeFeatures(uid);
12731285
delete this.features[uid];
1286+
this.featuresChanged = true;
12741287
}
12751288
}
12761289
this.markRebuild();

packages/vt/src/layer/vector/util/convert_to_feature.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extend, hasOwn } from '../../../common/Util';
1+
import { extend, hasOwn, isNil } from '../../../common/Util';
22
import * as maptalks from 'maptalks';
33
import { KEY_IDX } from '../../../common/Constant';
44
import { LINE_GRADIENT_PROP_KEY } from './symbols';
@@ -12,9 +12,19 @@ const GRADIENT_PROP_KEY = (LINE_GRADIENT_PROP_KEY + '').trim();
1212

1313
function watchGeoVisible(featue, symbol, geo) {
1414
//geo 的visible受options.visible 和style控制
15-
const styleFn = maptalks.MapboxUtil.loadGeoSymbol(symbol, geo);
15+
//symbol里是否有visible和opacity
16+
let hasVisibleProp = !isNil(symbol.visible), hasOpacityProp = !isNil(symbol.opacity);
17+
let styleFn = symbol;
18+
if (hasVisibleProp || hasOpacityProp) {
19+
styleFn = maptalks.MapboxUtil.loadGeoSymbol(symbol, geo);
20+
}
1621
Object.defineProperty(featue, 'getVisible', {
1722
get: function () {
23+
//样式里没有visible和opacity
24+
if (!hasVisibleProp && (!hasOpacityProp)) {
25+
const visible = geo.options.visible;
26+
return visible;
27+
}
1828
//实时检测geo的可见性,feature上的值是静态值,不应该检测其值
1929
const isVisible = geo.isVisible();
2030
if (!isVisible) {

0 commit comments

Comments
 (0)