https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109383
Bug ID: 109383
Summary: [QoI] std::type_index::operator<=> should not call
__builtin_strcmp twice
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: de34 at live dot cn
Target Milestone: ---
The current the current implementation of type_index::operator<=> in libstdc++
is the following:
```
strong_ordering
operator<=>(const type_index& __rhs) const noexcept
{
if (*_M_target == *__rhs._M_target)
return strong_ordering::equal;
if (_M_target->before(*__rhs._M_target))
return strong_ordering::less;
return strong_ordering::greater;
}
```
When the two referenced type_info are not equal, the current implementation may
needed to call __builtin_strcmp twice (each in type_info::operator== and
type_info::before).
IIUC whenever it's necessary to call __builtin_strcmp from operator<=>, we
should just call it once and return __builtin_strcmp(...) <=> 0. Perhaps it
would be better to add a member function to type_info for convenience.
It's a bit strange to me that while all implementations I investigated (libc++,
libstdc++, and msvc stl) need to use strcmp/__builtin_strcmp for type_info
comparison in some cases, currently none of them avoids calling it twice from
type_index::operator<=>, although strcmp/__builtin_strcmp is already performing
three-way comparison.