Skip to content

Commit e3e0169

Browse files
mordantecopybara-github
authored andcommitted
[libc++][format] Implements P3107R5 in <format>. (#86713)
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`` NOKEYCHECK=True GitOrigin-RevId: f08df56d3af3e67ab39ebbfb62be3ed3f86d0c9d
1 parent 17ee1e1 commit e3e0169

File tree

10 files changed

+337
-10
lines changed

10 files changed

+337
-10
lines changed

include/__format/formatter.h

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

4040
# if _LIBCPP_STD_VER >= 23
4141

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

include/__format/formatter_bool.h

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

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

7478
_LIBCPP_END_NAMESPACE_STD
7579

include/__format/formatter_char.h

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

8484
template <>
8585
struct _LIBCPP_TEMPLATE_VIS formatter<wchar_t, wchar_t> : public __formatter_char<wchar_t> {};
86-
8786
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
8887

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

9199
_LIBCPP_END_NAMESPACE_STD

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

include/__format/formatter_integer.h

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

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

93124
_LIBCPP_END_NAMESPACE_STD
94125

include/__format/formatter_pointer.h

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

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

7078
_LIBCPP_END_NAMESPACE_STD

include/__format/formatter_string.h

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

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

148173
_LIBCPP_END_NAMESPACE_STD
149174

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...>

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)