[libcxx-commits] [libcxx] [libc++] Remove <istream> and <ostream> includes from <iomanip> (PR #116223)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 14 05:05:35 PST 2024


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/116223

This reduces the include time of `<filesystem>` by ~50ms.


>From a4984f4e4c9e4d68e368bfe311ce994e20e6ff78 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 14 Nov 2024 14:04:38 +0100
Subject: [PATCH] [libc++] Remove <istream> and <ostream> includes from
 <iomanip>

---
 libcxx/include/CMakeLists.txt                 |  2 +
 libcxx/include/__locale_dir/pad_and_output.h  | 83 +++++++++++++++++++
 libcxx/include/__ostream/basic_ostream.h      | 28 +------
 .../__ostream/put_character_sequence.h        | 59 +++++++++++++
 libcxx/include/iomanip                        |  4 +-
 libcxx/include/locale                         | 61 +-------------
 libcxx/include/module.modulemap               |  5 +-
 7 files changed, 152 insertions(+), 90 deletions(-)
 create mode 100644 libcxx/include/__locale_dir/pad_and_output.h
 create mode 100644 libcxx/include/__ostream/put_character_sequence.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 1610d1ee848a5f..d77770475a84a7 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -503,6 +503,7 @@ set(files
   __locale_dir/locale_base_api/openbsd.h
   __locale_dir/locale_base_api/win32.h
   __locale_dir/locale_guard.h
+  __locale_dir/pad_and_output.h
   __locale_dir/support/apple.h
   __locale_dir/support/bsd_like.h
   __locale_dir/support/freebsd.h
@@ -596,6 +597,7 @@ set(files
   __numeric/transform_reduce.h
   __ostream/basic_ostream.h
   __ostream/print.h
+  __ostream/put_character_sequence.h
   __pstl/backend.h
   __pstl/backend_fwd.h
   __pstl/backends/default.h
diff --git a/libcxx/include/__locale_dir/pad_and_output.h b/libcxx/include/__locale_dir/pad_and_output.h
new file mode 100644
index 00000000000000..c35c5df90801ef
--- /dev/null
+++ b/libcxx/include/__locale_dir/pad_and_output.h
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
+#define _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
+
+#include <__config>
+#include <ios>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT, class _OutputIterator>
+_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
+    _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
+  streamsize __sz = __oe - __ob;
+  streamsize __ns = __iob.width();
+  if (__ns > __sz)
+    __ns -= __sz;
+  else
+    __ns = 0;
+  for (; __ob < __op; ++__ob, ++__s)
+    *__s = *__ob;
+  for (; __ns; --__ns, ++__s)
+    *__s = __fl;
+  for (; __ob < __oe; ++__ob, ++__s)
+    *__s = *__ob;
+  __iob.width(0);
+  return __s;
+}
+
+template <class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
+    ostreambuf_iterator<_CharT, _Traits> __s,
+    const _CharT* __ob,
+    const _CharT* __op,
+    const _CharT* __oe,
+    ios_base& __iob,
+    _CharT __fl) {
+  if (__s.__sbuf_ == nullptr)
+    return __s;
+  streamsize __sz = __oe - __ob;
+  streamsize __ns = __iob.width();
+  if (__ns > __sz)
+    __ns -= __sz;
+  else
+    __ns = 0;
+  streamsize __np = __op - __ob;
+  if (__np > 0) {
+    if (__s.__sbuf_->sputn(__ob, __np) != __np) {
+      __s.__sbuf_ = nullptr;
+      return __s;
+    }
+  }
+  if (__ns > 0) {
+    basic_string<_CharT, _Traits> __sp(__ns, __fl);
+    if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
+      __s.__sbuf_ = nullptr;
+      return __s;
+    }
+  }
+  __np = __oe - __op;
+  if (__np > 0) {
+    if (__s.__sbuf_->sputn(__op, __np) != __np) {
+      __s.__sbuf_ = nullptr;
+      return __s;
+    }
+  }
+  __iob.width(0);
+  return __s;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index c37566d2356caf..ad43c72a3c2eae 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -16,6 +16,7 @@
 #  include <__exception/operations.h>
 #  include <__memory/shared_ptr.h>
 #  include <__memory/unique_ptr.h>
+#  include <__ostream/put_character_sequence.h>
 #  include <__system_error/error_code.h>
 #  include <__type_traits/conjunction.h>
 #  include <__type_traits/enable_if.h>
@@ -496,33 +497,6 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const
   return *this;
 }
 
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
-__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
-#  if _LIBCPP_HAS_EXCEPTIONS
-  try {
-#  endif // _LIBCPP_HAS_EXCEPTIONS
-    typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
-    if (__s) {
-      typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
-      if (std::__pad_and_output(
-              _Ip(__os),
-              __str,
-              (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
-              __str + __len,
-              __os,
-              __os.fill())
-              .failed())
-        __os.setstate(ios_base::badbit | ios_base::failbit);
-    }
-#  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
-    __os.__set_badbit_and_consider_rethrow();
-  }
-#  endif // _LIBCPP_HAS_EXCEPTIONS
-  return __os;
-}
-
 template <class _CharT, class _Traits>
 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
   return std::__put_character_sequence(__os, &__c, 1);
