Skip to content

Misc performance optimization #1585

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 2 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 15 additions & 10 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ drawing.makeTester = function(gd) {
// always returns a copy of the bbox, so the caller can modify it safely
var savedBBoxes = [],
maxSavedBBoxes = 10000;

drawing.bBox = function(node) {
// cache elements we've already measured so we don't have to
// remeasure the same thing many times
Expand All @@ -512,32 +513,36 @@ drawing.bBox = function(node) {
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
}

var test3 = d3.select('#js-plotly-tester'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nicely done. There's only one tester div per page, so might as well cache in the Drawing module object. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if I understand correctly. I did cache the tester div in line 517 and 518 already. Is that what you are referring to?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm saying this is a great commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got used to expect modification suggestions in a code review lol. Thanks!

tester = test3.node();
if(!drawing.test3) {
drawing.test3 = d3.select('#js-plotly-tester');
drawing.tester = drawing.test3.node();
}

// copy the node to test into the tester
var testNode = node.cloneNode(true);
tester.appendChild(testNode);
drawing.tester.appendChild(testNode);
// standardize its position... do we really want to do this?
d3.select(testNode).attr({
x: 0,
y: 0,
transform: ''
});

var testRect = testNode.getBoundingClientRect(),
refRect = test3.select('.js-reference-point')
var testRect = testNode.getBoundingClientRect();
if(!drawing.refRect) {
drawing.refRect = drawing.test3.select('.js-reference-point')
.node().getBoundingClientRect();
}

tester.removeChild(testNode);
drawing.tester.removeChild(testNode);

var bb = {
height: testRect.height,
width: testRect.width,
left: testRect.left - refRect.left,
top: testRect.top - refRect.top,
right: testRect.right - refRect.left,
bottom: testRect.bottom - refRect.top
left: testRect.left - drawing.refRect.left,
top: testRect.top - drawing.refRect.top,
right: testRect.right - drawing.refRect.left,
bottom: testRect.bottom - drawing.refRect.top
};

// make sure we don't have too many saved boxes,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var stringMappings = require('../constants/string_mappings');

// Append SVG

var parser = new DOMParser();

d3.selection.prototype.appendSVG = function(_svgString) {
var skeleton = [
'<svg xmlns="', xmlnsNamespaces.svg, '" ',
Expand All @@ -27,7 +29,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
'</svg>'
].join('');

var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
var dom = parser.parseFromString(skeleton, 'application/xml'),
childNode = dom.documentElement.firstChild;

while(childNode) {
Expand Down
5 changes: 3 additions & 2 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ Plotly.plot = function(gd, data, layout, config) {
// Now plot the data
function drawData() {
var calcdata = gd.calcdata,
i;
i,
rangesliderContainers = fullLayout._infolayer.selectAll('g.rangeslider-container');

// in case of traces that were heatmaps or contour maps
// previously, remove them and their colorbars explicitly
Expand All @@ -302,7 +303,7 @@ Plotly.plot = function(gd, data, layout, config) {
.selectAll(query)
.remove();

fullLayout._infolayer.selectAll('g.rangeslider-container')
rangesliderContainers
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. How does this result in a perf improvement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did not tested separately. Just checked and my tests shows 200ms to 400ms improvement on 200 traces.

When I profile the 200-trace case, the select and selectAll are among top 5 of hot functions. So my thought was caching all the constant selected elements in data loops.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right. I understand the value of caching selections, but here you're just assigning the selection to a variable: .selectAll is still called inside a loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.selectAll('g.rangeslider-container') is moved out of the loop, maybe that's where the save comes from.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ha. Now I see it (it isn't part of the GH diff view). Thanks 👍

.selectAll(query)
.remove();
}
Expand Down