Skip to content

Misc perf improvements #1772

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

Merged
merged 14 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions devtools/test_dashboard/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var credentials = require('../../build/credentials.json');
var Lib = require('@src/lib');
var d3 = Plotly.d3;

require('./perf');
Copy link
Contributor

Choose a reason for hiding this comment

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

cc @rreusser would be nice to have something like that in https://github.com/rreusser/plotly-mock-viewer

Moreover, @dfcreative's brilliant ✨ https://www.npmjs.com/package/fps-indicator ✨ would be a nice addition to our dev tools.


// Our gracious testing object
var Tabs = {

Expand Down
54 changes: 54 additions & 0 deletions devtools/test_dashboard/perf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

/*
* timeit: tool for performance testing
* f: function to be tested
* n: number of timing runs
* nchunk: optional number of repetitions per timing run - useful if
* the function is very fast. Note though that if arg is a function
* it will not be re-evaluated within the chunk, only before each chunk.
* arg: optional argument to the function. Can be a function itself
* to provide a changing input to f
*/
window.timeit = function(f, n, nchunk, arg) {
var times = new Array(n);
var totalTime = 0;
var _arg;
var t0, t1, dt;

for(var i = 0; i < n; i++) {
if(typeof arg === 'function') _arg = arg();
else _arg = arg;

if(nchunk) {
t0 = performance.now();
for(var j = 0; j < nchunk; j++) { f(_arg); }
t1 = performance.now();
dt = (t1 - t0) / nchunk;
}
else {
t0 = performance.now();
f(_arg);
t1 = performance.now();
dt = t1 - t0;
}

times[i] = dt;
totalTime += dt;
}

var first = (times[0]).toFixed(4);
var last = (times[n - 1]).toFixed(4);
times.sort();
var min = (times[0]).toFixed(4);
var max = (times[n - 1]).toFixed(4);
var median = (times[Math.ceil(n / 2)]).toFixed(4);
var mean = (totalTime / n).toFixed(4);
console.log((f.name || 'function') + ' timing (ms) - min: ' + min +
' max: ' + max +
' median: ' + median +
' mean: ' + mean +
' first: ' + first +
' last: ' + last
);
};
9 changes: 6 additions & 3 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
fontColor: hoverFont.color
}, {
container: fullLayout._hoverlayer.node(),
outerContainer: fullLayout._paper.node()
outerContainer: fullLayout._paper.node(),
gd: gd
});
})
.on('mouseout', function() {
Expand Down Expand Up @@ -214,7 +215,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
}[options.align] || 'middle'
});

svgTextUtils.convertToTspans(s, drawGraphicalElements);
svgTextUtils.convertToTspans(s, gd, drawGraphicalElements);
return s;
}