diff --git a/libcxx/include/__ostream/put_character_sequence.h b/libcxx/include/__ostream/put_character_sequence.h
new file mode 100644
index 00000000000000..2a143acaed6eb7
--- /dev/null
+++ b/libcxx/include/__ostream/put_character_sequence.h
@@ -0,0 +1,59 @@
+//===---------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___OSTREAM_PRINT_H
+#define _LIBCPP___OSTREAM_PRINT_H
+
+#include <__config>
+
+#if _LIBCPP_HAS_LOCALIZATION
+
+#  include <__cstddef/size_t.h>
+#  include <__fwd/ostream.h>
+#  include <__iterator/ostreambuf_iterator.h>
+#  include <__locale_dir/pad_and_output.h>
+#  include <ios>
+
+#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#    pragma GCC system_header
+#  endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
+__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
+#  if _LIBCPP_HAS_EXCEPTIONS
+  try {
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+    typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
+    if (__s) {
+      typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
+      if (std::__pad_and_output(
+              _Ip(__os),
+              __str,
+              (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
+              __str + __len,
+              __os,
+              __os.fill())
+              .failed())
+        __os.setstate(ios_base::badbit | ios_base::failbit);
+    }
+#  if _LIBCPP_HAS_EXCEPTIONS
+  } catch (...) {
+    __os.__set_badbit_and_consider_rethrow();
+  }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
+  return __os;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_HAS_LOCALIZATION
+
+#endif // _LIBCPP___OSTREAM_PRINT_H
diff --git a/libcxx/include/iomanip b/libcxx/include/iomanip
index 8c711c94f11762..1722c7a3dcc7af 100644
--- a/libcxx/include/iomanip
+++ b/libcxx/include/iomanip
@@ -46,9 +46,9 @@ template <class charT, class traits, class Allocator>
 
 #if _LIBCPP_HAS_LOCALIZATION
 
-#  include <__ostream/basic_ostream.h>
+#  include <__ostream/put_character_sequence.h>
 #  include <ios>
-#  include <istream>
+#  include <iosfwd>
 #  include <locale>
 #  include <version>
 
diff --git a/libcxx/include/locale b/libcxx/include/locale
index c887140f38c224..65f5c6ba52f736 100644
--- a/libcxx/include/locale
+++ b/libcxx/include/locale
@@ -203,6 +203,7 @@ template <class charT> class messages_byname;
 #  include <__iterator/istreambuf_iterator.h>
 #  include <__iterator/ostreambuf_iterator.h>
 #  include <__locale>
+#  include <__locale_dir/pad_and_output.h>
 #  include <__memory/unique_ptr.h>
 #  include <__type_traits/make_unsigned.h>
 #  include <cerrno>
@@ -1239,66 +1240,6 @@ protected:
 template <class _CharT, class _OutputIterator>
 locale::id num_put<_CharT, _OutputIterator>::id;
 
-template <class _CharT, class _OutputIterator>
-_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
-    _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
-  streamsize __sz = __oe - __ob;
-  streamsize __ns = __iob.width();
-  if (__ns > __sz)
-    __ns -= __sz;
-  else
-    __ns = 0;
-  for (; __ob < __op; ++__ob, ++__s)
-    *__s = *__ob;
-  for (; __ns; --__ns, ++__s)
-    *__s = __fl;
-  for (; __ob < __oe; ++__ob, ++__s)
-    *__s = *__ob;
-  __iob.width(0);
-  return __s;
-}
-
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
-    ostreambuf_iterator<_CharT, _Traits> __s,
-    const _CharT* __ob,
-    const _CharT* __op,
-    const _CharT* __oe,
-    ios_base& __iob,
-    _CharT __fl) {
-  if (__s.__sbuf_ == nullptr)
-    return __s;
-  streamsize __sz = __oe - __ob;
-  streamsize __ns = __iob.width();
-  if (__ns > __sz)
-    __ns -= __sz;
-  else
-    __ns = 0;
-  streamsize __np = __op - __ob;
-  if (__np > 0) {
-    if (__s.__sbuf_->sputn(__ob, __np) != __np) {
-      __s.__sbuf_ = nullptr;
-      return __s;
-    }
-  }
-  if (__ns > 0) {
-    basic_string<_CharT, _Traits> __sp(__ns, __fl);
-    if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
-      __s.__sbuf_ = nullptr;
-      return __s;
-    }
-  }
-  __np = __oe - __op;
-  if (__np > 0) {
-    if (__s.__sbuf_->sputn(__op, __np) != __np) {
-      __s.__sbuf_ = nullptr;
-      return __s;
-    }
-  }
-  __iob.width(0);
-  return __s;
-}
-
 template <class _CharT, class _OutputIterator>
 _OutputIterator
 num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index cd08b2810e437b..2d8d7a3cae74c7 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1460,7 +1460,9 @@ module std [system] {
 
   module locale {
     header "locale"
-    header "__locale_dir/locale_guard.h"
+
+    module locale_guard   { header "__locale_dir/locale_guard.h" }
+    module pad_and_output { header "__locale_dir/pad_and_output.h" }
 
     module support {
       header "__locale_dir/locale_base_api.h"
@@ -1641,6 +1643,7 @@ module std [system] {
       header "__ostream/print.h"
       export *
     }
+    module put_character_sequence { header "__ostream/put_character_sequence.h" }
 
     header "ostream"
     export *



More information about the libcxx-commits mailing list