public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH][RFC] libstdc++: Optimize std::chrono::abs
Date: Fri, 26 Aug 2022 16:14:27 +0100	[thread overview]
Message-ID: <20220826151427.166021-1-jwakely@redhat.com> (raw)

While looking into LWG 3741 I came up with this small change to
chrono::abs, which reduces how much work the compiler does to compile
it, but makes the code less clear. The current implementation is very
easy to understand, and compiling chrono::abs probably isn't a hotspot
in anybody's build. Is this worth it?

-- >8 --

This change manually inlines the call to duration::zero, the comparison
using chrono::operator< with duration arguments, the call to
duration::operator- (and the common_type instantiation it does). This
also avoids calling the duration(const duration<R2,P2>&) constructor
(and its constraint checks).

By performing the arithmetic operations directly on the Rep value we
improve compilation throughput and also runtime performance for
unoptimized builds.

libstdc++-v3/ChangeLog:

	* include/bits/chrono.h (chrono::abs): Optimize.
---
 libstdc++-v3/include/bits/chrono.h | 42 +++++++++++++++---------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h
index 05987ca09df..99d47503af3 100644
--- a/libstdc++-v3/include/bits/chrono.h
+++ b/libstdc++-v3/include/bits/chrono.h
@@ -317,6 +317,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       { };
 #endif // C++20
 
+    /// duration_values
+    template<typename _Rep>
+      struct duration_values
+      {
+	static constexpr _Rep
+	zero() noexcept
+	{ return _Rep(0); }
+
+	static constexpr _Rep
+	max() noexcept
+	{ return numeric_limits<_Rep>::max(); }
+
+	static constexpr _Rep
+	min() noexcept
+	{ return numeric_limits<_Rep>::lowest(); }
+      };
+
 #if __cplusplus >= 201703L
 # define __cpp_lib_chrono 201611L
 
@@ -365,11 +382,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Rep, typename _Period>
       constexpr
       enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
-      abs(duration<_Rep, _Period> __d)
+      abs(duration<_Rep, _Period> __d) noexcept(is_arithmetic_v<_Rep>)
       {
-	if (__d >= __d.zero())
-	  return __d;
-	return -__d;
+	if (_Rep __c = __d.count(); __c < duration_values<_Rep>::zero())
+	  return duration<_Rep, _Period>(-__c);
+	return __d;
       }
 
     // Make chrono::ceil<D> also usable as chrono::__detail::ceil<D>.
@@ -399,23 +416,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 #endif // C++17
 
-    /// duration_values
-    template<typename _Rep>
-      struct duration_values
-      {
-	static constexpr _Rep
-	zero() noexcept
-	{ return _Rep(0); }
-
-	static constexpr _Rep
-	max() noexcept
-	{ return numeric_limits<_Rep>::max(); }
-
-	static constexpr _Rep
-	min() noexcept
-	{ return numeric_limits<_Rep>::lowest(); }
-      };
-
     /// @cond undocumented
 
     template<typename _Tp>
-- 
2.37.2


                 reply	other threads:[~2022-08-26 15:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220826151427.166021-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).