Skip to content

Commit 764834d

Browse files
authored
[libc++] Remove <istream> and <ostream> includes from <iomanip> (#116223)
This reduces the include time of `<filesystem>` by ~50ms.
1 parent 1636580 commit 764834d

File tree

9 files changed

+167
-98
lines changed

9 files changed

+167
-98
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ set(files
503503
__locale_dir/locale_base_api/openbsd.h
504504
__locale_dir/locale_base_api/win32.h
505505
__locale_dir/locale_guard.h
506+
__locale_dir/pad_and_output.h
506507
__locale_dir/support/apple.h
507508
__locale_dir/support/bsd_like.h
508509
__locale_dir/support/freebsd.h
@@ -596,6 +597,7 @@ set(files
596597
__numeric/transform_reduce.h
597598
__ostream/basic_ostream.h
598599
__ostream/print.h
600+
__ostream/put_character_sequence.h
599601
__pstl/backend.h
600602
__pstl/backend_fwd.h
601603
__pstl/backends/default.h
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
10+
#define _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
11+
12+
#include <__config>
13+
#include <ios>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
template <class _CharT, class _OutputIterator>
22+
_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
23+
_OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
24+
streamsize __sz = __oe - __ob;
25+
streamsize __ns = __iob.width();
26+
if (__ns > __sz)
27+
__ns -= __sz;
28+
else
29+
__ns = 0;
30+
for (; __ob < __op; ++__ob, ++__s)
31+
*__s = *__ob;
32+
for (; __ns; --__ns, ++__s)
33+
*__s = __fl;
34+
for (; __ob < __oe; ++__ob, ++__s)
35+
*__s = *__ob;
36+
__iob.width(0);
37+
return __s;
38+
}
39+
40+
template <class _CharT, class _Traits>
41+
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
42+
ostreambuf_iterator<_CharT, _Traits> __s,
43+
const _CharT* __ob,
44+
const _CharT* __op,
45+
const _CharT* __oe,
46+
ios_base& __iob,
47+
_CharT __fl) {
48+
if (__s.__sbuf_ == nullptr)
49+
return __s;
50+
streamsize __sz = __oe - __ob;
51+
streamsize __ns = __iob.width();
52+
if (__ns > __sz)
53+
__ns -= __sz;
54+
else
55+
__ns = 0;
56+
streamsize __np = __op - __ob;
57+
if (__np > 0) {
58+
if (__s.__sbuf_->sputn(__ob, __np) != __np) {
59+
__s.__sbuf_ = nullptr;
60+
return __s;
61+
}
62+
}
63+
if (__ns > 0) {
64+
basic_string<_CharT, _Traits> __sp(__ns, __fl);
65+
if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
66+
__s.__sbuf_ = nullptr;
67+
return __s;
68+
}
69+
}
70+
__np = __oe - __op;
71+
if (__np > 0) {
72+
if (__s.__sbuf_->sputn(__op, __np) != __np) {
73+
__s.__sbuf_ = nullptr;
74+
return __s;
75+
}
76+
}
77+
__iob.width(0);
78+
return __s;
79+
}
80+
81+
_LIBCPP_END_NAMESPACE_STD
82+
83+
#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H

libcxx/include/__ostream/basic_ostream.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# include <__exception/operations.h>
1717
# include <__memory/shared_ptr.h>
1818
# include <__memory/unique_ptr.h>
19+
# include <__ostream/put_character_sequence.h>
1920
# include <__system_error/error_code.h>
2021
# include <__type_traits/conjunction.h>
2122
# include <__type_traits/enable_if.h>
@@ -496,33 +497,6 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const
496497
return *this;
497498
}
498499

