-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Improve JS performance #79052
Improve JS performance #79052
Conversation
This comment has been minimized.
This comment has been minimized.
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.
How did you measure the performance change? I don't see why this would have anything to do with performace 😕
src/librustdoc/html/static/main.js
Outdated
for (var z = split.length - 1; z >= 0; --z) { | ||
if (split[z] === "") { | ||
split.splice(z, 1); | ||
z -= 1; | ||
} | ||
} |
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.
for (var z = split.length - 1; z >= 0; --z) { | |
if (split[z] === "") { | |
split.splice(z, 1); | |
z -= 1; | |
} | |
} | |
split = split.filter(function(segment) { return segment !== ""; }); |
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 is a huge speed gain. (84% apparently). Great catch!
name = result.item.name.toLowerCase(); | ||
path = result.item.path.toLowerCase(); | ||
parent = result.item.parent; |
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.
Does this really have a performance difference?
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.
No, it's more about cleanup. Recreating the variable at each iteration isn't "clean" (and invalid too, but js doesn't really care apparently).
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.
javascript variables defined with var
are hoisted to the top of the function you're in, so declaring them earlier doesn't make a difference. so it won't recreate the variable each iteration, so it's valid. are you referencing something when you say "clean"?
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.
src/librustdoc/html/static/main.js
Outdated
for (x = 0; allFound === false && x < elen; ++x) { | ||
allFound = getObjectFromId(elems[x]).name === firstGeneric; | ||
} | ||
if (allFound === true) { |
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.
for (x = 0; allFound === false && x < elen; ++x) { | |
allFound = getObjectFromId(elems[x]).name === firstGeneric; | |
} | |
if (allFound === true) { | |
var allFound = elems.every(function(id) { return getObjectFromId(id).name == firstGeneric; }); | |
if (allFound === true) { | |
elems.pop(); | |
} |
If you don't like the suggestion, you have a typo: elen
-> elems
.
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.
It's not a typo, it's meant for "elements length". But it's not that great so I'll update it a bit. Also, the code you suggested might be a worth a try, but I'll delay it for another PR until I fully get how it works.
src/librustdoc/html/static/main.js
Outdated
for (x = 0, len = obj[GENERICS_DATA].length; x < len; ++x) { | ||
if (obj[GENERICS_DATA][x] === val.name) { | ||
return true; | ||
} |
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.
for (x = 0, len = obj[GENERICS_DATA].length; x < len; ++x) { | |
if (obj[GENERICS_DATA][x] === val.name) { | |
return true; | |
} | |
if (obj[GENERICS_DATA].some(function(name) { return name === val.name; })) { | |
return true; |
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.
Actually, we could even use our own "onEach" function. It basically works the same after all.
@@ -927,8 +924,7 @@ function defocusSearchBar() { | |||
lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2); | |||
} else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) { | |||
// We can check if the type we're looking for is inside the generics! | |||
var olength = obj[GENERICS_DATA].length; | |||
for (x = 0; x < olength; ++x) { | |||
for (x = 0, len = obj[GENERICS_DATA].length; x < len; ++x) { |
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.
Why this change?
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.
I removed a useless extra variable.
@@ -942,8 +938,7 @@ function defocusSearchBar() { | |||
var lev_distance = MAX_LEV_DISTANCE + 1; | |||
|
|||
if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) { | |||
var length = obj.type[INPUTS_DATA].length; | |||
for (var i = 0; i < length; i++) { | |||
for (var i = 0, len = obj.type[INPUTS_DATA].length; i < len; i++) { |
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.
Why this change?
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.
More consistent with the rest.
Just a guess, but by having a local, the JIT compiler probably can do a better job with it. After all the original way always accesses the For the other changes where a |
@jyn514: I was running the performance comparison using https://jsbench.me/ for another project and came across this one. The 3% improvement is what came out after making the median out of the runs. |
@@ -569,7 +570,7 @@ function defocusSearchBar() { | |||
len = rootPath.match(/\.\.\//g).length + 1; | |||
|
|||
for (i = 0; i < len; ++i) { | |||
match = url.match(/\/[^\/]*$/); | |||
match = url.match(/\/[^/]*$/); |
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.
I think this was easier to read before - unless it matched a literal backslash?
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.
The linter complained about it so I removed it.
function buildHelperPopup() { | ||
var buildHelperPopup = function() { |
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.
Why this change?
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.
Because we call this function every time we want to show the help popup instead of creating the popup every time. However, since it only requires to be built once, we then set buildHelperPopup
to an empty function so that calling it doesn't do anything anymore.
☔ The latest upstream changes (presumably #78876) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
@@ -927,8 +920,7 @@ function defocusSearchBar() { | |||
lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2); | |||
} else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) { | |||
// We can check if the type we're looking for is inside the generics! | |||
var olength = obj[GENERICS_DATA].length; | |||
for (x = 0; x < olength; ++x) { | |||
for (x = 0, len = obj[GENERICS_DATA].length; x < len; ++x) { |
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.
where is x
declared?
I'll be honest, I am not comfortable making changes of this magnitude without tests. Maybe we could just keep the One way to verify this would be |
Actually, we use But if the amount of changes is problematic, I can make smaller PRs if you prefer? |
Part of rust-lang#79052, originally suggested in rust-lang#79052 (comment) Co-authored-by: Joshua Nelson <joshua@yottadb.com>
Use Array.prototype.filter instead of open-coding Part of rust-lang#79052, originally suggested in rust-lang#79052 (comment) by `@jyn514` Besides making main.js smaller (always a plus), this also performs better by using the optimized filter implementation in your browser's JavaScript engine (according to `@GuillaumeGomez,` an 84% performance improvement).
More js cleanup Part of rust-lang#79052 (Same kind as rust-lang#80515). This one is about some small fixes: * Replacing some loops with `onEachLazy`. * Removing unused function arguments. * Turn `buildHelperPopup` into a variable so it can be "replaced" once the function has been called once so it's not called again. r? `@jyn514`
…Nemo157,jyn514 Improve JS performance by storing length before comparing to it in loops Since rust-lang#79052 is quite complicated to review, I suggested to split into smaller parts. This first part is mostly about saving the array length into a variable (I tried to not change anything else as much as possible 😃 ). r? `@jyn514`
Can be closed now! |
Along my random journey on the internet, I came upon an interesting fact when looking at some code: they were always setting the length of the data they were iterating on in a variable instead of just accessing it at every turn.
Based on this strange code, I decided to check why they were doing so and discovered that you could get up to 3% of performance improvements (which is big enough for me to consider it).
So to sum it up:
Of course, it only makes sense to use this when the data we're iterating on doesn't have its length updated while iterating.
The second commit is a little cleanup after I ran "eslint".
r? @jyn514