Why does the performance of this non-matching pattern scale with the size of the search space?

后端 未结 2 1048
面向向阳花
面向向阳花 2020-12-19 05:22

I have code like the following:

#include 

int main()
{
   char buf[35000] = {};
   auto begin = std::cbegin(buf), end = std::cend(buf);

   std         


        
相关标签:
2条回答
  • 2020-12-19 05:42

    It's simply because libstdc++ and libc++ do not implement such optimization.

    The following is the main part of libstdc++'s implementation of regex_search:

    template<typename _BiIter, typename _Alloc, typename _TraitsT,
         bool __dfs_mode>
      bool _Executor<_BiIter, _Alloc, _TraitsT, __dfs_mode>::
      _M_search()
      {
        if (_M_search_from_first())
          return true;
        if (_M_flags & regex_constants::match_continuous)
          return false;
        _M_flags |= regex_constants::match_prev_avail;
        while (_M_begin != _M_end)
        {
          ++_M_begin;
          if (_M_search_from_first())
            return true;
        }
        return false;
      }
    

    So it does traverse the whole range even if the regular expression contains ^.

    So is libc++'s implementation.

    0 讨论(0)
  • 2020-12-19 06:01

    I am now considering this to be a quality of implementation issue that affects all three toolchains.

    Bugs raised:

    • libstdc++ bug 88947
      • Status: confirmed by dev team
    • libc++ bug 40390
      • Status: unconfirmed
    • Visual Studio bug 432178
      • Status: unconfirmed
    0 讨论(0)
提交回复
热议问题