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

Change match desugaring in MIR to be O(n) instead of O(n^2) #29763

Merged
merged 6 commits into from
Nov 11, 2015

Conversation

nikomatsakis
Copy link
Contributor

The older algorithm was pretty inefficient for big matches. Fixes #29227. (On my computer, MIR construction on this test case goes from 9.9s to 0.025s.) Whereas before we had a loop like:

  • for all outcomes of the test we are performing
    • for all candidates
      • check whether candidate is relevant to outcome

We now do:

  • for all candidates
    • determine which outcomes the candidate is relevant to

Since the number of outcomes in this case is proportional to the number of candidates, the original algorithm turned out to be O(n^2), and the newer one is just O(n).

This PR also does some minor speedups by eagerly mirroring all patterns, so that we can just pass around &Pattern<'tcx>, which makes cloning cheaper. We could probably go a bit further in this direction.

r? @Aatch

pass around references instead of boxed values to save on clone costs.
before we iterated over the test and each outcome thereof, and then
checked processed every candidate against this outcome, we now organize
the walk differently. Instead, we visit each candidate and say "Here is
the test being performed. Figure out the resulting candidates for each
possible outcome and add yourself into the appropriate places."
@Aatch
Copy link
Contributor

Aatch commented Nov 11, 2015

Looks good to me. r=me with that one change.

@nikomatsakis
Copy link
Contributor Author

@bors r=aatch

@bors
Copy link
Contributor

bors commented Nov 11, 2015

📌 Commit 0a4d6fe has been approved by aatch

@bors
Copy link
Contributor

bors commented Nov 11, 2015

⌛ Testing commit 0a4d6fe with merge daa72e2...

@bors
Copy link
Contributor

bors commented Nov 11, 2015

💔 Test failed - auto-linux-64-nopt-t

@nikomatsakis
Copy link
Contributor Author

@bors r=aatch

(queue is basically empty, so I'll keep abusing bors, since my local build is being slow)

@bors
Copy link
Contributor

bors commented Nov 11, 2015

📌 Commit 3b903a7 has been approved by aatch

@bors
Copy link
Contributor

bors commented Nov 11, 2015

⌛ Testing commit 3b903a7 with merge 138f45a...

@bors
Copy link
Contributor

bors commented Nov 11, 2015

💔 Test failed - auto-mac-64-opt

@nikomatsakis
Copy link
Contributor Author

@bors r+ fml

@bors
Copy link
Contributor

bors commented Nov 11, 2015

📌 Commit 38cf175 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Nov 11, 2015

⌛ Testing commit 38cf175 with merge f1f5c04...

bors added a commit that referenced this pull request Nov 11, 2015
The older algorithm was pretty inefficient for big matches. Fixes #29227. (On my computer, MIR construction on this test case goes from 9.9s to 0.025s.) Whereas before we had a loop like:

- for all outcomes of the test we are performing
    - for all candidates
        - check whether candidate is relevant to outcome

We now do:

- for all candidates
    - determine which outcomes the candidate is relevant to

Since the number of outcomes in this case is proportional to the number of candidates, the original algorithm turned out to be O(n^2), and the newer one is just O(n).

This PR also does some minor speedups by eagerly mirroring all patterns, so that we can just pass around `&Pattern<'tcx>`, which makes cloning cheaper. We could probably go a bit further in this direction.

r? @Aatch
@bors bors merged commit 38cf175 into rust-lang:master Nov 11, 2015
Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 12, 2015
In previous PRs, I changed the match desugaring to generate more efficient code for ints/chars and the like. But this doesn't help when you're matching strings, ranges, or other crazy complex things (leading to rust-lang#29740). This commit restructures match desugaring *yet again* to handle that case better -- basically we now degenerate to an if-else-if chain in such cases.

~~Note that this builds on rust-lang#29763 which will hopefully land soon. So ignore the first few commits.~~ landed now

r? @Aatch since he's been reviewing the other commits in this series
@nikomatsakis nikomatsakis deleted the mir-29227 branch March 30, 2016 16:16
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.

3 participants