499-
template <class _CharT, class _Traits>
500-
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
501-
__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
502-
# if _LIBCPP_HAS_EXCEPTIONS
503-
try {
504-
# endif // _LIBCPP_HAS_EXCEPTIONS
505-
typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
506-
if (__s) {
507-
typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
508-
if (std::__pad_and_output(
509-
_Ip(__os),
510-
__str,
511-
(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
512-
__str + __len,
513-
__os,
514-
__os.fill())
515-
.failed())
516-
__os.setstate(ios_base::badbit | ios_base::failbit);
517-
}
518-
# if _LIBCPP_HAS_EXCEPTIONS
519-
} catch (...) {
520-
__os.__set_badbit_and_consider_rethrow();
521-
}
522-
# endif // _LIBCPP_HAS_EXCEPTIONS
523-
return __os;
524-
}
525-
526500
template <class _CharT, class _Traits>
527501
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
528502
return std::__put_character_sequence(__os, &__c, 1);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===---------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___OSTREAM_PUT_CHARACTER_SEQUENCE_H
10+
#define _LIBCPP___OSTREAM_PUT_CHARACTER_SEQUENCE_H
11+
12+
#include <__config>
13+
14+
#if _LIBCPP_HAS_LOCALIZATION
15+
16+
# include <__cstddef/size_t.h>
17+
# include <__fwd/ostream.h>
18+
# include <__iterator/ostreambuf_iterator.h>
19+
# include <__locale_dir/pad_and_output.h>
20+
# include <ios>
21+
22+
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23+
# pragma GCC system_header
24+
# endif
25+
26+
_LIBCPP_BEGIN_NAMESPACE_STD
27+
28+
template <class _CharT, class _Traits>
29+
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
30+
__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
31+
# if _LIBCPP_HAS_EXCEPTIONS
32+
try {
33+
# endif // _LIBCPP_HAS_EXCEPTIONS
34+
typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
35+
if (__s) {
36+
typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
37+
if (std::__pad_and_output(
38+
_Ip(__os),
39+
__str,
40+
(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
41+
__str + __len,
42+
__os,
43+
__os.fill())
44+
.failed())
45+
__os.setstate(ios_base::badbit | ios_base::failbit);
46+
}
47+
# if _LIBCPP_HAS_EXCEPTIONS
48+
} catch (...) {
49+
__os.__set_badbit_and_consider_rethrow();
50+
}
51+
# endif // _LIBCPP_HAS_EXCEPTIONS
52+
return __os;
53+
}
54+
55+
_LIBCPP_END_NAMESPACE_STD
56+
57+
#endif // _LIBCPP_HAS_LOCALIZATION
58+
59+
#endif // _LIBCPP___OSTREAM_PUT_CHARACTER_SEQUENCE_H

libcxx/include/iomanip

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ template <class charT, class traits, class Allocator>
4646

4747
#if _LIBCPP_HAS_LOCALIZATION
4848

49-
# include <__ostream/basic_ostream.h>
49+
# include <__ostream/put_character_sequence.h>
5050
# include <ios>
51-
# include <istream>
51+
# include <iosfwd>
5252
# include <locale>
5353
# include <version>
5454

@@ -547,4 +547,19 @@ _LIBCPP_END_NAMESPACE_STD
547547

548548
#endif // _LIBCPP_HAS_LOCALIZATION
549549

550+
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
551+
# include <array>
552+
# include <bitset>
553+
# include <deque>
554+
# include <format>
555+
# include <functional>
556+
# include <istream>
557+
# include <ostream>
558+
# include <print>
559+
# include <queue>
560+
# include <stack>
561+
# include <unordered_map>
562+
# include <vector>
563+
#endif
564+
550565
#endif // _LIBCPP_IOMANIP

libcxx/include/locale

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ template <class charT> class messages_byname;
203203
# include <__iterator/istreambuf_iterator.h>
204204
# include <__iterator/ostreambuf_iterator.h>
205205
# include <__locale>
206+
# include <__locale_dir/pad_and_output.h>
206207
# include <__memory/unique_ptr.h>
207208
# include <__type_traits/make_unsigned.h>
208209
# include <cerrno>
@@ -1239,66 +1240,6 @@ protected:
12391240
template <class _CharT, class _OutputIterator>
12401241
locale::id num_put<_CharT, _OutputIterator>::id;
12411242