Expand Down Expand Up @@ -554,6 +555,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
// (head/tail/text) all together
dragElement.init({
element: arrowDrag.node(),
gd: gd,
prepFn: function() {
var pos = Drawing.getTranslate(annTextGroupInner);

Expand Down Expand Up @@ -616,6 +618,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
// textbox and tail, leave the head untouched
dragElement.init({
element: annTextGroupInner.node(),
gd: gd,
prepFn: function() {
baseTextTransform = annTextGroup.attr('transform');
update = {};
Expand Down Expand Up @@ -686,7 +689,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
}

if(gd._context.editable) {
annText.call(svgTextUtils.makeEditable, annTextGroupInner)
annText.call(svgTextUtils.makeEditable, {delegate: annTextGroupInner, gd: gd})
.call(textLayout)
.on('edit', function(_text) {
options.text = _text;
Expand Down
1 change: 1 addition & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ module.exports = function draw(gd, id) {

dragElement.init({
element: container.node(),
gd: gd,
prepFn: function() {
t0 = container.attr('transform');
setCursor(container);
Expand Down
2 changes: 1 addition & 1 deletion src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dragElement.unhoverRaw = unhover.raw;
* the click & drag interaction has been initiated
*/
dragElement.init = function init(options) {
var gd = Lib.getPlotDiv(options.element) || {},
var gd = options.gd,
numClicks = 1,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
startX,
Expand Down
53 changes: 37 additions & 16 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,14 @@ drawing.singlePointStyle = function(d, sel, trace, markerScale, lineScale, gd) {

};

drawing.pointStyle = function(s, trace) {
drawing.pointStyle = function(s, trace, gd) {
if(!s.size()) return;

// allow array marker and marker line colors to be
// scaled by given max and min to colorscales
var marker = trace.marker;
var markerScale = drawing.tryColorscale(marker, '');
var lineScale = drawing.tryColorscale(marker, 'line');
var gd = Lib.getPlotDiv(s.node());

s.each(function(d) {
drawing.singlePointStyle(d, d3.select(this), trace, markerScale, lineScale, gd);
Expand All @@ -423,7 +422,7 @@ drawing.tryColorscale = function(marker, prefix) {
// draw text at points
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1},
LINEEXPAND = 1.3;
drawing.textPointStyle = function(s, trace) {
drawing.textPointStyle = function(s, trace, gd) {
s.each(function(d) {
var p = d3.select(this),
text = d.tx || trace.text;
Expand Down Expand Up @@ -454,7 +453,7 @@ drawing.textPointStyle = function(s, trace) {
d.tc || trace.textfont.color)
.attr('text-anchor', h)
.text(text)
.call(svgTextUtils.convertToTspans);
.call(svgTextUtils.convertToTspans, gd);
var pgroup = d3.select(this.parentNode),
tspans = p.selectAll('tspan.line'),
numLines = ((tspans[0].length || 1) - 1) * LINEEXPAND + 1,
Expand Down Expand Up @@ -611,19 +610,18 @@ drawing.makeTester = function() {
// in a reference frame where it isn't translated and its anchor
// point is at (0,0)
// always returns a copy of the bbox, so the caller can modify it safely
var savedBBoxes = [];
var savedBBoxes = {};
var savedBBoxesCount = 0;
var maxSavedBBoxes = 10000;

drawing.bBox = function(node) {
// cache elements we've already measured so we don't have to
// remeasure the same thing many times
var saveNum = node.attributes['data-bb'];
if(saveNum && saveNum.value) {
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
}
var hash = nodeHash(node);
var out = savedBBoxes[hash];
if(out) return Lib.extendFlat({}, out);

var tester3 = drawing.tester;
var tester = tester3.node();
var tester = drawing.tester.node();

// copy the node to test into the tester
var testNode = node.cloneNode(true);
Expand Down Expand Up @@ -655,18 +653,41 @@ drawing.bBox = function(node) {
// make sure we don't have too many saved boxes,
// or a long session could overload on memory
// by saving boxes for long-gone elements
if(savedBBoxes.length >= maxSavedBBoxes) {
d3.selectAll('[data-bb]').attr('data-bb', null);
savedBBoxes = [];
if(savedBBoxesCount >= maxSavedBBoxes) {
savedBBoxes = {};
maxSavedBBoxes = 0;
}

// cache this bbox
node.setAttribute('data-bb', savedBBoxes.length);
savedBBoxes.push(bb);
savedBBoxes[hash] = bb;
savedBBoxesCount++;

return Lib.extendFlat({}, bb);
};

// capture everything about a node (at least in our usage) that
// impacts its bounding box, given that bBox clears x, y, and transform
// TODO: is this really everything? Is it worth taking only parts of style,
// so we can share across more changes (like colors)? I guess we can't strip
// colors and stuff from inside innerHTML so maybe not worth bothering outside.
// TODO # 2: this can be long, so could take a lot of memory, do we want to
// hash it? But that can be slow...
// extracting this string from a typical element takes ~3 microsec, where
// doing a simple hash ala https://stackoverflow.com/questions/7616461
// adds ~15 microsec (nearly all of this is spent in charCodeAt)
// function hash(s) {
// var h = 0;
// for (var i = 0; i < s.length; i++) {
// h = (((h << 5) - h) + s.charCodeAt(i)) | 0; // codePointAt?
// }
// return h;
// }
function nodeHash(node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting patch. Do you have any benchmark results comparing old and new Drawing.bBox?

If you're concerned about memory usage, you might want to consider setting maxSavedBBoxes to something less than 1e4. 10000 text nodes on one page sounds a bit much.

cc'ing @monfera @rreusser who might have experience doing stuff like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you have any benchmark results comparing old and new Drawing.bBox?

I don't have any explicit benchmarks, but putting in a few console.log statements you can see that previously during redraws (and especially interactions like dragging axis ends) we were only actually using the cache in a few cases, mostly we were redoing the cloneNode and getBoundingClientRect. Now we hardly ever need to do that. The key is that managing data-bb so we can safely use the cache is really hard and we weren't doing it well. This way there's nothing to manage, and nodeHash is very fast (as long as I don't actually hash the resulting string 🤔 ).

10000 text nodes on one page sounds a bit much.

Yes, perhaps I could drop the limit a bit, but two things to note:

  • I'm not storing text nodes, just a lookup object of {nodeHash: bBox} where bBox is a plain object, my main concern is that nodeHash itself can be quite a long string, though I guess a bBox object of 6 numbers can also take non-negligible memory.
  • A large number of text nodes is exactly when this would be most useful, and wiping the cache will kill this whole gain. Perhaps I could take advantage of Object.keys being ordered in insertion order (usually? always?) and drop the oldest 50% of cache entries or something if we hit the limit?

return node.innerHTML +
node.getAttribute('text-anchor') +
node.getAttribute('style');
}

/*
* make a robust clipPath url from a local id
* note! We'd better not be exporting from a page
Expand Down
12 changes: 6 additions & 6 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ exports.loneHover = function loneHover(hoverItem, opts) {
outerContainer: outerContainer3
};

var hoverLabel = createHoverText([pointData], fullOpts);
var hoverLabel = createHoverText([pointData], fullOpts, opts.gd);
alignHoverText(hoverLabel, fullOpts.rotateLabels);

return hoverLabel.node();
Expand Down Expand Up @@ -490,7 +490,7 @@ function _hover(gd, evt, subplot) {
commonLabelOpts: fullLayout.hoverlabel
};

var hoverLabels = createHoverText(hoverData, labelOpts);
var hoverLabels = createHoverText(hoverData, labelOpts, gd);

hoverAvoidOverlaps(hoverData, rotateLabels ? 'xa' : 'ya');

Expand Down Expand Up @@ -523,7 +523,7 @@ function _hover(gd, evt, subplot) {
});
}

function createHoverText(hoverData, opts) {
function createHoverText(hoverData, opts, gd) {
var hovermode = opts.hovermode;
var rotateLabels = opts.rotateLabels;
var bgColor = opts.bgColor;
Expand Down Expand Up @@ -595,7 +595,7 @@ function createHoverText(hoverData, opts) {
.attr('data-notex', 1);

ltext.text(t0)
.call(svgTextUtils.convertToTspans)
.call(svgTextUtils.convertToTspans, gd)
.call(Drawing.setPosition, 0, 0)
.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
Expand Down Expand Up @@ -745,7 +745,7 @@ function createHoverText(hoverData, opts) {
.call(Drawing.setPosition, 0, 0)
.text(text)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans);
.call(svgTextUtils.convertToTspans, gd);
tx.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);

Expand All @@ -761,7 +761,7 @@ function createHoverText(hoverData, opts) {
.text(name)
.call(Drawing.setPosition, 0, 0)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans);
.call(svgTextUtils.convertToTspans, gd);
tx2.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
tx2width = tx2.node().getBoundingClientRect().width + 2 * HOVERTEXTPAD;
Expand Down
7 changes: 4 additions & 3 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = function draw(gd) {
traces.enter().append('g').attr('class', 'traces');
traces.exit().remove();

traces.call(style)
traces.call(style, gd)
.style('opacity', function(d) {
var trace = d[0].trace;
if(Registry.traceIs(trace, 'pie')) {
Expand Down Expand Up @@ -317,6 +317,7 @@ module.exports = function draw(gd) {

dragElement.init({
element: legend.node(),
gd: gd,
prepFn: function() {
var transform = Drawing.getTranslate(legend);

Expand Down Expand Up @@ -380,14 +381,14 @@ function drawTexts(g, gd) {
.text(name);

function textLayout(s) {
svgTextUtils.convertToTspans(s, function() {
svgTextUtils.convertToTspans(s, gd, function() {
s.selectAll('tspan.line').attr({x: s.attr('x')});
g.call(computeTextDimensions, gd);
});
}

if(gd._context.editable && !isPie) {
text.call(svgTextUtils.makeEditable)
text.call(svgTextUtils.makeEditable, {gd: gd})
.call(textLayout)
.on('edit', function(text) {
this.attr({'data-unformatted': text});
Expand Down
Loading