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 4 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
7 changes: 3 additions & 4 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
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