Bug 110739 - std::format for chrono types compiles very slowly
Summary: std::format for chrono types compiles very slowly
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 13.1.1
: P3 normal
Target Milestone: ---
Assignee: Jonathan Wakely
URL:
Keywords: compile-time-hog
Depends on:
Blocks:
 
Reported: 2023-07-19 11:33 UTC by Jonathan Wakely
Modified: 2023-07-25 10:52 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-07-19 00:00:00


Attachments
Instantiate less code for chrono formatters (5.60 KB, patch)
2023-07-19 11:40 UTC, Jonathan Wakely
Details | Diff
Instantiate less code for chrono formatters, take 2 (3.64 KB, patch)
2023-07-19 12:55 UTC, Jonathan Wakely
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2023-07-19 11:33:18 UTC
Some of the compilation overhead is unavoidable, because we need to parse the format string during constant evaluation.

But as it says in this comment in bits/chrono_io.h:

      // TODO this function template is instantiated for every different _Tp.
      // Consider creating a polymorphic interface for calendar types so
      // that we instantiate fewer different specializations. Similar to
      // _Sink_iter for std::format. Replace each _S_year, _S_day etc. with
      // member functions of that type.
      template<typename _Tp, typename _FormatContext>
	typename _FormatContext::iterator
	_M_format(const _Tp& __t, _FormatContext& __fc,
		  bool __is_neg = false) const
Comment 1 Jonathan Wakely 2023-07-19 11:40:15 UTC
Created attachment 55579 [details]
Instantiate less code for chrono formatters

           * include/bits/chrono_io.h (__formatter_chrono): Use
            if-constexpr to avoid instantiating function bodies for types
            that can never be used with those functions.

This patch helps a little bit, but less than I'd hoped it would.
Comment 2 Jonathan Wakely 2023-07-19 12:55:58 UTC
Created attachment 55580 [details]
Instantiate less code for chrono formatters, take 2

In this patch the converters like _S_date and _S_hms are called by _M_format before passing the argument to _M_x, _M_d_e etc. so that those function templates are only called with arguments that are already the right type. This reduces the number of different instantiations of those function templates.

But this doesn't really help either.
Comment 3 Jonathan Wakely 2023-07-25 10:52:49 UTC
Explicit instantiation declarations+definitions might help, both in <chrono> and <format>.