Commit e112d53a by Jonathan Wakely Committed by Jonathan Wakely

Optimize std::sub_match comparisons using string_view-like type

Avoid creation of unnecessary basic_string objects by using a simplified
string_view type and performing comparisons on that type instead. A
temporary basic_string object is still used when the sub_match's
iterators are not contiguous, in order to get an object that the
__string_view can reference.

	* include/bits/regex.h (sub_match::operator string_type): Call str().
	(sub_match::compare): Use _M_str() instead of str().
	(sub_match::_M_compare): New public function.
	(sub_match::__string_view): New helper type.
	(sub_match::_M_str): New overloaded functions to avoid creating a
	string_type object when not needed.
	(operator==, operator!=, operator<, operator>, operator<=, operator>=):
	Use sub_match::_M_compare instead of creating string_type objects.
	Fix Doxygen comments.
	* include/bits/regex_compiler.h (__has_contiguous_iter): Remove.
	(__is_contiguous_normal_iter): Rename to __is_contiguous_iter and
	simplify.
	(__enable_if_contiguous_iter, __disable_if_contiguous_iter): Use
	__enable_if_t.
	* include/std/type_traits (__enable_if_t): Define for C++11.
	* testsuite/28_regex/sub_match/compare.cc: New.
	* testsuite/util/testsuite_iterators.h (remove_cv): Add transformation
	trait.
	(input_iterator_wrapper): Use remove_cv for value_type argument of
	std::iterator base class.

