Skip to content

Missed optimization: computing smaller index than length does not elliminate bound check #80963

Closed
@newpavlov

Description

@newpavlov

Code:

pub fn foo(buf: &[u8]) -> Option<&[u8]> {
    // guard against potential overflow
    if buf.len() > usize::MAX/3 {
        return None;
    }
    let n = (3*buf.len())/4;
    Some(&buf[..n])
}

godbolt

Even replacing usize::MAX/3 with any non-zero small number (even 1) does not result in removal of the bound check.

This behavior can be reproduced on current Nightly and previous stable compiler versions.

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions