Skip to content

Commit

Permalink
Rollup merge of rust-lang#73910 - cuviper:while-indexing, r=oli-obk
Browse files Browse the repository at this point in the history
Rewrite a few manual index loops with while-let

There were a few instances of this pattern:

```rust
while index < vec.len() {
    let item = &vec[index];
    // ...
}
```

These can be indexed at once:

```rust
while let Some(item) = vec.get(index) {
    // ...
}
```

Particularly in `ObligationForest::process_obligations`, this mitigates
a codegen regression found with LLVM 11 (rust-lang#73526).
  • Loading branch information
Manishearth committed Jul 2, 2020
2 parents b145f8c + 47425e4 commit 30f6fac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/librustc_data_structures/obligation_forest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,7 @@ impl<O: ForestObligation> ObligationForest<O> {
// be computed with the initial length, and we would miss the appended
// nodes. Therefore we use a `while` loop.
let mut index = 0;
while index < self.nodes.len() {
let node = &mut self.nodes[index];

while let Some(node) = self.nodes.get_mut(index) {
// `processor.process_obligation` can modify the predicate within
// `node.obligation`, and that predicate is the key used for
// `self.active_cache`. This means that `self.active_cache` can get
Expand Down Expand Up @@ -666,16 +664,16 @@ impl<O: ForestObligation> ObligationForest<O> {

for node in &mut self.nodes {
let mut i = 0;
while i < node.dependents.len() {
let new_index = node_rewrites[node.dependents[i]];
while let Some(dependent) = node.dependents.get_mut(i) {
let new_index = node_rewrites[*dependent];
if new_index >= orig_nodes_len {
node.dependents.swap_remove(i);
if i == 0 && node.has_parent {
// We just removed the parent.
node.has_parent = false;
}
} else {
node.dependents[i] = new_index;
*dependent = new_index;
i += 1;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_data_structures/transitive_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,12 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
/// - Input: `[a, x, b, y]`. Output: `[a, x]`.
fn pare_down(candidates: &mut Vec<usize>, closure: &BitMatrix<usize, usize>) {
let mut i = 0;
while i < candidates.len() {
let candidate_i = candidates[i];
while let Some(&candidate_i) = candidates.get(i) {
i += 1;

let mut j = i;
let mut dead = 0;
while j < candidates.len() {
let candidate_j = candidates[j];
while let Some(&candidate_j) = candidates.get(j) {
if closure.contains(candidate_i, candidate_j) {
// If `i` can reach `j`, then we can remove `j`. So just
// mark it as dead and move on; subsequent indices will be
Expand Down

0 comments on commit 30f6fac

Please sign in to comment.