From-SVN: r262318
parent dbf96d49
2018-07-02 Jonathan Wakely <jwakely@redhat.com>
* include/bits/regex.h (sub_match::operator string_type): Call str().
(sub_match::compare): Use _M_str() instead of str().
(sub_match::_M_compare): New public function.
(sub_match::__string_view): New helper type.
(sub_match::_M_str): New overloaded functions to avoid creating a
string_type object when not needed.
(operator==, operator!=, operator<, operator>, operator<=, operator>=):
Use sub_match::_M_compare instead of creating string_type objects.
Fix Doxygen comments.
* include/bits/regex_compiler.h (__has_contiguous_iter): Remove.
(__is_contiguous_normal_iter): Rename to __is_contiguous_iter and
simplify.
(__enable_if_contiguous_iter, __disable_if_contiguous_iter): Use
__enable_if_t.
* include/std/type_traits (__enable_if_t): Define for C++11.
* testsuite/28_regex/sub_match/compare.cc: New.
* testsuite/util/testsuite_iterators.h (remove_cv): Add transformation
trait.
(input_iterator_wrapper): Use remove_cv for value_type argument of
std::iterator base class.
2018-06-29 Jonathan Wakely <jwakely@redhat.com>
* testsuite/20_util/add_rvalue_reference/requirements/alias_decl.cc:
......
......@@ -154,42 +154,25 @@ namespace __detail
};
template<typename _Tp>
struct __has_contiguous_iter : std::false_type { };
template<typename _Ch, typename _Tr, typename _Alloc>
struct __has_contiguous_iter<std::basic_string<_Ch, _Tr, _Alloc>>
: std::true_type
{ };
template<typename _Tp, typename _Alloc>
struct __has_contiguous_iter<std::vector<_Tp, _Alloc>>
: std::true_type
{ };
template<typename _Tp>
struct __is_contiguous_normal_iter : std::false_type { };
template<typename _CharT>
struct __is_contiguous_normal_iter<_CharT*> : std::true_type { };
struct __is_contiguous_iter : is_pointer<_Tp>::type { };
template<typename _Tp, typename _Cont>
struct
__is_contiguous_normal_iter<__gnu_cxx::__normal_iterator<_Tp, _Cont>>
: __has_contiguous_iter<_Cont>::type
{ };
__is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
: true_type { };
template<typename _Iter, typename _TraitsT>
using __enable_if_contiguous_normal_iter
= typename enable_if< __is_contiguous_normal_iter<_Iter>::value,
std::shared_ptr<const _NFA<_TraitsT>> >::type;
using __enable_if_contiguous_iter
= __enable_if_t< __is_contiguous_iter<_Iter>::value,
std::shared_ptr<const _NFA<_TraitsT>> >;
template<typename _Iter, typename _TraitsT>
using __disable_if_contiguous_normal_iter
= typename enable_if< !__is_contiguous_normal_iter<_Iter>::value,
std::shared_ptr<const _NFA<_TraitsT>> >::type;
using __disable_if_contiguous_iter
= __enable_if_t< !__is_contiguous_iter<_Iter>::value,
std::shared_ptr<const _NFA<_TraitsT>> >;
template<typename _TraitsT, typename _FwdIter>
inline __enable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
inline __enable_if_contiguous_iter<_FwdIter, _TraitsT>
__compile_nfa(_FwdIter __first, _FwdIter __last,
const typename _TraitsT::locale_type& __loc,
regex_constants::syntax_option_type __flags)
......@@ -201,7 +184,7 @@ namespace __detail
}
template<typename _TraitsT, typename _FwdIter>
inline __disable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
inline __disable_if_contiguous_iter<_FwdIter, _TraitsT>
__compile_nfa(_FwdIter __first, _FwdIter __last,
const typename _TraitsT::locale_type& __loc,
regex_constants::syntax_option_type __flags)
......
......@@ -2331,7 +2331,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __invoke_result<_Functor, _ArgTypes...>
{ };
#if __cplusplus > 201103L
#if __cplusplus >= 201402L
/// Alias template for aligned_storage
template<size_t _Len, size_t _Align =
__alignof__(typename __aligned_storage_msa<_Len>::__type)>
......@@ -2363,11 +2363,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Alias template for result_of
template<typename _Tp>
using result_of_t = typename result_of<_Tp>::type;
#endif
#endif // C++14
// __enable_if_t (std::enable_if_t for C++11)
template<bool _Cond, typename _Tp = void>
using __enable_if_t = typename enable_if<_Cond, _Tp>::type;
// __void_t (std::void_t for C++11)
template<typename...> using __void_t = void;
#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
#if __cplusplus >= 201703L || !defined(__STRICT_ANSI__) // c++17 or gnu++11
#define __cpp_lib_void_t 201411
/// A metafunction that always yields void, used for detecting valid types.
template<typename...> using void_t = void;
......
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++11 } }
#include <regex>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::bidirectional_iterator_wrapper;
template<typename C> struct traits : std::char_traits<C> { };
void
test01()
{
const std::basic_string<char, traits<char>> s0, s1 = "1";
const std::ssub_match sm, sm2;
VERIFY( sm.compare(sm) == 0 );
VERIFY( sm.compare(sm2) == 0 );
VERIFY( sm.compare(sm.str()) == 0 );
VERIFY( sm.compare(sm.str().c_str()) == 0 );
VERIFY( sm.compare(sm2.str()) == 0 );
VERIFY( sm.compare(sm2.str().c_str()) == 0 );
VERIFY( sm.compare(std::string(s1.c_str())) == -1 );
VERIFY( sm.compare(s1.c_str()) == -1 );
VERIFY( sm == sm2 );
VERIFY( !(sm != sm2) );
VERIFY( !(sm < sm2) );
VERIFY( !(sm > sm2) );
VERIFY( sm <= sm2 );
VERIFY( sm >= sm2 );
VERIFY( sm == s0 );
VERIFY( !(sm != s0) );
VERIFY( !(sm < s0) );
VERIFY( !(sm > s0) );
VERIFY( sm <= s0 );
VERIFY( sm >= s0 );
VERIFY( s0 == sm );
VERIFY( !(s0 != sm) );
VERIFY( !(s0 < sm) );
VERIFY( !(s0 > sm) );
VERIFY( s0 <= sm );
VERIFY( s0 >= sm );
VERIFY( sm == s0.c_str() );
VERIFY( !(sm != s0.c_str()) );
VERIFY( !(sm < s0.c_str()) );
VERIFY( !(sm > s0.c_str()) );
VERIFY( sm <= s0.c_str() );
VERIFY( sm >= s0.c_str() );
VERIFY( s0.c_str() == sm );
VERIFY( !(s0.c_str() != sm) );
VERIFY( !(s0.c_str() < sm) );
VERIFY( !(s0.c_str() > sm) );
VERIFY( s0.c_str() <= sm );
VERIFY( s0.c_str() >= sm );
VERIFY( !(sm == s1) );
VERIFY( sm != s1 );
VERIFY( sm < s1 );
VERIFY( !(sm > s1) );
VERIFY( sm <= s1 );
VERIFY( !(sm >= s1) );
VERIFY( !(sm == s1.c_str()) );
VERIFY( sm != s1.c_str() );
VERIFY( sm < s1.c_str() );
VERIFY( !(sm > s1.c_str()) );
VERIFY( sm <= s1.c_str() );
VERIFY( !(sm >= s1.c_str()) );
VERIFY( !(s1.c_str() == sm) );
VERIFY( s1.c_str() != sm );
VERIFY( !(s1.c_str() < sm) );
VERIFY( s1.c_str() > sm );
VERIFY( !(s1.c_str() <= sm) );
VERIFY( s1.c_str() >= sm );
VERIFY( !(sm == s1[0]) );
VERIFY( sm != s1[0] );
VERIFY( sm < s1[0] );
VERIFY( !(sm > s1[0]) );
VERIFY( sm <= s1[0] );
VERIFY( !(sm >= s1[0]) );
VERIFY( !(s1[0] == sm) );
VERIFY( s1[0] != sm );
VERIFY( !(s1[0] < sm) );
VERIFY( s1[0] > sm );
VERIFY( !(s1[0] <= sm) );
VERIFY( s1[0] >= sm );
}
void
test02()
{
const std::basic_string<char, traits<char>> s0, s1 = "1";
std::csub_match sm;
const std::csub_match sm2;
const char c[] = "1";
sm.matched = true;
sm.first = c;
sm.second = c+1;
VERIFY( sm.compare(sm) == 0 );
VERIFY( sm.compare(sm2) == 1 );
VERIFY( sm.compare(sm.str()) == 0 );
VERIFY( sm.compare(sm.str().c_str()) == 0 );
VERIFY( sm.compare(sm2.str()) == 1 );
VERIFY( sm.compare(sm2.str().c_str()) == 1 );
VERIFY( sm.compare(std::string(s1.c_str())) == 0 );
VERIFY( sm.compare(s1.c_str()) == 0 );
VERIFY( !(sm == sm2) );
VERIFY( sm != sm2 );
VERIFY( !(sm < sm2) );
VERIFY( sm > sm2 );
VERIFY( !(sm <= sm2) );
VERIFY( sm >= sm2 );
VERIFY( !(sm2 == sm) );
VERIFY( sm2 != sm );
VERIFY( sm2 < sm );
VERIFY( !(sm2 > sm) );
VERIFY( sm2 <= sm );
VERIFY( !(sm2 >= sm) );
VERIFY( !(sm == s0) );
VERIFY( sm != s0 );
VERIFY( !(sm < s0) );
VERIFY( sm > s0 );
VERIFY( !(sm <= s0) );
VERIFY( sm >= s0 );
VERIFY( !(sm == s0.c_str()) );
VERIFY( sm != s0.c_str() );
VERIFY( !(sm < s0.c_str()) );
VERIFY( sm > s0.c_str() );
VERIFY( !(sm <= s0.c_str()) );
VERIFY( sm >= s0.c_str() );
VERIFY( !(s0.c_str() == sm) );
VERIFY( s0.c_str() != sm );
VERIFY( s0.c_str() < sm );
VERIFY( !(s0.c_str() > sm) );
VERIFY( s0.c_str() <= sm );
VERIFY( !(s0.c_str() >= sm) );
VERIFY( s1 == sm );
VERIFY( !(s1 != sm) );
VERIFY( !(s1 < sm) );
VERIFY( !(s1 > sm) );
VERIFY( s1 <= sm );
VERIFY( s1 >= sm );
VERIFY( sm == s1.c_str() );
VERIFY( !(sm != s1.c_str()) );
VERIFY( !(sm < s1.c_str()) );
VERIFY( !(sm > s1.c_str()) );
VERIFY( sm <= s1.c_str() );
VERIFY( sm >= s1.c_str() );
VERIFY( s1.c_str() == sm );
VERIFY( !(s1.c_str() != sm) );
VERIFY( !(s1.c_str() < sm) );
VERIFY( !(s1.c_str() > sm) );
VERIFY( s1.c_str() <= sm );
VERIFY( s1.c_str() >= sm );
VERIFY( sm == s1[0] );
VERIFY( !(sm != s1[0]) );
VERIFY( !(sm < s1[0]) );
VERIFY( !(sm > s1[0]) );
VERIFY( sm <= s1[0] );
VERIFY( sm >= s1[0] );
VERIFY( s1[0] == sm );
VERIFY( !(s1[0] != sm) );
VERIFY( !(s1[0] < sm) );
VERIFY( !(s1[0] > sm) );
VERIFY( s1[0] <= sm );
VERIFY( s1[0] >= sm );
}
void
test03()
{
const std::basic_string<char, traits<char>> s0, s1 = "1";
const char c[] = "1";
test_container<const char, bidirectional_iterator_wrapper> tc(c, c+1);
std::sub_match<bidirectional_iterator_wrapper<const char>> sm;
const std::sub_match<bidirectional_iterator_wrapper<const char>> sm2;
sm.matched = true;
sm.first = tc.begin();
sm.second = tc.end();
VERIFY( sm.compare(sm) == 0 );
VERIFY( sm.compare(sm2) == 1 );
VERIFY( sm.compare(sm.str()) == 0 );
VERIFY( sm.compare(sm.str().c_str()) == 0 );
VERIFY( sm.compare(sm2.str()) == 1 );
VERIFY( sm.compare(sm2.str().c_str()) == 1 );
VERIFY( sm.compare(std::string(s1.c_str())) == 0 );
VERIFY( sm.compare(s1.c_str()) == 0 );
VERIFY( !(sm == sm2) );
VERIFY( sm != sm2 );
VERIFY( !(sm < sm2) );
VERIFY( sm > sm2 );
VERIFY( !(sm <= sm2) );
VERIFY( sm >= sm2 );
VERIFY( !(sm2 == sm) );
VERIFY( sm2 != sm );
VERIFY( sm2 < sm );
VERIFY( !(sm2 > sm) );
VERIFY( sm2 <= sm );
VERIFY( !(sm2 >= sm) );
VERIFY( !(sm == s0) );
VERIFY( sm != s0 );
VERIFY( !(sm < s0) );
VERIFY( sm > s0 );
VERIFY( !(sm <= s0) );
VERIFY( sm >= s0 );
VERIFY( !(sm == s0.c_str()) );
VERIFY( sm != s0.c_str() );
VERIFY( !(sm < s0.c_str()) );
VERIFY( sm > s0.c_str() );
VERIFY( !(sm <= s0.c_str()) );
VERIFY( sm >= s0.c_str() );
VERIFY( !(s0.c_str() == sm) );
VERIFY( s0.c_str() != sm );
VERIFY( s0.c_str() < sm );
VERIFY( !(s0.c_str() > sm) );
VERIFY( s0.c_str() <= sm );
VERIFY( !(s0.c_str() >= sm) );
VERIFY( s1 == sm );
VERIFY( !(s1 != sm) );
VERIFY( !(s1 < sm) );
VERIFY( !(s1 > sm) );
VERIFY( s1 <= sm );
VERIFY( s1 >= sm );
VERIFY( sm == s1.c_str() );
VERIFY( !(sm != s1.c_str()) );
VERIFY( !(sm < s1.c_str()) );
VERIFY( !(sm > s1.c_str()) );
VERIFY( sm <= s1.c_str() );
VERIFY( sm >= s1.c_str() );
VERIFY( s1.c_str() == sm );
VERIFY( !(s1.c_str() != sm) );
VERIFY( !(s1.c_str() < sm) );
VERIFY( !(s1.c_str() > sm) );
VERIFY( s1.c_str() <= sm );
VERIFY( s1.c_str() >= sm );
VERIFY( sm == s1[0] );
VERIFY( !(sm != s1[0]) );
VERIFY( !(sm < s1[0]) );
VERIFY( !(sm > s1[0]) );
VERIFY( sm <= s1[0] );
VERIFY( sm >= s1[0] );
VERIFY( s1[0] == sm );
VERIFY( !(s1[0] != sm) );
VERIFY( !(s1[0] < sm) );
VERIFY( !(s1[0] > sm) );
VERIFY( s1[0] <= sm );
VERIFY( s1[0] >= sm );
}
int main()
{
test01();
test02();
test03();
}
......@@ -185,6 +185,11 @@ namespace __gnu_test
void operator,(const T&, const output_iterator_wrapper<U>&) = delete;
#endif
template<typename T> struct remove_cv { typedef T type; };
template<typename T> struct remove_cv<const T> { typedef T type; };
template<typename T> struct remove_cv<volatile T> { typedef T type; };
template<typename T> struct remove_cv<const volatile T> { typedef T type; };
/**
* @brief input_iterator wrapper for pointer
*
......@@ -194,7 +199,8 @@ namespace __gnu_test
*/
template<class T>
class input_iterator_wrapper
: public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T&>
: public std::iterator<std::input_iterator_tag, typename remove_cv<T>::type,
std::ptrdiff_t, T*, T&>
{
protected:
input_iterator_wrapper()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment