[PATCH] libstdc++: Use 'if constexpr' to slightly simplify <regex>
Jonathan Wakely
jwakely@redhat.com
Mon Apr 28 10:48:03 GMT 2025
On Mon, 28 Apr 2025 at 09:19, Tomasz Kaminski <tkaminsk@redhat.com> wrote:
>
>
>
> On Fri, Apr 25, 2025 at 9:47 PM Jonathan Wakely <jwakely@redhat.com> wrote:
>>
>> This will hardly make a dent in the very slow compile times for <regex>
>> but it seems worth doing anyway.
>>
>> libstdc++-v3/ChangeLog:
>>
>> * include/bits/regex_compiler.h (_AnyMatcher::operator()):
>> Replace tag dispatching with 'if constexpr'.
>> (_AnyMatcher::_M_apply): Remove both overloads.
>> (_BracketMatcher::operator(), _BracketMatcher::_M_ready):
>> Replace tag dispatching with 'if constexpr'.
>> (_BracketMatcher::_M_apply(_CharT, true_type)): Remove.
>> (_BracketMatcher::_M_apply(_CharT, false_type)): Remove second
>> parameter.
>> (_BracketMatcher::_M_make_cache): Remove both overloads.
>> * include/bits/regex_compiler.tcc (_BracketMatcher::_M_apply):
>> Remove second parameter.
>> ---
>>
>> Tested x86_64-linux.
>>
>> libstdc++-v3/include/bits/regex_compiler.h | 59 +++++++++-----------
>> libstdc++-v3/include/bits/regex_compiler.tcc | 2 +-
>> 2 files changed, 26 insertions(+), 35 deletions(-)
>>
>> diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h
>> index f24c7e3baa6..3931790091a 100644
>> --- a/libstdc++-v3/include/bits/regex_compiler.h
>> +++ b/libstdc++-v3/include/bits/regex_compiler.h
>> @@ -376,26 +376,21 @@ namespace __detail
>>
>> bool
>> operator()(_CharT __ch) const
>> - { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
>> -
>> - bool
>> - _M_apply(_CharT __ch, true_type) const
>> {
>> +#pragma GCC diagnostic push
>> +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
>> auto __c = _M_translator._M_translate(__ch);
>> auto __n = _M_translator._M_translate('\n');
>> auto __r = _M_translator._M_translate('\r');
>> - return __c != __n && __c != __r;
>> - }
>> -
>> - bool
>> - _M_apply(_CharT __ch, false_type) const
>> - {
>> - auto __c = _M_translator._M_translate(__ch);
>> - auto __n = _M_translator._M_translate('\n');
>> - auto __r = _M_translator._M_translate('\r');
>> - auto __u2028 = _M_translator._M_translate(u'\u2028');
>> - auto __u2029 = _M_translator._M_translate(u'\u2029');
>> - return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
>
> Could we implement this as, to avoid calling _M_translate when not necessary?
> ```
> if (__c == __n || __c == __r)
> return false;
> if constexpr (!is_same<_CharT, char>::value)
> {
> auto __ls = _M_translator._M_translate(u'\u2028'); // line sep
> auto __ps = _M_translator._M_translate(u'\u2029'); // para sep
> return __c != __ls && __c != __ps;
> }
> return true;
>
> ```
Yes, or even like this:
const auto __c = _M_translator._M_translate(__ch);
if (__c == _M_translator._M_translate('\n'))
return false;
if (__c == _M_translator._M_translate('\r'))
return false;
if constexpr (!is_same<_CharT, char>::value)
{
if (__c == _M_translator._M_translate(u'\u2028')) // line sep
return false
if (__c == _M_translator._M_translate(u'\u2029')) // para sep
return false
}
return true;
I found some other places in <regex> that should use if-constexpr so
I'll prepare an update patch.
More information about the Libstdc++
mailing list