Regex instantiation

Tim Shen timshen91@gmail.com
Thu Jan 9 09:03:00 GMT 2014


On Thu, Jan 9, 2014 at 1:45 AM, Marc Glisse <marc.glisse@inria.fr> wrote:
> It looks strange, I seem to see the same code (with "extern template") in
> the headers and in src/, whereas I don't expect "extern" in src/.

Oops thank you, it's a silly mistake.

> We would have saved quite a bit using pointers as iterators for vector,
> string, array, etc.
>
> Because of the replacement of basic_string in the next (?) version of gcc, I
> am not sure it is such a good idea to export a new instantiation for it.

For user code that uses things like sregex_token_iterator (triggers
_Compiler<basic_regex<char>::const_iterator>), it saves compile time:
Without any instantiation, testsuite/performance/28_regex/split.cc
cost about 5 sec to compile in my machine; with basic_string inst, it
costs 1.5 sec to compile; with pointers insts only (in this patch), it
costs 2.8 secs. But no doubt it generates larger object file.

Originally I suggested to copy the input regex into _Compiler as a
"const _CharT*", but I really saw a 3000 regex input string, which
makes the time cost of copying not negligible.


-- 
Regards,
Tim Shen
-------------- next part --------------
commit abf1c028eec675a3854198933f173b7eb61cac6e
Author: tim <timshen91@gmail.com>
Date:   Wed Jan 8 21:01:44 2014 -0500

    Not a formal patch.

diff --git a/libstdc++-v3/include/bits/regex.tcc b/libstdc++-v3/include/bits/regex.tcc
index 1ceac60..325b323 100644
--- a/libstdc++-v3/include/bits/regex.tcc
+++ b/libstdc++-v3/include/bits/regex.tcc
@@ -132,6 +132,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __ret;
     }
 
+#if _GLIBCXX_EXTERN_TEMPLATE > 0
+#define __REGEX_INST(__c, __iter)\
+  extern template bool __regex_algo_impl<__iter, allocator<sub_match<__iter>>,\
+    __c, regex_traits<__c>, _RegexExecutorPolicy::_S_auto, false>\
+      (__iter, __iter, match_results<__iter>&, const basic_regex<__c>&,\
+       regex_constants::match_flag_type);\
+  extern template bool __regex_algo_impl<__iter, allocator<sub_match<__iter>>,\
+    __c, regex_traits<__c>, _RegexExecutorPolicy::_S_auto, true>\
+      (__iter, __iter, match_results<__iter>&, const basic_regex<__c>&,\
+       regex_constants::match_flag_type);
+
+  __REGEX_INST(char, const char*)
+#ifdef _GLIBCXX_USE_WCHAR_T
+  __REGEX_INST(wchar_t, const wchar_t*)
+#endif
+#undef __REGEX_INST
+#endif
+
 _GLIBCXX_END_NAMESPACE_VERSION
 }
 
@@ -704,6 +722,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_M_result = nullptr;
     }
 
+#if _GLIBCXX_EXTERN_TEMPLATE > 0
+  extern template class regex_iterator<const char*>;
+  extern template class regex_token_iterator<const char*>;
+#ifdef _GLIBCXX_USE_WCHAR_T
+  extern template class regex_iterator<const wchar_t*>;
+  extern template class regex_token_iterator<const wchar_t*>;
+#endif
+#endif
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 
diff --git a/libstdc++-v3/include/bits/regex_compiler.tcc b/libstdc++-v3/include/bits/regex_compiler.tcc
index 9ff538d..622c8ff 100644
--- a/libstdc++-v3/include/bits/regex_compiler.tcc
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
@@ -528,6 +528,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	return __ret;
     }
 
+#if _GLIBCXX_EXTERN_TEMPLATE > 0
+  extern template class _Compiler<const char*, regex_traits<char>>;
+#ifdef _GLIBCXX_USE_WCHAR_T
+  extern template class _Compiler<const wchar_t*, regex_traits<wchar_t>>;
+#endif
+#endif
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace __detail
 } // namespace
diff --git a/libstdc++-v3/src/c++11/regex-inst.cc b/libstdc++-v3/src/c++11/regex-inst.cc
new file mode 100644
index 0000000..69d7797
--- /dev/null
+++ b/libstdc++-v3/src/c++11/regex-inst.cc
@@ -0,0 +1,56 @@
+// regex -*- C++ -*-
+
+// Copyright (C) 2011-2014 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+#include <regex>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+namespace __detail
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+#define __REGEX_INST(__c, __iter)\
+  template bool __regex_algo_impl<__iter, allocator<sub_match<__iter>>,\
+    __c, regex_traits<__c>, _RegexExecutorPolicy::_S_auto, false>\
+      (__iter, __iter, match_results<__iter>&, const basic_regex<__c>&,\
+       regex_constants::match_flag_type);\
+  template bool __regex_algo_impl<__iter, allocator<sub_match<__iter>>,\
+    __c, regex_traits<__c>, _RegexExecutorPolicy::_S_auto, true>\
+      (__iter, __iter, match_results<__iter>&, const basic_regex<__c>&,\
+       regex_constants::match_flag_type);
+
+  __REGEX_INST(char, const char*)
+  __REGEX_INST(wchar_t, const wchar_t*)
+#undef __REGEX_INST
+  template class _Compiler<const char*, regex_traits<char>>;
+  template class _Compiler<const wchar_t*, regex_traits<wchar_t>>;
+_GLIBCXX_END_NAMESPACE_VERSION
+}
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+  template class regex_iterator<const char*>;
+  template class regex_token_iterator<const char*>;
+
+  template class regex_iterator<const wchar_t*>;
+  template class regex_token_iterator<const wchar_t*>;
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std


More information about the Libstdc++ mailing list