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

In chapter 3.2, Why Aliasing Matters section, is the optimize example wrong? #148

Closed
adispring opened this issue Jul 10, 2019 · 1 comment · Fixed by #272
Closed

In chapter 3.2, Why Aliasing Matters section, is the optimize example wrong? #148

adispring opened this issue Jul 10, 2019 · 1 comment · Fixed by #272

Comments

@adispring
Copy link

In chapter 3.2(Aliasing), Why Aliasing Matters section, is the optimize example wrong?

fn compute(input: &u32, output: &mut u32) {
    let cached_input = *input; // keep *input in a register
    if cached_input > 10 {
        *output = 2;  // x > 10 implies x > 5, so double and exit immediately
    } else if cached_input > 5 {
        *output *= 2;
    }
}

should be:

fn compute(input: &u32, output: &mut u32) {
    let cached_input = *input; // keep *input in a register
    if cached_input > 10 {
        *output *= 2;  // x > 10 implies x > 5, so double and exit immediately
    } else if cached_input > 5 {
        *output *= 2;
    }
}

or

fn compute(input: &u32, output: &mut u32) {
    let cached_input = *input; // keep *input in a register
    if cached_input > 10 {
        *output = 1;  // x > 10 implies x > 5, so double and exit immediately
    } else if cached_input > 5 {
        *output *= 2;
    }
}
@Gankra
Copy link
Contributor

Gankra commented Jul 13, 2019

No it's correct. When it's > 10 we set it to 1 and then double it. The given optimization is to just replace this with setting it to 2.

It's a misleading comment, though. That should be fixed.

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 a pull request may close this issue.

2 participants