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

Optimize the performance of scatter trace sorting comparator with map #1555

Merged
merged 1 commit into from
Apr 7, 2017

Conversation

hy9be
Copy link
Contributor

@hy9be hy9be commented Apr 5, 2017

Construct a map rather than an array to improve the performance of index search. Jasmine tests passed.

Construct a map rather than an array to improve the performance of index search. Jasmine tests passed.
@hy9be hy9be changed the title Optimize the scatter trace sorting comparator with map Optimize the performance of scatter trace sorting comparator with map Apr 5, 2017
@etpinard
Copy link
Contributor

etpinard commented Apr 5, 2017

Looks good to me.

@rreusser can you think of any side effects (e.g. for animations)?

@@ -51,13 +51,13 @@ module.exports = function plot(gd, plotinfo, cdscatter, transitionOpts, makeOnCo
// Sort the traces, once created, so that the ordering is preserved even when traces
// are shown and hidden. This is needed since we're not just wiping everything out
// and recreating on every update.
for(i = 0, uids = []; i < cdscatter.length; i++) {
uids[i] = cdscatter[i][0].trace.uid;
for(i = 0, uids = {}; i < cdscatter.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems entirely equivalent to me 👍

I didn't anticipate this would be more than like 10-15 or so, which would make the overhead of indexOf negligible. I'll keep this in mind when contemplating orders of magnitude!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, actually there is almost no difference on map and array here if the amount of trace is small. My cases might be a bit crazy (with 5000+ traces). But it seems worthwhile to use map instead here since there is almost no side effect.

@etpinard
Copy link
Contributor

etpinard commented Apr 7, 2017

@hy9be before merging this, would you sharing a few before / after benchmark numbers?

(Can't wait to wrap up #1511)

@rreusser
Copy link
Contributor

rreusser commented Apr 7, 2017

Suggested benchmark strategy for this case (though end-to-end is relevant too):

var t0 = performance.now() // if your browser supports it, otherwise Date.now()
... the above lines of code before, then after
var t1 = performance.now()
console.log('Time taken to sort:', t0 - t1)

The fix seems appropriate either way (I should have known better in the first place 🙄 ), it's just very useful to have a sense for the cost and gains of optimizations like this.

@hy9be
Copy link
Contributor Author

hy9be commented Apr 7, 2017

Here is my script.

And some data points collected on my computer (time in ms):
image

@etpinard
Copy link
Contributor

etpinard commented Apr 7, 2017

@hy9be thanks for that!

@etpinard etpinard added this to the v1.26.0 milestone Apr 7, 2017
@etpinard etpinard merged commit 10acee0 into plotly:master Apr 7, 2017
@etpinard
Copy link
Contributor

etpinard commented Apr 7, 2017

By the way, @hy9be thanks for all your contributions this week!

You get my vote for contributor of the week 🏆

@hy9be
Copy link
Contributor Author

hy9be commented Apr 7, 2017

Wow...feel flattered. Thanks!

I had some exposure to the idea of grammar of graphics. Plotly is not quite an implementation of that but the design is much more orthogonal and abstract than most other charting libraries. I like the project.

Will try to help more.

@rreusser
Copy link
Contributor

rreusser commented Apr 7, 2017

Thanks for the benchmark! Honestly I didn't think that single call would make a big enough difference to have that obvious of an effect in an end-to-end test. 🐢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants