public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Speed up inplace_merge algorithm & fix inefficient logic(PR c++/83938)
@ 2018-01-19  8:44 chang jc
  2018-01-19 20:45 ` Jason Merrill
  0 siblings, 1 reply; 12+ messages in thread
From: chang jc @ 2018-01-19  8:44 UTC (permalink / raw)
  To: gcc-patches

Current std::inplace_merge() suffers from performance issue by inefficient
logic under limited memory,

It leads to performance downgrade.

Please help to review it.

Index: include/bits/stl_algo.h
===================================================================
--- include/bits/stl_algo.h	(revision 256871)
+++ include/bits/stl_algo.h	(working copy)
@@ -2437,7 +2437,7 @@
 	  _BidirectionalIterator __second_cut = __middle;
 	  _Distance __len11 = 0;
 	  _Distance __len22 = 0;
-	  if (__len1 > __len2)
+	  if (__len1 < __len2)
 	    {
 	      __len11 = __len1 / 2;
 	      std::advance(__first_cut, __len11);
@@ -2539,9 +2539,15 @@
       const _DistanceType __len1 = std::distance(__first, __middle);
       const _DistanceType __len2 = std::distance(__middle, __last);

+
       typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
-      _TmpBuf __buf(__first, __last);
-
+      _BidirectionalIterator __start, __end;
+      if (__len1 < __len2) {
+	__start = __first; __end = __middle;
+      } else {
+	__start = __middle; __end = __last;
+      }
+      _TmpBuf __buf(__start, ___end);
       if (__buf.begin() == 0)
 	std::__merge_without_buffer
 	  (__first, __middle, __last, __len1, __len2, __comp);
Index: include/bits/stl_tempbuf.h
===================================================================
--- include/bits/stl_tempbuf.h	(revision 256871)
+++ include/bits/stl_tempbuf.h	(working copy)
@@ -95,7 +95,7 @@
 							std::nothrow));
 	  if (__tmp != 0)
 	    return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
-	  __len /= 2;
+	  __len = (__len + 1) / 2;
 	}
       return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
     }




Thanks

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-11-19 12:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-19  8:44 [C++ PATCH] Speed up inplace_merge algorithm & fix inefficient logic(PR c++/83938) chang jc
2018-01-19 20:45 ` Jason Merrill
2018-01-25 23:10   ` chang jc
2018-01-30 22:43     ` François Dumont
2018-06-06 16:39     ` François Dumont
2018-07-24 10:22       ` [C++ PATCH] Speed up inplace_merge algorithm & fix inefficient logic(PR libstdc++/83938) François Dumont
2018-08-21 20:34         ` François Dumont
2018-10-29  8:55           ` François Dumont
2018-12-21 21:03             ` Jonathan Wakely
2019-06-09 14:27               ` François Dumont
2019-07-16 16:41                 ` François Dumont
2020-11-19 12:08                   ` [C++ PATCH] Speed up inplace_merge algorithm & fix inefficient logic(PR libstdc++/83938)# Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).