Skip to content

Commit b44af9d

Browse files
authored
Merge pull request #272 from JohnTitor/clarify-aliasing
Clarify the conditions on the aliasing section
2 parents 35cd622 + c996703 commit b44af9d

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
@@ -28,16 +28,20 @@ fn compute(input: &u32, output: &mut u32) {
2828
if *input > 5 {
2929
*output *= 2;
3030
}
31+
// remember that `output` will be `2` if `input > 10`
3132
}
3233
```
3334

3435
We would *like* to be able to optimize it to the following function:
3536

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

0 commit comments

Comments
 (0)