Skip to content

Trivial vec![] creation then iteration doesn't optimize out the allocation #43272

Closed
@bluss

Description

@bluss
Member

Rustc used to be able to replace vec![1, 2, 3].iter().sum::<i32>() with a constant when optimizing, not at the moment. It simply looks like a Layout method that is not inlinable.

(This is a regression strictly speaking — stable & beta can do this optimization, but not nightly.)

Code to reproduce:

playground link

pub fn sum_me() -> i32 {
    vec![1, 2, 3].iter().sum::<i32>()
}

Activity

added
I-slowIssue: Problems and improvements with respect to performance of generated code.
on Jul 16, 2017
nodakai

nodakai commented on Jul 17, 2017

@nodakai
Contributor

Are there any ways to write automated tests to check for "regressions" like this? Assertions against LLVM bitcode?

kennytm

kennytm commented on Jul 17, 2017

@kennytm
Member

@nodakai Yes codegen tests can be used for this, e.g. https://github.com/rust-lang/rust/blob/master/src/test/codegen/alloc-optimisation.rs. Assuming this is run against the bitcode after optimization, that is.

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jul 26, 2017
Mark-Simulacrum

Mark-Simulacrum commented on Jul 27, 2017

@Mark-Simulacrum
Member

cc @arielb1 -- seems LLVM related

arielb1

arielb1 commented on Jul 27, 2017

@arielb1
Contributor

Nope. Just Layout::repeat (

pub fn repeat(&self, n: usize) -> Option<(Self, usize)> {
) that is not marked as inline.

arielb1

arielb1 commented on Jul 27, 2017

@arielb1
Contributor
added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
and removed
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jul 27, 2017
arielb1

arielb1 commented on Jul 27, 2017

@arielb1
Contributor

I didn't confirm that making Layout::repeat as inline fixes this, so please confirm that/add a test before closing.

added a commit that references this issue on Jul 29, 2017

Rollup merge of rust-lang#43513 - alexcrichton:inline-repeat, r=Burnt…

279d25b
added a commit that references this issue on Jul 30, 2017

Rollup merge of rust-lang#43513 - alexcrichton:inline-repeat, r=Burnt…

a3eadd7
added a commit that references this issue on Aug 12, 2017
086f9e4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @alexcrichton@nodakai@kennytm@arielb1@bluss

      Issue actions

        Trivial vec![] creation then iteration doesn't optimize out the allocation · Issue #43272 · rust-lang/rust