Skip to content

Optimize comparison functions of Iterator #45007

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 1 commit into from
Oct 12, 2017
Merged

Optimize comparison functions of Iterator #45007

merged 1 commit into from
Oct 12, 2017

Conversation

lezgomatt
Copy link
Contributor

Replaced matching on tuples which led to less performant code generation. Testing on microbenchmarks consistently showed ~1.35x improvement in performance on my machine.

Fixes #44729.

Replaced matching on tuples which led to less performant code generation.
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@leonardo-m
Copy link

Is improving the optimizer here a better idea than working around this specific problem?

@pcwalton
Copy link
Contributor

pcwalton commented Oct 4, 2017

It'd depend on MIR optimizations, which consistently have not enough people working on them.

@lezgomatt
Copy link
Contributor Author

I agree, improving the optimizer would be the ideal solution, but that's also more difficult to pull off. Is there any newbie friendly guide to adding optimizations to the MIR?

I thinking we can rewrite

match (x, y) {
    (x_0, y_0) => b_{0, 0},
    ...
    (x_m, y_n) => b_{m, n},
}

into

match x {
    x_0 => match y { y_0 => b_{0, 0}, ..., y_n => b_{0, n}, },
    ...
    x_m => match y { y_0 => b_{m, 0}, ..., y_n => b_{m, n}, },
}

or whatever the equivalent is in MIR. This should also be generalizable to tuples of any length.

The two transforms should be equivalent even if x and y have side effects since they are called in the same order (I'm assuming that the tuple arguments are evaluated from left-to-right, correct?), there are no other effects between their evaluation (the only thing in between is the pattern matching)**, and the they are called the same number of times.

** We could also evaluate them all first before any of the matching, but when I tested this, it seemed to produce worse code than when it was evaluated only right before the value was matched on.

@aidanhs aidanhs added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 5, 2017
@carols10cents
Copy link
Member

@aturon wasn't available last week and is on PTO this week, so let's try....

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned aturon Oct 9, 2017
@eddyb
Copy link
Member

eddyb commented Oct 9, 2017

r? @bluss

@rust-highfive rust-highfive assigned bluss and unassigned eddyb Oct 9, 2017
@bluss
Copy link
Member

bluss commented Oct 10, 2017

Thank you!

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 10, 2017

📌 Commit 3264c83 has been approved by bluss

@bors
Copy link
Collaborator

bors commented Oct 10, 2017

⌛ Testing commit 3264c83 with merge aac5ea781254d9d59adacc8937da9dd8e1912912...

@bors
Copy link
Collaborator

bors commented Oct 10, 2017

💔 Test failed - status-travis

@kennytm
Copy link
Member

kennytm commented Oct 11, 2017

@bors retry

@bors
Copy link
Collaborator

bors commented Oct 12, 2017

⌛ Testing commit 3264c83 with merge dcbbfb6...

bors added a commit that referenced this pull request Oct 12, 2017
Optimize comparison functions of Iterator

Replaced matching on tuples which led to less performant code generation. Testing on microbenchmarks consistently showed ~1.35x improvement in performance on my machine.

Fixes #44729.
@bors
Copy link
Collaborator

bors commented Oct 12, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: bluss
Pushing dcbbfb6 to master...

@bors bors merged commit 3264c83 into rust-lang:master Oct 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.