Optimize format string compilation

This commit is contained in:
Victor Zverovich 2021-05-09 08:30:15 -07:00
parent 3207a8bbbf
commit 4862930845
2 changed files with 28 additions and 8 deletions

View File

@ -319,7 +319,8 @@ template <typename Char, typename T, int N> struct spec_field {
formatter<T, Char> fmt;
template <typename OutputIt, typename... Args>
constexpr OutputIt format(OutputIt out, const Args&... args) const {
constexpr FMT_INLINE OutputIt format(OutputIt out,
const Args&... args) const {
const auto& vargs =
make_format_args<basic_format_context<OutputIt, Char>>(args...);
basic_format_context<OutputIt, Char> ctx(out, vargs);
@ -556,9 +557,9 @@ template <typename CompiledFormat, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
const Args&... args) {
basic_memory_buffer<Char> buffer;
cf.format(detail::buffer_appender<Char>(buffer), args...);
return to_string(buffer);
auto s = std::basic_string<Char>();
cf.format(std::back_inserter(s), args...);
return s;
}
template <typename OutputIt, typename CompiledFormat, typename... Args,

View File

@ -1609,9 +1609,9 @@ FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) {
template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(std::is_integral<T>::value &&
!std::is_same<T, bool>::value)>
FMT_CONSTEXPR OutputIt write(OutputIt out, T value,
const basic_format_specs<Char>& specs,
locale_ref loc) {
FMT_CONSTEXPR FMT_INLINE OutputIt
write_int(OutputIt out, T value, const basic_format_specs<Char>& specs,
locale_ref loc) {
auto prefix = 0u;
auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
if (is_negative(value)) {
@ -1673,6 +1673,25 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, T value,
}
return out;
}
template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(std::is_integral<T>::value &&
!std::is_same<T, bool>::value &&
std::is_same<OutputIt, buffer_appender<Char>>::value)>
FMT_CONSTEXPR OutputIt write(OutputIt out, T value,
const basic_format_specs<Char>& specs,
locale_ref loc) {
return write_int(out, value, specs, loc);
}
// An inlined version of format_int used in format string compilation.
template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(std::is_integral<T>::value &&
!std::is_same<T, bool>::value &&
!std::is_same<OutputIt, buffer_appender<Char>>::value)>
FMT_CONSTEXPR FMT_INLINE OutputIt write(OutputIt out, T value,
const basic_format_specs<Char>& specs,
locale_ref loc) {
return write_int(out, value, specs, loc);
}
template <typename OutputIt, typename StrChar, typename Char>
FMT_CONSTEXPR OutputIt write(OutputIt out, basic_string_view<StrChar> s,
@ -2900,7 +2919,7 @@ struct formatter<T, Char,
}
template <typename FormatContext>
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
FMT_CONSTEXPR FMT_INLINE auto format(const T& val, FormatContext& ctx) const
-> decltype(ctx.out()) {
if (specs_.width_ref.kind != detail::arg_id_kind::none ||
specs_.precision_ref.kind != detail::arg_id_kind::none) {