Skip to content

Commit c996703

Browse files
committed
Clarify the conditions on the aliasing section
1 parent 55de6fa commit c996703

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/aliasing.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ fn compute(input: &u32, output: &mut u32) {
3131
if *input > 5 {
3232
*output *= 2;
3333
}
34+
// remember that `output` will be `2` if `input > 10`
3435
}
3536
```
3637

3738
We would *like* to be able to optimize it to the following function:
3839

3940
```rust
4041
fn compute(input: &u32, output: &mut u32) {
41-
let cached_input = *input; // keep *input in a register
42+
let cached_input = *input; // keep `*input` in a register
4243
if cached_input > 10 {
43-
*output = 2; // x > 10 implies x > 5, so double and exit immediately
44+
// If the input is greater than 10, the previous code would set the output to 1 and then double it,
45+
// resulting in an output of 2 (because `>10` implies `>5`).
46+
// Here, we avoid the double assignment and just set it directly to 2.
47+
*output = 2;
4448
} else if cached_input > 5 {
4549
*output *= 2;
4650
}

0 commit comments

Comments
 (0)