-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Optimize performance of setCategoryIndex #1544
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
Changes from 1 commit
40e69d3
b47c7af
e8941d6
b3fb03d
2992126
989f07a
91cfaa4
abcd729
153f547
ff043b4
c32163e
d2be6c9
14d6649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… mock object in unit test instead
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,10 +152,8 @@ module.exports = function setConvert(ax, fullLayout) { | |
if(ax._categoriesMap) { | ||
index = ax._categoriesMap[v]; | ||
if(index !== undefined) return index; | ||
} else { | ||
index = ax._categories.indexOf(v); | ||
if(index !== -1) return index; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this block is only here to fix that test, and never gets hit in real code (which looks to be the case to me, since For the record, this made me curious "do we still need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree. I will make the change on the mock object instead. Yeah I tried to completely replacing the Actually, comparing to maintaining a pair of synced-up array and map, I feel there must be some better data structure for a performant categories collection. But I was sort of lazy and did not give it another think anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point, I'm sure there is, though it won't be built in so is unlikely to improve performance, it would just clean up the API... lets not worry about it for now, this two-part solution is light and easy enough to use here but we can look into it further if this comes up in enough other places. |
||
|
||
if(typeof v === 'number') { return v; } | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, I'm glad it was as easy as that :)
Apologies for the iterations, but one more related request since this is a perf PR of a couple of very hot code paths: I don't think that either
getCategoryIndex
orsetCategoryIndex
needs the fallbacks for missing_categoriesMap
- the previous version, using only_categories
, didn't have these fallbacks, and every path that creates_categories
also creates_categoriesMap
, right?(also 📚 your comment above this is out of date now)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now no functions will fallback to use
indexOf
on_categories
anymore.Currently I made sure
_categoriesMap
is always populated accordingly when_categories
is created or initialized with some items for the current code. But this is not a robust way. Someone could change_categories
without updating_categoriesMap
in their code, and it will not be trivial for them to realize they need to update_categoriesMap
.I have code with fallbacks in stash. If you think that's a better way I can quickly change it back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I just meant we can remove the fallbacks for if
_categoriesMap
doesn't even exist, ie ingetCategoryIndex
changing:to just:
and removing from
setCategoryIndex
:As far as I can tell, that much should be robust. Not a huge deal but should be a small performance boost.
But you raise a good point about future changes, I guess that's an argument for converting these two to a single bidirectional map object even if it doesn't yield any additional perf benefits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I get it. Sorry I misunderstood your point.
Well, I feel a liiiittle bit uncomfortable of doing that, because if there is any case that the map is not created, it will result in an uncaught exception, which to me, seems more severe than a messed up chart.
And I tried on jsperf, the overhead of checking null is less than 10% on Chrome:

And considering the performance of reading a map itself, 10% differences here will not be a big cost for the whole workflow.
For Safari the differences are even less.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine, I'm pretty confident that it's OK since that's how it worked previously, but I can take responsibility for removing it (in another PR) and ensuring we have sufficient test coverage. You're right that the penalty is much smaller than the improvement you've made here (though I'd be wary of that jsperf result, I suspect in Safari the compiler has optimized away almost everything you see there).
So I think this is ready to go! Nice job @hy9be !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know! I used to trust the results I got from jsperf a lot...
Thanks a lot for reviewing my code! @alexcjohnson