1242-
template <class _CharT, class _OutputIterator>
1243-
_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
1244-
_OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
1245-
streamsize __sz = __oe - __ob;
1246-
streamsize __ns = __iob.width();
1247-
if (__ns > __sz)
1248-
__ns -= __sz;
1249-
else
1250-
__ns = 0;
1251-
for (; __ob < __op; ++__ob, ++__s)
1252-
*__s = *__ob;
1253-
for (; __ns; --__ns, ++__s)
1254-
*__s = __fl;
1255-
for (; __ob < __oe; ++__ob, ++__s)
1256-
*__s = *__ob;
1257-
__iob.width(0);
1258-
return __s;
1259-
}
1260-
1261-
template <class _CharT, class _Traits>
1262-
_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
1263-
ostreambuf_iterator<_CharT, _Traits> __s,
1264-
const _CharT* __ob,
1265-
const _CharT* __op,
1266-
const _CharT* __oe,
1267-
ios_base& __iob,
1268-
_CharT __fl) {
1269-
if (__s.__sbuf_ == nullptr)
1270-
return __s;
1271-
streamsize __sz = __oe - __ob;
1272-
streamsize __ns = __iob.width();
1273-
if (__ns > __sz)
1274-
__ns -= __sz;
1275-
else
1276-
__ns = 0;
1277-
streamsize __np = __op - __ob;
1278-
if (__np > 0) {
1279-
if (__s.__sbuf_->sputn(__ob, __np) != __np) {
1280-
__s.__sbuf_ = nullptr;
1281-
return __s;
1282-
}
1283-
}
1284-
if (__ns > 0) {
1285-
basic_string<_CharT, _Traits> __sp(__ns, __fl);
1286-
if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
1287-
__s.__sbuf_ = nullptr;
1288-
return __s;
1289-
}
1290-
}
1291-
__np = __oe - __op;
1292-
if (__np > 0) {
1293-
if (__s.__sbuf_->sputn(__op, __np) != __np) {
1294-
__s.__sbuf_ = nullptr;
1295-
return __s;
1296-
}
1297-
}
1298-
__iob.width(0);
1299-
return __s;
1300-
}
1301-
13021243
template <class _CharT, class _OutputIterator>
13031244
_OutputIterator
13041245
num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {

libcxx/include/module.modulemap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,9 @@ module std [system] {
14601460

14611461
module locale {
14621462
header "locale"
1463-
header "__locale_dir/locale_guard.h"
1463+
1464+
module locale_guard { header "__locale_dir/locale_guard.h" }
1465+
module pad_and_output { header "__locale_dir/pad_and_output.h" }
14641466

14651467
module support {
14661468
header "__locale_dir/locale_base_api.h"
@@ -1641,6 +1643,7 @@ module std [system] {
16411643
header "__ostream/print.h"
16421644
export *
16431645
}
1646+
module put_character_sequence { header "__ostream/put_character_sequence.h" }
16441647

16451648
header "ostream"
16461649
export *

libcxx/test/libcxx/transitive_includes/cxx23.csv

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ experimental/utility initializer_list
331331
experimental/utility limits
332332
experimental/utility utility
333333
experimental/utility version
334-
filesystem bitset
335334
filesystem cctype
336335
filesystem cerrno
337336
filesystem climits
@@ -349,7 +348,6 @@ filesystem initializer_list
349348
filesystem iomanip
350349
filesystem ios
351350
filesystem iosfwd
352-
filesystem istream
353351
filesystem limits
354352
filesystem locale
355353
filesystem new
@@ -492,7 +490,6 @@ future tuple
492490
future typeinfo
493491
future version
494492
initializer_list version
495-
iomanip bitset
496493
iomanip cctype
497494
iomanip cerrno
498495
iomanip climits
@@ -509,7 +506,6 @@ iomanip cwctype
509506
iomanip initializer_list
510507
iomanip ios
511508
iomanip iosfwd
512-
iomanip istream
513509
iomanip limits
514510
iomanip locale
515511
iomanip new

libcxx/test/libcxx/transitive_includes/cxx26.csv

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ experimental/utility initializer_list
331331
experimental/utility limits
332332
experimental/utility utility
333333
experimental/utility version
334-
filesystem bitset
335334
filesystem cctype
336335
filesystem cerrno
337336
filesystem climits
@@ -349,7 +348,6 @@ filesystem initializer_list
349348
filesystem iomanip
350349
filesystem ios
351350
filesystem iosfwd
352-
filesystem istream
353351
filesystem limits
354352
filesystem locale
355353
filesystem new
@@ -491,7 +489,6 @@ future tuple
491489
future typeinfo
492490
future version
493491
initializer_list version
494-
iomanip bitset
495492
iomanip cctype
496493
iomanip cerrno
497494
iomanip climits
@@ -508,7 +505,6 @@ iomanip cwctype
508505
iomanip initializer_list
509506
iomanip ios
510507
iomanip iosfwd
511-
iomanip istream
512508
iomanip limits
513509
iomanip locale
514510
iomanip new

0 commit comments

Comments
 (0)