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

[perf] cache resolved data element options #6579

Merged
merged 3 commits into from Oct 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/controllers/controller.bubble.js
Expand Up @@ -154,6 +154,11 @@ module.exports = DatasetController.extend({
datasetIndex: me.index
};

// In case values were cached (and thus frozen), we need to clone the values
if (me._cachedDataOpts === values) {
values = helpers.extend({}, values);
}

// Custom radius resolution
values.radius = resolve([
custom.radius,
Expand Down
22 changes: 19 additions & 3 deletions src/core/core.datasetController.js
Expand Up @@ -290,6 +290,7 @@ helpers.extend(DatasetController.prototype, {
_update: function(reset) {
var me = this;
me._configure();
me._cachedDataOpts = null;
me.update(reset);
},

Expand Down Expand Up @@ -389,9 +390,13 @@ helpers.extend(DatasetController.prototype, {
*/
_resolveDataElementOptions: function(element, index) {
var me = this;
var custom = element && element.custom;
var cached = me._cachedDataOpts;
if (cached && !custom) {
return cached;
}
var chart = me.chart;
var datasetOpts = me._config;
var custom = element.custom || {};
var options = chart.options.elements[me.dataElementType.prototype._type] || {};
var elementOptions = me._dataElementOptions;
var values = {};
Expand All @@ -405,14 +410,21 @@ helpers.extend(DatasetController.prototype, {
datasetIndex: me.index
};

// information about resolution
var info = {
cacheable: !custom
};

custom = custom || {};

if (helpers.isArray(elementOptions)) {
for (i = 0, ilen = elementOptions.length; i < ilen; ++i) {
key = elementOptions[i];
values[key] = resolve([
custom[key],
datasetOpts[key],
options[key]
], context, index);
], context, index, info);
}
} else {
keys = Object.keys(elementOptions);
Expand All @@ -423,10 +435,14 @@ helpers.extend(DatasetController.prototype, {
datasetOpts[elementOptions[key]],
datasetOpts[key],
options[key]
], context, index);
], context, index, info);
}
}

if (info.cacheable) {
me._cachedDataOpts = Object.freeze(values);
}

return values;
},

Expand Down
9 changes: 8 additions & 1 deletion src/helpers/helpers.options.js
Expand Up @@ -114,10 +114,12 @@ module.exports = {
* @param {object} [context] - If defined and the current value is a function, the value
* is called with `context` as first argument and the result becomes the new input.
* @param {number} [index] - If defined and the current value is an array, the value
* @param {object} [info] - object to return information about resolution in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd initialize info.cacheable to true in this method so that if you call resolve outside of _resolveDataElementOptions then info contains a usable value. Then in _resolveDataElementOptions you can check if (info.cacheable && !custom) instead of if (info.cacheable)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have to be something like

if (info && info.cacheable === undefined) {
  info.cacheable = true;
}

because resolve is called in a loop. But I think its not needed. You'd need to provide the info object anyway, so not a biggie to provide initial value too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, I like to keep the initialization (and any related if's) out of loop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just document it then?

* @param {boolean} [info.cacheable] - Should be initialized to true. Will be updated by this method.

* at `index` become the new input.
* @since 2.7.0
*/
resolve: function(inputs, context, index) {
resolve: function(inputs, context, index, info) {
var cacheable = true;
var i, ilen, value;

for (i = 0, ilen = inputs.length; i < ilen; ++i) {
Expand All @@ -127,11 +129,16 @@ module.exports = {
}
if (context !== undefined && typeof value === 'function') {
value = value(context);
cacheable = false;
}
if (index !== undefined && helpers.isArray(value)) {
value = value[index];
cacheable = false;
}
if (value !== undefined) {
if (info && !cacheable) {
info.cacheable = false;
}
return value;
}
}
Expand Down