This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [Patch] Codecvt::do_out 20x faster (libstdc++/11602)
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: Paolo Carlini <pcarlini at suse dot de>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 20 Nov 2003 12:21:53 +0100
- Subject: Re: [Patch] Codecvt::do_out 20x faster (libstdc++/11602)
- References: <3FBC8F29.3080006@suse.de>
... nothing essential, but I have attached by mistake a wrong
temporarily tweaked version of the patch, which doesn't compile
due to a missing const qualifier.
The below is the right one, sorry.
Paolo.
////////////
diff -prN libstdc++-v3-orig/config/locale/gnu/codecvt_members.cc libstdc++-v3/config/locale/gnu/codecvt_members.cc
*** libstdc++-v3-orig/config/locale/gnu/codecvt_members.cc Wed Nov 19 10:18:17 2003
--- libstdc++-v3/config/locale/gnu/codecvt_members.cc Thu Nov 20 10:32:29 2003
*************** namespace std
*** 48,103 ****
extern_type*& __to_next) const
{
result __ret = ok;
! // A temporary state must be used since the result of the last
! // conversion may be thrown away.
! state_type __tmp_state(__state);
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
__c_locale __old = __uselocale(_M_c_locale_codecvt);
#endif
! // The conversion must be done by calling wcrtomb in a loop rather
! // than using wcsrtombs because wcsrtombs assumes that the input is
! // zero-terminated.
!
! // Either we can upper bound the total number of external characters to
! // something smaller than __to_end - __to or the conversion must be done
! // using a temporary destination buffer since it is not possible to
! // pass the size of the buffer to wcrtomb
! if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
! while (__from < __from_end)
! {
! const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
! if (__conv == static_cast<size_t>(-1))
! {
! __ret = error;
! break;
! }
! __state = __tmp_state;
! __to += __conv;
! __from++;
! }
! else
{
! extern_type __buf[MB_LEN_MAX];
! while (__from < __from_end && __to < __to_end)
{
! const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
{
! __ret = error;
! break;
! }
! else if (__conv > static_cast<size_t>(__to_end - __to))
! {
! __ret = partial;
! break;
}
-
- memcpy(__to, __buf, __conv);
- __state = __tmp_state;
- __to += __conv;
- __from++;
}
}
--- 48,115 ----
extern_type*& __to_next) const
{
result __ret = ok;
! state_type __tmp_state(__state);
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
__c_locale __old = __uselocale(_M_c_locale_codecvt);
#endif
! // wcsnrtombs is *very* fast but stops if encounters NUL characters:
! // in case we fall back to wcrtomb and then continue, in a loop.
! // NB: wcsnrtombs is a GNU extension
! __from_next = __from;
! __to_next = __to;
! while (__from_next < __from_end && __to_next < __to_end
! && __ret == ok)
{
! const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
! __from_end - __from_next);
! if (!__from_chunk_end)
! __from_chunk_end = __from_end;
!
! const intern_type* __tmp_from = __from_next;
! const size_t __conv = wcsnrtombs(__to_next, &__from_next,
! __from_chunk_end - __from_next,
! __to_end - __to_next, &__state);
! if (__conv == static_cast<size_t>(-1))
! {
! // In case of error, in order to stop at the exact place we
! // have to start again from the beginning with a series of
! // wcrtomb.
! __state = __tmp_state;
! while (__tmp_from < __from_next)
! __to_next += wcrtomb(__to_next, *__tmp_from++, &__state);
! __ret = error;
! }
! else if (__from_next && __from_next < __from_chunk_end)
! {
! __to_next += __conv;
! __ret = partial;
! }
! else
{
! __from_next = __from_chunk_end;
! __to_next += __conv;
! __ret = ok;
! }
!
! if (__from_next < __from_end && __ret == ok)
! {
! extern_type __buf[MB_LEN_MAX];
! __tmp_state = __state;
! const size_t __conv = wcrtomb(__buf, *__from_next, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
+ __ret = error;
+ else if (__conv > static_cast<size_t>(__to_end - __to_next))
+ __ret = partial;
+ else
{
! memcpy(__to_next, __buf, __conv);
! __state = __tmp_state;
! __to_next += __conv;
! __from_next++;
! __ret = ok;
}
}
}
*************** namespace std
*** 105,115 ****
__uselocale(__old);
#endif
- if (__ret == ok && __from < __from_end)
- __ret = partial;
-
- __from_next = __from;
- __to_next = __to;
return __ret;
}
--- 117,122 ----
diff -prN libstdc++-v3-orig/testsuite/performance/wchar_t_out.cc libstdc++-v3/testsuite/performance/wchar_t_out.cc
*** libstdc++-v3-orig/testsuite/performance/wchar_t_out.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/performance/wchar_t_out.cc Thu Nov 20 00:25:40 2003
***************
*** 0 ****
--- 1,84 ----
+ // Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
+ // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ // USA.
+
+ // As a special exception, you may use this file as part of a free software
+ // library without restriction. Specifically, if other files instantiate
+ // templates or use macros or inline functions from this file, or you compile
+ // this file and link it with other files to produce an executable, this
+ // file does not by itself cause the resulting executable to be covered by
+ // the GNU General Public License. This exception does not however
+ // invalidate any other reasons why the executable file might be covered by
+ // the GNU General Public License.
+
+ #include <cstdio>
+ #include <cstring>
+ #include <fstream>
+ #include <langinfo.h>
+ #include <iconv.h>
+ #include <testsuite_performance.h>
+
+ // libstdc++/11602
+ int main(int argc, char** argv)
+ {
+ using namespace std;
+ using namespace __gnu_test;
+
+ time_counter time;
+ resource_counter resource;
+ const int iters = 300000;
+
+ wchar_t wbuf[1024];
+ char cbuf[1024];
+
+ wmemset(wbuf, L'a', 1024);
+
+ // C (iconv)
+ iconv_t cd = iconv_open(nl_langinfo(CODESET), "WCHAR_T");
+ start_counters(time, resource);
+ for (int i = 0; i < iters; ++i)
+ {
+ size_t inbytesleft = 1024 * sizeof(wchar_t);
+ size_t outbytesleft = 1024;
+ char* in = reinterpret_cast<char*>(wbuf);
+ char* out = cbuf;
+ iconv(cd, &in, &inbytesleft, &out, &outbytesleft);
+ }
+ stop_counters(time, resource);
+ iconv_close(cd);
+ report_performance(__FILE__, "C (iconv)", time, resource);
+ clear_counters(time, resource);
+
+ // C++ (codecvt)
+ locale loc;
+ const codecvt<wchar_t, char, mbstate_t>& cvt =
+ use_facet<codecvt<wchar_t, char, mbstate_t> >(loc);
+ mbstate_t state;
+ memset(&state, 0, sizeof(state));
+ start_counters(time, resource);
+ for (int i = 0; i < iters; ++i)
+ {
+ const wchar_t* from_next;
+ char* to_next;
+ cvt.out(state, wbuf, wbuf + 1024, from_next,
+ cbuf, cbuf + 1024, to_next);
+ }
+ stop_counters(time, resource);
+ report_performance(__FILE__, "C++ (codecvt)", time, resource);
+
+ return 0;
+ }