From 486293084523302ecec255f0ef132fd3e333d0ab Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 9 May 2021 08:30:15 -0700 Subject: [PATCH] Optimize format string compilation --- include/fmt/compile.h | 9 +++++---- include/fmt/format.h | 27 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 7ff2fe86..9c0ff9bd 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -319,7 +319,8 @@ template struct spec_field { formatter fmt; template - 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>(args...); basic_format_context ctx(out, vargs); @@ -556,9 +557,9 @@ template ::value)> FMT_INLINE std::basic_string format(const CompiledFormat& cf, const Args&... args) { - basic_memory_buffer buffer; - cf.format(detail::buffer_appender(buffer), args...); - return to_string(buffer); + auto s = std::basic_string(); + cf.format(std::back_inserter(s), args...); + return s; } template ::value && !std::is_same::value)> -FMT_CONSTEXPR OutputIt write(OutputIt out, T value, - const basic_format_specs& specs, - locale_ref loc) { +FMT_CONSTEXPR FMT_INLINE OutputIt +write_int(OutputIt out, T value, const basic_format_specs& specs, + locale_ref loc) { auto prefix = 0u; auto abs_value = static_cast>(value); if (is_negative(value)) { @@ -1673,6 +1673,25 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, T value, } return out; } +template ::value && + !std::is_same::value && + std::is_same>::value)> +FMT_CONSTEXPR OutputIt write(OutputIt out, T value, + const basic_format_specs& specs, + locale_ref loc) { + return write_int(out, value, specs, loc); +} +// An inlined version of format_int used in format string compilation. +template ::value && + !std::is_same::value && + !std::is_same>::value)> +FMT_CONSTEXPR FMT_INLINE OutputIt write(OutputIt out, T value, + const basic_format_specs& specs, + locale_ref loc) { + return write_int(out, value, specs, loc); +} template FMT_CONSTEXPR OutputIt write(OutputIt out, basic_string_view s, @@ -2900,7 +2919,7 @@ struct formatter - 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) {