This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] Speed up operator>>(istream, string)


Hi,

the below consistently improves operator>>(..., string) in the same way of
getline(..., string).

For a testcase identical to streams.cpp in 15002, besides 'f >> l' instead
of 'getline(f, l)', these are some numbers on my usual machine (on x86_64
the results are similar):

3.4.0
-----
9.090u 0.080s 0:09.22 99.4%     0+0k 0+0io 206pf+0w

3.4.1 pre
---------
3.470u 0.140s 0:03.81 94.7%     0+0k 0+0io 204pf+0w

3.4.1 pre + patch
-----------------
1.400u 0.110s 0:01.51 100.0% 0+0k 0+0io 205pf+0w


3.5 exp
-------
7.050u 0.090s 0:07.18 99.4%     0+0k 0+0io 208pf+0w

3.5 exp + patch
---------------
1.500u 0.080s 0:01.58 100.0%    0+0k 0+0io 209pf+0w

Icc8.0
------
2.770u 0.110s 0:02.89 99.6%     0+0k 0+0io 203pf+0w


As you can see, the code generated by unpatched mainline is not as good as 3_4-branch: sadly, I'm seeing this rather often, for other performance testcases in the iostreams area too.

Anyway, of course the patch would go in mainline only, for now, and I'm
also adding a couple of new testcases.

A will wait a few hours, in case of comments.

Regtested x86-linux.

Paolo.

///////////////
2004-05-20  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/istream.tcc (operator>>(basic_istream<>&,
	basic_string<>&)): Use a temporary buffer, thus avoiding
	reallocation for common case.
	* testsuite/21_strings/basic_string/inserters_extractors/char/11.cc:
	New.
	* testsuite/21_strings/basic_string/inserters_extractors/wchar_t/11.cc:
	Likewise.
diff -urN libstdc++-v3-orig/include/bits/istream.tcc libstdc++-v3/include/bits/istream.tcc
--- libstdc++-v3-orig/include/bits/istream.tcc	2004-04-26 17:26:18.000000000 +0200
+++ libstdc++-v3/include/bits/istream.tcc	2004-05-20 10:50:40.000000000 +0200
@@ -1043,11 +1043,13 @@
 	{
 	  try
 	    {
+	      // Avoid reallocation for common case.
 	      __str.erase();
-	      streamsize __w = __in.width();
-	      __size_type __n;
-	      __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
-
+	      _CharT __buf[128];
+	      __size_type __len = 0;	      
+	      const streamsize __w = __in.width();
+	      const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
+		                              : __str.max_size();
 	      const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
 	      const __int_type __eof = _Traits::eof();
 	      __streambuf_type* __sb = __in.rdbuf();
@@ -1057,10 +1059,17 @@
 		     && !_Traits::eq_int_type(__c, __eof)
 		     && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
 		{
-		  __str += _Traits::to_char_type(__c);
+		  if (__len == sizeof(__buf) / sizeof(_CharT))
+		    {
+		      __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
+		      __len = 0;
+		    }
+		  __buf[__len++] = _Traits::to_char_type(__c);
 		  ++__extracted;
 		  __c = __sb->snextc();
 		}
+	      __str.append(__buf, __len);
+
 	      if (_Traits::eq_int_type(__c, __eof))
 		__err |= ios_base::eofbit;
 	      __in.width(0);
@@ -1102,7 +1111,7 @@
 	{
 	  try
 	    {
-	      // Avoid reallocation for common case.	      
+	      // Avoid reallocation for common case.
 	      __str.erase();
 	      _CharT __buf[128];
 	      __size_type __len = 0;
diff -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/inserters_extractors/char/11.cc libstdc++-v3/testsuite/21_strings/basic_string/inserters_extractors/char/11.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/inserters_extractors/char/11.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/inserters_extractors/char/11.cc	2004-05-20 11:11:37.000000000 +0200
@@ -0,0 +1,79 @@
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.
+
+// 21.3.7.9 inserters and extractors
+
+#include <istream>
+#include <string>
+#include <fstream>
+#include <testsuite_hooks.h>
+
+using namespace std;
+
+string prepare(string::size_type len, unsigned nchunks)
+{
+  string ret;
+  for (unsigned i = 0; i < nchunks; ++i)
+    {
+      for (string::size_type j = 0; j < len; ++j)
+	ret.push_back('a' + rand() % 26);
+      len *= 2;
+      ret.push_back(' ');
+    }
+  return ret;
+}
+
+void check(istream& stream, const string& str)
+{
+  bool test __attribute__((unused)) = true;
+
+  string chunk;
+  string::size_type index = 0, index_new = 0;
+
+  while (stream >> chunk)
+    {
+      index_new = str.find(' ', index);
+      VERIFY( !str.compare(index, index_new - index, chunk) );
+      index = index_new + 1;
+    }
+  VERIFY( stream.eof() );
+}
+
+// istream& operator>>(istream&, string&)
+void test01()
+{
+  const char filename[] = "inserters_extractors-3.txt";
+
+  const string data = prepare(666, 10);
+
+  ofstream ofstrm;
+  ofstrm.open(filename);
+  ofstrm.write(data.data(), data.size());
+  ofstrm.close();
+
+  ifstream ifstrm;
+  ifstrm.open(filename);
+  check(ifstrm, data);
+  ifstrm.close();
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/inserters_extractors/wchar_t/11.cc libstdc++-v3/testsuite/21_strings/basic_string/inserters_extractors/wchar_t/11.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/inserters_extractors/wchar_t/11.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/inserters_extractors/wchar_t/11.cc	2004-05-20 11:15:34.000000000 +0200
@@ -0,0 +1,79 @@
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.
+
+// 21.3.7.9 inserters and extractors
+
+#include <istream>
+#include <string>
+#include <fstream>
+#include <testsuite_hooks.h>
+
+using namespace std;
+
+wstring prepare(wstring::size_type len, unsigned nchunks)
+{
+  wstring ret;
+  for (unsigned i = 0; i < nchunks; ++i)
+    {
+      for (wstring::size_type j = 0; j < len; ++j)
+	ret.push_back(L'a' + rand() % 26);
+      len *= 2;
+      ret.push_back(L' ');
+    }
+  return ret;
+}
+
+void check(wistream& stream, const wstring& str)
+{
+  bool test __attribute__((unused)) = true;
+
+  wstring chunk;
+  wstring::size_type index = 0, index_new = 0;
+
+  while (stream >> chunk)
+    {
+      index_new = str.find(L' ', index);
+      VERIFY( !str.compare(index, index_new - index, chunk) );
+      index = index_new + 1;
+    }
+  VERIFY( stream.eof() );
+}
+
+// istream& operator>>(istream&, string&)
+void test01()
+{
+  const char filename[] = "inserters_extractors-3.txt";
+
+  const wstring data = prepare(666, 10);
+
+  wofstream ofstrm;
+  ofstrm.open(filename);
+  ofstrm.write(data.data(), data.size());
+  ofstrm.close();
+
+  wifstream ifstrm;
+  ifstrm.open(filename);
+  check(ifstrm, data);
+  ifstrm.close();
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]