https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109442
--- Comment #28 from Jan Hubicka <hubicka at ucw dot cz> ---
> vector::size() is called **very often** so needs to be as fast as possible.
> Does this still inline identically?
Last year I made patch for inliner to ignore conditions guarding
__builtin_unreachable. Richi convinced me I should extend it to also
handle computations feeding the conditions. I have WIP patch for that
and then those check should become noop for inlining decisions.
>
> I tried something like that in r14-1452-gfb409a15d9babc and reverted it in
> r14-1470-gb7b255e77a2719, but my _M_invariant() function was more complicated.
>
> Maybe the other part of my _M_invariant() function could be added directly to
> capacity()
>
> --- a/libstdc++-v3/include/bits/stl_vector.h
> +++ b/libstdc++-v3/include/bits/stl_vector.h
> @@ -1201,8 +1201,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
> size_type
> capacity() const _GLIBCXX_NOEXCEPT
> {
> - return size_type(this->_M_impl._M_end_of_storage
> - - this->_M_impl._M_start);
> + ptrdiff_t __dif
> + = this->_M_impl._M_end_of_storage - this->_M_impl._M_start;
> + if (__dir < 0)
> + __builtin_unreachable();
> + return size_type(__dif);
If inliner handles those as noop, I think we could give it a try.
There will be some compile time costs, but hopefully not extreme. It
seems that adding the range check optimizes out quite a lot of calls
that inliner needs to do when dealing with vectors.
Honza