boostorg / date_time

Boost.org date_time module
http://boost.org/libs/date_time
Boost Software License 1.0
67 stars 95 forks source link

Fix constexpr of gregorian::date::date(special_values) to improve perf #214

Closed p12tic closed 2 years ago

p12tic commented 2 years ago

Fixes #121

Turns out even when all functions without the library are prefixed with constexpr, GCC up to at least v10 does not consider this constructor as constexpr at least when generating code.

BOOST_CXX14_CONSTEXPR explicit date(special_values sv):
      date_time::date<date, gregorian_calendar, date_duration>(date_rep_type::from_special(sv))
    {
      if (sv == min_date_time)
      {
        *this = date(1400, 1, 1);
      }
      if (sv == max_date_time)
      {
        *this = date(9999, 12, 31);
      }
    }

The culprit is the assignment to *this. Refactoring the code to initialize the instance just once improves code generation significantly. Note that clang since at least v11 does not exhibit this problem.

codecov[bot] commented 2 years ago

Codecov Report

Merging #214 (a8478d4) into develop (f972dc9) will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #214   +/-   ##
========================================
  Coverage    91.74%   91.75%           
========================================
  Files           77       77           
  Lines         4823     4827    +4     
========================================
+ Hits          4425     4429    +4     
  Misses         398      398           
Impacted Files Coverage Δ
include/boost/date_time/gregorian/greg_date.hpp 100.00% <100.00%> (ø)
...boost/date_time/posix_time/posix_time_duration.hpp 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f972dc9...a8478d4. Read the comment docs.

JeffGarland commented 2 years ago

Looks good - thanks!