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

Improve stacking performance and footprint #6762

Merged
merged 3 commits into from
Nov 18, 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
26 changes: 13 additions & 13 deletions src/controllers/controller.bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ module.exports = DatasetController.extend({
* @private
*/
_parseObjectData: function(meta, data, start, count) {
var xScale = this.getScaleForId(meta.xAxisID);
var yScale = this.getScaleForId(meta.yAxisID);
var parsed = [];
const xScale = meta.xScale;
const yScale = meta.yScale;
const parsed = [];
var i, ilen, item, obj;
for (i = start, ilen = start + count; i < ilen; ++i) {
obj = data[i];
Expand Down Expand Up @@ -95,8 +95,8 @@ module.exports = DatasetController.extend({
_getLabelAndValue: function(index) {
const me = this;
const meta = me._cachedMeta;
const xScale = me.getScaleForId(meta.xAxisID);
const yScale = me.getScaleForId(meta.yAxisID);
const xScale = meta.xScale;
const yScale = meta.yScale;
const parsed = me._getParsed(index);
const x = xScale.getLabelForValue(parsed[xScale.id]);
const y = yScale.getLabelForValue(parsed[yScale.id]);
Expand Down Expand Up @@ -126,14 +126,14 @@ module.exports = DatasetController.extend({
* @protected
*/
updateElement: function(point, index, reset) {
var me = this;
var meta = me.getMeta();
var xScale = me.getScaleForId(meta.xAxisID);
var yScale = me.getScaleForId(meta.yAxisID);
var options = me._resolveDataElementOptions(index);
var parsed = !reset && me._getParsed(index);
var x = reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(parsed[xScale.id]);
var y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed[yScale.id]);
const me = this;
const meta = me._cachedMeta;
const xScale = meta.xScale;
const yScale = meta.yScale;
const options = me._resolveDataElementOptions(index);
const parsed = !reset && me._getParsed(index);
const x = reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(parsed[xScale.id]);
const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed[yScale.id]);

point._options = options;
point._model = {
Expand Down
21 changes: 9 additions & 12 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ module.exports = DatasetController.extend({
var showLine = me._showLine = valueOrDefault(config.showLine, options.showLines);
var i, ilen;

me._xScale = me.getScaleForId(meta.xAxisID);
me._yScale = me.getScaleForId(meta.yAxisID);

// Update Line
if (showLine) {
// Data
Expand All @@ -106,15 +103,15 @@ module.exports = DatasetController.extend({
},

updateElement: function(point, index, reset) {
var me = this;
var meta = me.getMeta();
var xScale = me._xScale;
benmccann marked this conversation as resolved.
Show resolved Hide resolved
var yScale = me._yScale;
var stacked = meta._stacked;
var parsed = me._getParsed(index);
var options = me._resolveDataElementOptions(index);
var x = xScale.getPixelForValue(parsed[xScale.id]);
var y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);
const me = this;
const meta = me._cachedMeta;
const xScale = meta.xScale;
const yScale = meta.yScale;
const stacked = meta._stacked;
const parsed = me._getParsed(index);
const options = me._resolveDataElementOptions(index);
const x = xScale.getPixelForValue(parsed[xScale.id]);
const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);

// Utility
point._options = options;
Expand Down
123 changes: 69 additions & 54 deletions src/core/core.datasetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,45 @@ function getUserBounds(scale) {
max: maxDefined ? max : Number.POSITIVE_INFINITY
};
}

function getOrCreateStack(stacks, stackKey, indexValue) {
const subStack = stacks[stackKey] || (stacks[stackKey] = {});
return subStack[indexValue] || (subStack[indexValue] = {});
}

function updateStacks(controller, parsed) {
const chart = controller.chart;
const meta = controller._cachedMeta;
const stacks = chart._stacks || (chart._stacks = {}); // map structure is {stackKey: {datasetIndex: value}}
const xScale = meta.xScale;
const yScale = meta.yScale;
const xId = xScale.id;
const yId = yScale.id;
const xStacked = isStacked(xScale, meta);
const yStacked = isStacked(yScale, meta);
const xKey = yStacked && getStackKey(xScale, yScale, meta);
const yKey = xStacked && getStackKey(yScale, xScale, meta);
const ilen = parsed.length;
const datasetIndex = meta.index;
let stack;

for (let i = 0; i < ilen; ++i) {
const item = parsed[i];
const x = item[xId];
const y = item[yId];
const itemStacks = item._stacks || (item._stacks = {});

if (yStacked) {
stack = itemStacks[yId] = getOrCreateStack(stacks, xKey, x);
stack[datasetIndex] = y;
}
if (xStacked) {
stack = itemStacks[xId] = getOrCreateStack(stacks, yKey, y);
stack[datasetIndex] = x;
}
}
}

// Base class for all dataset controllers (line, bar, etc)
var DatasetController = function(chart, datasetIndex) {
this.initialize(chart, datasetIndex);
Expand Down Expand Up @@ -257,12 +296,14 @@ helpers.extend(DatasetController.prototype, {
},

linkScales: function() {
var chart = this.chart;
var meta = this._cachedMeta;
var dataset = this.getDataset();

meta.xAxisID = dataset.xAxisID || getFirstScaleId(chart, 'x');
meta.yAxisID = dataset.yAxisID || getFirstScaleId(chart, 'y');
const me = this;
const chart = me.chart;
const meta = me._cachedMeta;
const dataset = me.getDataset();
const xid = meta.xAxisID = dataset.xAxisID || getFirstScaleId(chart, 'x');
const yid = meta.yAxisID = dataset.yAxisID || getFirstScaleId(chart, 'y');
meta.xScale = me.getScaleForId(xid);
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a really good idea!

Copy link
Contributor

Choose a reason for hiding this comment

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

do you think we should store iScale and vScale here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably

meta.yScale = me.getScaleForId(yid);
},

getDataset: function() {
Expand Down Expand Up @@ -444,20 +485,12 @@ helpers.extend(DatasetController.prototype, {
*/
_parse: function(start, count) {
const me = this;
const chart = me.chart;
const meta = me._cachedMeta;
const data = me._data;
const stacks = chart._stacks || (chart._stacks = {}); // map structure is {stackKey: {datasetIndex: value}}
const xScale = me._getIndexScale();
const yScale = me._getValueScale();
const xId = xScale.id;
const yId = yScale.id;
const xStacked = isStacked(xScale, meta);
const yStacked = isStacked(yScale, meta);
const xKey = yStacked && getStackKey(xScale, yScale, meta);
const yKey = xStacked && getStackKey(yScale, xScale, meta);
const stacked = xStacked || yStacked;
var i, ilen, parsed, stack, item, x, y;
const iScale = me._getIndexScale();
const vScale = me._getValueScale();
const stacked = isStacked(iScale, meta) || isStacked(vScale, meta);
var i, ilen, parsed;

if (helpers.isArray(data[start])) {
parsed = me._parseArrayData(meta, data, start, count);
Expand All @@ -467,34 +500,16 @@ helpers.extend(DatasetController.prototype, {
parsed = me._parsePrimitiveData(meta, data, start, count);
}

function storeStack(stackKey, indexValue, scaleId, value) {
stackKey += '.' + indexValue;
item._stackKeys[scaleId] = stackKey;
stack = stacks[stackKey] || (stacks[stackKey] = {});
stack[meta.index] = value;
}

for (i = 0, ilen = parsed.length; i < ilen; ++i) {
item = parsed[i];
meta.data[start + i]._parsed = item;

if (stacked) {
item._stackKeys = {};
x = item[xId];
y = item[yId];

if (yStacked) {
storeStack(xKey, x, yId, y);
}
if (xStacked) {
storeStack(yKey, y, xId, x);
}
}
meta.data[start + i]._parsed = parsed[i];
}
if (stacked) {
updateStacks(me, parsed);
}

xScale._invalidateCaches();
if (yScale !== xScale) {
yScale._invalidateCaches();
iScale._invalidateCaches();
if (vScale !== iScale) {
vScale._invalidateCaches();
}
},

Expand Down Expand Up @@ -538,9 +553,9 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
_parseArrayData: function(meta, data, start, count) {
var xScale = this.getScaleForId(meta.xAxisID);
var yScale = this.getScaleForId(meta.yAxisID);
var parsed = [];
const xScale = meta.xScale;
const yScale = meta.yScale;
const parsed = [];
var i, ilen, item, arr;
for (i = start, ilen = start + count; i < ilen; ++i) {
arr = data[i];
Expand All @@ -564,9 +579,9 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
_parseObjectData: function(meta, data, start, count) {
var xScale = this.getScaleForId(meta.xAxisID);
var yScale = this.getScaleForId(meta.yAxisID);
var parsed = [];
const xScale = meta.xScale;
const yScale = meta.yScale;
const parsed = [];
var i, ilen, item, obj;
for (i = start, ilen = start + count; i < ilen; ++i) {
obj = data[i];
Expand Down Expand Up @@ -598,7 +613,7 @@ helpers.extend(DatasetController.prototype, {
var value = parsed[scale.id];
var stack = {
keys: getSortedDatasetIndices(chart, true),
values: chart._stacks[parsed._stackKeys[scale.id]]
values: parsed._stacks[scale.id]
};
return applyStack(stack, value, meta.index);
},
Expand All @@ -611,7 +626,6 @@ helpers.extend(DatasetController.prototype, {
var meta = this._cachedMeta;
var metaData = meta.data;
var ilen = metaData.length;
var stacks = chart._stacks || (chart._stacks = {});
var max = Number.NEGATIVE_INFINITY;
var stacked = canStack && meta._stacked;
var indices = getSortedDatasetIndices(chart, true);
Expand All @@ -633,7 +647,7 @@ helpers.extend(DatasetController.prototype, {
if (stacked) {
stack = {
keys: indices,
values: stacks[parsed._stackKeys[scale.id]]
values: parsed._stacks[scale.id]
};
value = applyStack(stack, value, meta.index, true);
}
Expand Down Expand Up @@ -722,11 +736,12 @@ helpers.extend(DatasetController.prototype, {
* @private
*/
_update: function(reset) {
var me = this;
const me = this;
const meta = me._cachedMeta;
me._configure();
me._cachedDataOpts = null;
me.update(reset);
me._cachedMeta._clip = toClip(helpers.valueOrDefault(me._config.clip, defaultClip(me._xScale, me._yScale, me._getMaxOverflow())));
meta._clip = toClip(helpers.valueOrDefault(me._config.clip, defaultClip(meta.xScale, meta.yScale, me._getMaxOverflow())));
me._cacheScaleStackStatus();
},

Expand Down