Skip to content

Commit 6bc6630

Browse files
kurkleetimberg
authored andcommitted
[perf] cache resolved data element options (#6579)
* [perf] cache resolved data element options * Address review comments * Move uninitialized variables, update comments
1 parent 959ea08 commit 6bc6630

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/controllers/controller.bubble.js

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ module.exports = DatasetController.extend({
154154
datasetIndex: me.index
155155
};
156156

157+
// In case values were cached (and thus frozen), we need to clone the values
158+
if (me._cachedDataOpts === values) {
159+
values = helpers.extend({}, values);
160+
}
161+
157162
// Custom radius resolution
158163
values.radius = resolve([
159164
custom.radius,

src/core/core.datasetController.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ helpers.extend(DatasetController.prototype, {
290290
_update: function(reset) {
291291
var me = this;
292292
me._configure();
293+
me._cachedDataOpts = null;
293294
me.update(reset);
294295
},
295296

@@ -389,13 +390,16 @@ helpers.extend(DatasetController.prototype, {
389390
*/
390391
_resolveDataElementOptions: function(element, index) {
391392
var me = this;
393+
var custom = element && element.custom;
394+
var cached = me._cachedDataOpts;
395+
if (cached && !custom) {
396+
return cached;
397+
}
392398
var chart = me.chart;
393399
var datasetOpts = me._config;
394-
var custom = element.custom || {};
395400
var options = chart.options.elements[me.dataElementType.prototype._type] || {};
396401
var elementOptions = me._dataElementOptions;
397402
var values = {};
398-
var keys, i, ilen, key;
399403

400404
// Scriptable options
401405
var context = {
@@ -405,14 +409,21 @@ helpers.extend(DatasetController.prototype, {
405409
datasetIndex: me.index
406410
};
407411

412+
// `resolve` sets cacheable to `false` if any option is indexed or scripted
413+
var info = {cacheable: !custom};
414+
415+
var keys, i, ilen, key;
416+
417+
custom = custom || {};
418+
408419
if (helpers.isArray(elementOptions)) {
409420
for (i = 0, ilen = elementOptions.length; i < ilen; ++i) {
410421
key = elementOptions[i];
411422
values[key] = resolve([
412423
custom[key],
413424
datasetOpts[key],
414425
options[key]
415-
], context, index);
426+
], context, index, info);
416427
}
417428
} else {
418429
keys = Object.keys(elementOptions);
@@ -423,10 +434,14 @@ helpers.extend(DatasetController.prototype, {
423434
datasetOpts[elementOptions[key]],
424435
datasetOpts[key],
425436
options[key]
426-
], context, index);
437+
], context, index, info);
427438
}
428439
}
429440

441+
if (info.cacheable) {
442+
me._cachedDataOpts = Object.freeze(values);
443+
}
444+
430445
return values;
431446
},
432447

src/helpers/helpers.options.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ module.exports = {
115115
* is called with `context` as first argument and the result becomes the new input.
116116
* @param {number} [index] - If defined and the current value is an array, the value
117117
* at `index` become the new input.
118+
* @param {object} [info] - object to return information about resolution in
119+
* @param {boolean} [info.cacheable] - Will be set to `false` if option is not cacheable.
118120
* @since 2.7.0
119121
*/
120-
resolve: function(inputs, context, index) {
122+
resolve: function(inputs, context, index, info) {
123+
var cacheable = true;
121124
var i, ilen, value;
122125

123126
for (i = 0, ilen = inputs.length; i < ilen; ++i) {
@@ -127,11 +130,16 @@ module.exports = {
127130
}
128131
if (context !== undefined && typeof value === 'function') {
129132
value = value(context);
133+
cacheable = false;
130134
}
131135
if (index !== undefined && helpers.isArray(value)) {
132136
value = value[index];
137+
cacheable = false;
133138
}
134139
if (value !== undefined) {
140+
if (info && !cacheable) {
141+
info.cacheable = false;
142+
}
135143
return value;
136144
}
137145
}

0 commit comments

Comments
 (0)