Skip to content

Commit 8c306d1

Browse files
committed
[libc++][format] Implements P3107R5 in <format>.
This adds the new std::enable_nonlocking_formatter_optimization trait in <format>. This trait will be used in std::print to implement the performance benefits. Implements parts of - P3107R5 - Permit an efficient implementation of ``std::print``
1 parent cf6e62d commit 8c306d1

File tree

10 files changed

+336
-10
lines changed

10 files changed

+336
-10
lines changed

libcxx/include/__format/formatter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ struct _LIBCPP_TEMPLATE_VIS formatter {
4040

4141
# if _LIBCPP_STD_VER >= 23
4242

43+
template <class _Tp>
44+
constexpr bool enable_nonlocking_formatter_optimization = false;
45+
4346
template <class _Tp>
4447
_LIBCPP_HIDE_FROM_ABI constexpr void __set_debug_format(_Tp& __formatter) {
4548
if constexpr (requires { __formatter.set_debug_format(); })

libcxx/include/__format/formatter_bool.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ struct _LIBCPP_TEMPLATE_VIS formatter<bool, _CharT> {
7070
__format_spec::__parser<_CharT> __parser_;
7171
};
7272

73-
#endif //_LIBCPP_STD_VER >= 20
73+
# if _LIBCPP_STD_VER >= 23
74+
template <>
75+
inline constexpr bool enable_nonlocking_formatter_optimization<bool> = true;
76+
# endif //_LIBCPP_STD_VER >= 23
77+
#endif //_LIBCPP_STD_VER >= 20
7478

7579
_LIBCPP_END_NAMESPACE_STD
7680

libcxx/include/__format/formatter_char.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,17 @@ struct _LIBCPP_TEMPLATE_VIS formatter<char, wchar_t> : public __formatter_char<w
8484

8585
template <>
8686
struct _LIBCPP_TEMPLATE_VIS formatter<wchar_t, wchar_t> : public __formatter_char<wchar_t> {};
87-
8887
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
8988

89+
# if _LIBCPP_STD_VER >= 23
90+
template <>
91+
inline constexpr bool enable_nonlocking_formatter_optimization<char> = true;
92+
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
93+
template <>
94+
inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t> = true;
95+
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
96+
# endif //_LIBCPP_STD_VER >= 23
97+
9098
#endif //_LIBCPP_STD_VER >= 20
9199

92100
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__format/formatter_floating_point.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,14 @@ struct _LIBCPP_TEMPLATE_VIS formatter<double, _CharT> : public __formatter_float
774774
template <__fmt_char_type _CharT>
775775
struct _LIBCPP_TEMPLATE_VIS formatter<long double, _CharT> : public __formatter_floating_point<_CharT> {};
776776

777+
# if _LIBCPP_STD_VER >= 23
778+
template <>
779+
inline constexpr bool enable_nonlocking_formatter_optimization<float> = true;
780+
template <>
781+
inline constexpr bool enable_nonlocking_formatter_optimization<double> = true;
782+
template <>
783+
inline constexpr bool enable_nonlocking_formatter_optimization<long double> = true;
784+
# endif //_LIBCPP_STD_VER >= 23
777785
#endif //_LIBCPP_STD_VER >= 20
778786

779787
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__format/formatter_integer.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,38 @@ template <__fmt_char_type _CharT>
8989
struct _LIBCPP_TEMPLATE_VIS formatter<__uint128_t, _CharT> : public __formatter_integer<_CharT> {};
9090
# endif
9191

92-
#endif //_LIBCPP_STD_VER >= 20
92+
# if _LIBCPP_STD_VER >= 23
93+
template <>
94+
inline constexpr bool enable_nonlocking_formatter_optimization<signed char> = true;
95+
template <>
96+
inline constexpr bool enable_nonlocking_formatter_optimization<short> = true;
97+
template <>
98+
inline constexpr bool enable_nonlocking_formatter_optimization<int> = true;
99+
template <>
100+
inline constexpr bool enable_nonlocking_formatter_optimization<long> = true;
101+
template <>
102+
inline constexpr bool enable_nonlocking_formatter_optimization<long long> = true;
103+
# ifndef _LIBCPP_HAS_NO_INT128
104+
template <>
105+
inline constexpr bool enable_nonlocking_formatter_optimization<__int128_t> = true;
106+
# endif
107+
108+
template <>
109+
inline constexpr bool enable_nonlocking_formatter_optimization<unsigned char> = true;
110+
template <>
111+
inline constexpr bool enable_nonlocking_formatter_optimization<unsigned short> = true;
112+
template <>
113+
inline constexpr bool enable_nonlocking_formatter_optimization<unsigned> = true;
114+
template <>
115+
inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long> = true;
116+
template <>
117+
inline constexpr bool enable_nonlocking_formatter_optimization<unsigned long long> = true;
118+
# ifndef _LIBCPP_HAS_NO_INT128
119+
template <>
120+
inline constexpr bool enable_nonlocking_formatter_optimization<__uint128_t> = true;
121+
# endif
122+
# endif //_LIBCPP_STD_VER >= 23
123+
#endif //_LIBCPP_STD_VER >= 20
93124

94125
_LIBCPP_END_NAMESPACE_STD
95126

libcxx/include/__format/formatter_pointer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ struct _LIBCPP_TEMPLATE_VIS formatter<void*, _CharT> : public __formatter_pointe
6666
template <__fmt_char_type _CharT>
6767
struct _LIBCPP_TEMPLATE_VIS formatter<const void*, _CharT> : public __formatter_pointer<_CharT> {};
6868

69+
# if _LIBCPP_STD_VER >= 23
70+
template <>
71+
inline constexpr bool enable_nonlocking_formatter_optimization<nullptr_t> = true;
72+
template <>
73+
inline constexpr bool enable_nonlocking_formatter_optimization<void*> = true;
74+
template <>
75+
inline constexpr bool enable_nonlocking_formatter_optimization<const void*> = true;
76+
# endif //_LIBCPP_STD_VER >= 23
6977
#endif //_LIBCPP_STD_VER >= 20
7078

7179
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__format/formatter_string.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,32 @@ struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT
144144
}
145145
};
146146

