Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[] add a cache to style-layer-index so we don't recalculate keys #8122

Merged
merged 3 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/style-spec/group_by_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ export default groupByLayout;
*
* @private
* @param {Array<Layer>} layers
* @param {Object} [cachedKeys] - an object to keep already calculated keys.
* @returns {Array<Array<Layer>>}
*/
function groupByLayout(layers) {
function groupByLayout(layers, cachedKeys) {
const groups = {};

for (let i = 0; i < layers.length; i++) {
const k = getKey(layers[i]);

const k = (cachedKeys && cachedKeys[layers[i].id] ) || getKey(layers[i]);
// update the cache if there is one
if ( cachedKeys )
cachedKeys[layers[i].id] = k;

let group = groups[k];
if (!group) {
group = groups[k] = [];
Expand Down
7 changes: 6 additions & 1 deletion src/style/style_layer_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export type Family<Layer: TypedStyleLayer> = Array<Layer>;

class StyleLayerIndex {
familiesBySource: { [source: string]: { [sourceLayer: string]: Array<Family<*>> } };
keyCache: { [source: string]: string };

_layerConfigs: LayerConfigs;
_layers: { [string]: StyleLayer };

constructor(layerConfigs: ?Array<LayerSpecification>) {
this.keyCache = {};
if (layerConfigs) {
this.replace(layerConfigs);
}
Expand All @@ -37,15 +39,18 @@ class StyleLayerIndex {

const layer = this._layers[layerConfig.id] = createStyleLayer(layerConfig);
layer._featureFilter = featureFilter(layer.filter);
if (this.keyCache[layerConfig.id])
delete this.keyCache[layerConfig.id];
}
for (const id of removedIds) {
delete this.keyCache[id];
delete this._layerConfigs[id];
delete this._layers[id];
}

this.familiesBySource = {};

const groups = groupByLayout(values(this._layerConfigs));
const groups = groupByLayout(values(this._layerConfigs), this.keyCache);

for (const layerConfigs of groups) {
const layers = layerConfigs.map((layerConfig) => this._layers[layerConfig.id]);
Expand Down
10 changes: 5 additions & 5 deletions test/unit/style-spec/group_by_layout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ t('group layers whose ref properties are identical', (t) => {
'id': 'child',
'type': 'line'
};
t.deepEqual(group([a, b]), [[a, b]]);
t.equal(group([a, b])[0][0], a);
t.equal(group([a, b])[0][1], b);
t.deepEqual(group([a, b], {}), [[a, b]]);
t.equal(group([a, b], {})[0][0], a);
t.equal(group([a, b], {})[0][1], b);
t.end();
});

Expand All @@ -26,7 +26,7 @@ t('group does not group unrelated layers', (t) => {
'id': 'child',
'type': 'fill'
}
]), [
], {}), [
[{
'id': 'parent',
'type': 'line'
Expand All @@ -50,7 +50,7 @@ t('group works even for differing layout key orders', (t) => {
'type': 'line',
'layout': {'b': 2, 'a': 1}
}
]), [[
], {}), [[
{
'id': 'parent',
'type': 'line',
Expand Down