replaced static const/fixed std::vector containers with std::array#4440
replaced static const/fixed std::vector containers with std::array#4440danmar merged 2 commits intocppcheck-opensource:mainfrom
static const/fixed std::vector containers with std::array#4440Conversation
|
I filed https://trac.cppcheck.net/ticket/11292 about detecting this. |
|
Do you have any intuition why this would be faster? |
I looked at the profiling output and it uses less instructions but there's no indication where the performance is won. I posted the <rambling> Looking at the IR gives a bit more insight. It is not doing any So my "intuition" (actually something I consider obvious) is using a lighter (non-)container makes things lighter overall. |
|
Thanks. I would not have thought that there is anything to gain here after optimization. |
The compilers are able to inline things from STL containers to a varying degree. I also added data for GCC 12 to the ticket. |
|
It would be better if we had a |
But we have a Will give it a spin, but I guess for those 4 occurrences it seems unnecessary. In Too small arrays are reported by the compiler, too big ones aren't (I thought a filed a ticket about detecting that already). |
That would make be useful with enabling the |
static const std::vector containers with std::arraystatic const/fixed std::vector containers with std::array
|
|
|
We could switch to C++17 (or 20) and do this: https://godbolt.org/z/GjoPoa69o |
We barely were able to raise the minimum requirement to GCC 4.8 and LLVM just recently switched to C++17. I guess that's still at least a decade off for us. 😁 |
I am not sure I follow the problem. You can get the count with template<class T, class... Ts>
std::array<T, sizeof...(Ts)> make_array(Ts&&... xs)
{
return {std::forward<Ts>(xs)...};
}The problem is that we can't pass the initializer lists to this function, like // Like std::index_sequence in c++14
template <size_t...> struct index_sequence { using type = index_sequence; };
// Used to construct the sequence with O(log n) template instantiations
template <class, class> struct merge_sequence;
template <size_t... Xs, size_t... Ys>
struct merge_sequence<index_sequence<Xs...>, index_sequence<Ys...>>
: index_sequence<Xs..., (sizeof...(Xs) + Ys)...> {};
template <size_t N>
struct make_index_sequence
: merge_sequence<typename make_index_sequence<N / 2>::type,
typename make_index_sequence<N - N / 2>::type> {};
template <> struct make_index_sequence<0> : index_sequence<> {};
template <> struct make_index_sequence<1> : index_sequence<0> {};
template<class T, size_t N, size_t... Is>
std::array<T, N> to_array_impl(const T (&x)[N], index_sequence<Is...>)
{
return {{x[Is]...}};
}
template<class T, size_t N>
std::array<T, N> to_array(const T (&x)[N])
{
return to_array_impl(x, make_index_sequence<N>{});
}We should be able to write |
I think that's overkill to save specifying a single digit the compiler will tell us about if it is wrong - and will probably never be touched again. I will consider this if we ever apply the |
| inline static bool isStringCharLiteral(const std::string &str, char q) | ||
| { | ||
| static const std::vector<std::string> suffixes{"", "u8", "u", "U", "L"}; | ||
| static const std::array<std::string, 5> suffixes{{"", "u8", "u", "U", "L"}}; |
There was a problem hiding this comment.
hmm.. shouldn't we be able to construct the array without extra {}?
static const std::array<std::string, 5> suffixes{"", "u8", "u", "U", "L"};
There was a problem hiding this comment.
We are, but not in all cases. I removed the unnecessary extra ones.
Scanning
mame_regtestwith--enable=all --inconclusiveandDISABLE_VALUEFLOW=1:`Clang 14
1,901,904,224->1,899,570,897->1,899,550,337