147-
#endif //_LIBCPP_STD_VER >= 20
147+
# if _LIBCPP_STD_VER >= 23
148+
template <>
149+
inline constexpr bool enable_nonlocking_formatter_optimization<char*> = true;
150+
template <>
151+
inline constexpr bool enable_nonlocking_formatter_optimization<const char*> = true;
152+
template <size_t _Size>
153+
inline constexpr bool enable_nonlocking_formatter_optimization<char[_Size]> = true;
154+
template <class _Traits, class _Allocator>
155+
inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<char, _Traits, _Allocator>> = true;
156+
template <class _Traits>
157+
inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<char, _Traits>> = true;
158+
159+
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
160+
template <>
161+
inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t*> = true;
162+
template <>
163+
inline constexpr bool enable_nonlocking_formatter_optimization<const wchar_t*> = true;
164+
template <size_t _Size>
165+
inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t[_Size]> = true;
166+
template <class _Traits, class _Allocator>
167+
inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Traits, _Allocator>> = true;
168+
template <class _Traits>
169+
inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Traits>> = true;
170+
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
171+
# endif //_LIBCPP_STD_VER >= 23
172+
#endif //_LIBCPP_STD_VER >= 20
148173

149174
_LIBCPP_END_NAMESPACE_STD
150175

libcxx/include/format

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ namespace std {
126126
// [format.formatter], formatter
127127
template<class T, class charT = char> struct formatter;
128128
129+
template<class T>
130+
constexpr bool enable_nonlocking_formatter_optimization = false; // since C++23
131+
129132
// [format.parse.ctx], class template basic_format_parse_context
130133
template<class charT> class basic_format_parse_context;
131134
using format_parse_context = basic_format_parse_context<char>;
132135
using wformat_parse_context = basic_format_parse_context<wchar_t>;
133136
134137
// [format.range], formatting of ranges
135138
// [format.range.fmtkind], variable template format_kind
136-
enum class range_format { // since C++23
139+
enum class range_format { // since C++23
137140
disabled,
138141
map,
139142
set,
@@ -143,20 +146,20 @@ namespace std {
143146
};
144147
145148
template<class R>
146-
constexpr unspecified format_kind = unspecified; // since C++23
149+
constexpr unspecified format_kind = unspecified; // since C++23
147150
148151
template<ranges::input_range R>
149152
requires same_as<R, remove_cvref_t<R>>
150-
constexpr range_format format_kind<R> = see below; // since C++23
153+
constexpr range_format format_kind<R> = see below; // since C++23
151154
152155
// [format.range.formatter], class template range_formatter
153156
template<class T, class charT = char>
154157
requires same_as<remove_cvref_t<T>, T> && formattable<T, charT>
155-
class range_formatter; // since C++23
158+
class range_formatter; // since C++23
156159
157160
// [format.range.fmtdef], class template range-default-formatter
158161
template<range_format K, ranges::input_range R, class charT>
159-
struct range-default-formatter; // exposition only, since C++23
162+
struct range-default-formatter; // exposition only, since C++23
160163
161164
// [format.range.fmtmap], [format.range.fmtset], [format.range.fmtstr],
162165
// specializations for maps, sets, and strings
@@ -173,7 +176,7 @@ namespace std {
173176
see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); // Deprecated in C++26
174177
175178
// [format.arg.store], class template format-arg-store
176-
template<class Context, class... Args> struct format-arg-store; // exposition only
179+
template<class Context, class... Args> struct format-arg-store; // exposition only
177180
178181
template<class Context = format_context, class... Args>
179182
format-arg-store<Context, Args...>

libcxx/modules/std/format.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export namespace std {
4646
using std::formatter;
4747

4848
#if _LIBCPP_STD_VER >= 23
49+
using std::enable_nonlocking_formatter_optimization;
50+
4951
// [format.formattable], concept formattable
5052
using std::formattable;
5153
#endif

0 commit comments

Comments
 (0)