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

Improve JS performance #79052

Closed
wants to merge 3 commits into from
Closed

Conversation

GuillaumeGomez
Copy link
Member

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:

var some_array = [];

for (var i = 0, len = some_array.length; i < len; ++i) {}
// is faster than:
for (var i = 0; i < some_array.length; ++i) {}

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

@rust-highfive

This comment has been minimized.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 14, 2020
@jyn514 jyn514 added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Nov 14, 2020
Copy link
Member

@jyn514 jyn514 left a 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 Show resolved Hide resolved
Comment on lines 666 to 670
for (var z = split.length - 1; z >= 0; --z) {
if (split[z] === "") {
split.splice(z, 1);
z -= 1;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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 !== ""; });

Copy link
Member Author

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!

Comment on lines +789 to +791
name = result.item.name.toLowerCase();
path = result.item.path.toLowerCase();
parent = result.item.parent;
Copy link
Member

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?

Copy link
Member Author

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).

Copy link
Member

@frewsxcv frewsxcv Dec 11, 2020

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"?

Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines 881 to 884
for (x = 0; allFound === false && x < elen; ++x) {
allFound = getObjectFromId(elems[x]).name === firstGeneric;
}
if (allFound === true) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Member Author

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.

Comment on lines 911 to 914
for (x = 0, len = obj[GENERICS_DATA].length; x < len; ++x) {
if (obj[GENERICS_DATA][x] === val.name) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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;

Copy link
Member Author

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) {
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

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++) {
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

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.

@Lonami
Copy link
Contributor

Lonami commented Nov 14, 2020

don't see why this would have anything to do with performace

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 .length attribute of an arbitrary object which is probably more expensive than a local.

For the other changes where a length was declared before and is now also inside the for, I'd argue it makes it more consistent with the rest of these changes :P

@GuillaumeGomez
Copy link
Member Author

@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(/\/[^/]*$/);
Copy link
Member

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?

Copy link
Member Author

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.

Comment on lines -2851 to +2844
function buildHelperPopup() {
var buildHelperPopup = function() {
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

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.

@bors
Copy link
Contributor

bors commented Dec 1, 2020

☔ 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:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

where is x declared?

@jyn514
Copy link
Member

jyn514 commented Dec 11, 2020

I'll be honest, I am not comfortable making changes of this magnitude without tests. Maybe we could just keep the filter improvement (#79052 (comment)) and leave the rest for later?

One way to verify this would be use strict, right? I don't think we have that on currently.

@GuillaumeGomez
Copy link
Member Author

Actually, we use "use strict";. ;)

But if the amount of changes is problematic, I can make smaller PRs if you prefer?

notriddle added a commit to notriddle/rust that referenced this pull request Jan 1, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2021
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).
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 4, 2021
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`
@JohnCSimon JohnCSimon added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 11, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jan 15, 2021
…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`
@GuillaumeGomez
Copy link
Member Author

Can be closed now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants