Skip to content

Missing optimization for return vector used inside a function. #151390

@AlexgitHubAlex

Description

@AlexgitHubAlex

Optimization that removes vector allocations works correctly in simple cases. When the code becomes more complex, optimization is disabled.

#include <vector>
#include <iostream>


int foo() {        
    std::vector<int> vec1 = {16,2,77,29};
    return vec1[3]; 
} // OK

int bar() {        
    std::vector<int> vec1 = {16,2,77,29};
    std::cout << vec1[0]<< vec1[1]; 
    return 0; 
} // OK

int baz() {        
    std::vector<int> vec1 = {16,2,77,29};
    std::cout << vec1[0]<< vec1[1]; 
    return vec1[3]; 
} // Return value of part object broken remove allocation in vector implementation

The baz function preserves memory allocation which can be optimized.

https://godbolt.org/z/xz9jxr8GP

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions