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

Reduce object creation during parsing #6758

Merged
merged 4 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
* `helpers.removeEvent`
* `helpers.roundedRect`
* `helpers.scaleMerge`
* `Scale.getRightValue`
* `Scale.mergeTicksOptions`
* `Scale.ticksAsNumbers`
* `Chart.Controller`
* `Chart.chart.chart`
* `Chart.types`
* `Line.calculatePointY`
* `DatasetController.addElementAndReset`
* `Element.getArea`
* `Element.height`
* `Element.inLabelRange`
* Made `scale.handleDirectionalChanges` private
* Made `scale.tickValues` private
* `Line.calculatePointY`
* `Scale.getRightValue`
* `Scale.mergeTicksOptions`
* `Scale.ticksAsNumbers`
* `Scale.handleDirectionalChanges` is now private
* `Scale.tickValues` is now private

#### Removal of private APIs

Expand All @@ -95,6 +96,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
* `helpers.log10` was renamed to `helpers.math.log10`
* `Chart.Animation.animationObject` was renamed to `Chart.Animation`
* `Chart.Animation.chartInstance` was renamed to `Chart.Animation.chart`
* `DatasetController.createMetaData` and `DatasetController.createMetaDataset` were replaced with `DatasetController.createMeta`
* `TimeScale.getLabelCapacity` was renamed to `TimeScale._getLabelCapacity`
* `TimeScale.tickFormatFunction` was renamed to `TimeScale._tickFormatFunction`
* `TimeScale.getPixelForOffset` was renamed to `TimeScale._getPixelForOffset`
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = DatasetController.extend({
var metaData = this.getMeta().data;
var i, ilen;
for (i = start, ilen = start + count; i < ilen; ++i) {
metaData[i]._val = +data[i];
metaData[i]._parsed = +data[i];
}
},

Expand Down Expand Up @@ -232,7 +232,7 @@ module.exports = DatasetController.extend({
var centerY = (chartArea.top + chartArea.bottom) / 2;
var startAngle = opts.rotation; // non reset case handled later
var endAngle = opts.rotation; // non reset case handled later
var circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(arc._val * opts.circumference / DOUBLE_PI);
var circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(arc._parsed * opts.circumference / DOUBLE_PI);
var innerRadius = reset && animationOpts.animateScale ? 0 : me.innerRadius;
var outerRadius = reset && animationOpts.animateScale ? 0 : me.outerRadius;
var options = arc._options || {};
Expand Down Expand Up @@ -276,7 +276,7 @@ module.exports = DatasetController.extend({
var value;

helpers.each(metaData, function(arc) {
value = arc ? arc._val : NaN;
value = arc ? arc._parsed : NaN;
if (!isNaN(value) && !arc.hidden) {
total += Math.abs(value);
}
Expand Down
35 changes: 11 additions & 24 deletions src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,9 @@ helpers.extend(DatasetController.prototype, {
}
},

createMetaDataset: function() {
var me = this;
var type = me.datasetElementType;
createMeta: function(type) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
return type && new type({
_ctx: me.chart.ctx
});
},

createMetaData: function() {
var me = this;
var type = me.dataElementType;
return type && new type({
_ctx: me.chart.ctx,
_parsed: {}
_ctx: this.chart.ctx
});
},

Expand Down Expand Up @@ -411,16 +400,10 @@ helpers.extend(DatasetController.prototype, {
data = me._data;

for (i = 0, ilen = data.length; i < ilen; ++i) {
metaData[i] = metaData[i] || me.createMetaData();
metaData[i] = metaData[i] || me.createMeta(me.dataElementType);
}

meta.dataset = meta.dataset || me.createMetaDataset();
},

addElementAndReset: function(index) {
var element = this.createMetaData();
this._cachedMeta.data.splice(index, 0, element);
this.updateElement(element, index, true);
meta.dataset = meta.dataset || me.createMeta(me.datasetElementType);
},

buildOrUpdateElements: function() {
Expand Down Expand Up @@ -973,10 +956,14 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
insertElements: function(start, count) {
for (var i = 0; i < count; ++i) {
this.addElementAndReset(start + i);
const me = this;
var i;
for (i = start; i < start + count; ++i) {
const element = me.createMeta(me.dataElementType);
me._cachedMeta.data.splice(i, 0, element);
me._parse(i, 1);
Copy link
Member

Choose a reason for hiding this comment

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

Sould be more efficient to first create all, splice once, parse once, update each

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had been debating that and wasn't sure which would be more efficient. The splice is clearly more efficient the way you suggested, but we're now looping over the elements multiple times, so it wasn't clear to me which would be faster. I've updated it the way you suggested though

me.updateElement(element, i, true);
}
this._parse(start, count);
},

/**
Expand Down