Skip to content

Clang does not throw out useless vector::resize() with new/memcpy/delete code during optimization. #107000

@socketpair

Description

@socketpair

https://godbolt.org/z/deTsM91P9

#include <array>
#include <exception>
#include <vector>

void append_column_descriptions(std::vector<int> &iovecs) {
    const size_t new_size = iovecs.size() + 5;

    if (new_size > iovecs.capacity() || new_size > 1000) std::terminate();

    iovecs.resize(new_size);
}

As you can see, new_size > iovecs.capacity() is an assertion that means .resize() will never exceed .capacity(). This means that the compiler can definitely not include realloc code when inlining vector::resize().

So, assembler list for this function probably should only have a code that first validates current size against capacity, and then just increases vector's size + memset() to fill space taken by 5 ints.

[[assume(iovecs.capacity() >= new_size)]]; says:

<source>:9:14: warning: assumption is ignored because it contains (potential) side-effects [-Wassume]

But why ? Both .size() and .capacity() are constexpr noexcept. And also, I guess both just return simple size_t from private: section.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions