[libcxx-commits] [libcxx] c3478ef - [libc++] reduce <complex> parsing time

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 19 13:11:23 PST 2020


Author: Louis Dionne
Date: 2020-02-19T16:09:41-05:00
New Revision: c3478eff7a65d6a77b34e756eabc7176a2b422e8

URL: https://github.com/llvm/llvm-project/commit/c3478eff7a65d6a77b34e756eabc7176a2b422e8
DIFF: https://github.com/llvm/llvm-project/commit/c3478eff7a65d6a77b34e756eabc7176a2b422e8.diff

LOG: [libc++] reduce <complex> parsing time

Instead of including <ios> for ios_base::failbit, simply get failbit
member of the template argument. Print directly to a stream instead
of using intermediate ostringstream.

    Parsing time: 874ms -> 164ms (-81%)

Thanks to Nikita Kniazev for the patch!

Differential Revision: https://reviews.llvm.org/D71214

Added: 
    

Modified: 
    libcxx/include/complex

Removed: 
    


################################################################################
diff  --git a/libcxx/include/complex b/libcxx/include/complex
index c168406befbd..92295be976cc 100644
--- a/libcxx/include/complex
+++ b/libcxx/include/complex
@@ -243,7 +243,7 @@ template<class T, class charT, class traits>
 #include <type_traits>
 #include <stdexcept>
 #include <cmath>
-#include <sstream>
+#include <iosfwd>
 #include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -1406,10 +1406,10 @@ operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
                             __x = complex<_Tp>(__r, __i);
                         }
                         else
-                            __is.setstate(ios_base::failbit);
+                            __is.setstate(__is.failbit);
                     }
                     else
-                        __is.setstate(ios_base::failbit);
+                        __is.setstate(__is.failbit);
                 }
                 else if (__c == _CharT(')'))
                 {
@@ -1417,10 +1417,10 @@ operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
                     __x = complex<_Tp>(__r, _Tp(0));
                 }
                 else
-                    __is.setstate(ios_base::failbit);
+                    __is.setstate(__is.failbit);
             }
             else
-                __is.setstate(ios_base::failbit);
+                __is.setstate(__is.failbit);
         }
         else
         {
@@ -1429,11 +1429,11 @@ operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
             if (!__is.fail())
                 __x = complex<_Tp>(__r, _Tp(0));
             else
-                __is.setstate(ios_base::failbit);
+                __is.setstate(__is.failbit);
         }
     }
     else
-        __is.setstate(ios_base::failbit);
+        __is.setstate(__is.failbit);
     return __is;
 }
 
@@ -1441,12 +1441,7 @@ template<class _Tp, class _CharT, class _Traits>
 basic_ostream<_CharT, _Traits>&
 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
 {
-    basic_ostringstream<_CharT, _Traits> __s;
-    __s.flags(__os.flags());
-    __s.imbue(__os.getloc());
-    __s.precision(__os.precision());
-    __s << '(' << __x.real() << ',' << __x.imag() << ')';
-    return __os << __s.str();
+    return __os << '(' << __x.real() << ',' << __x.imag() << ')';
 }
 
 #if _LIBCPP_STD_VER > 11


        


More information about the libcxx-commits mailing list