From patchwork Fri Aug 26 15:14:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 57099 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6343F3850857 for ; Fri, 26 Aug 2022 15:14:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6343F3850857 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661526899; bh=+ffIIEvL/RnM1MQv4a73SXlY0ILDOQY/J2qWyJ6nlmg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Jh1jwtGvuwSnzRMq5Ls8ltqythMhvFuqHqZCiBBxKWnnzg4MReSmK01ra1QUJJRZT Jum+kbs9IeCTCVmr19jQfG9kccif3XNN3d///fwXg2KDFq140cb6i90LCPxDXNAnJ0 KVQiMbQBr1sPteAXv+pISL6RqpHIAmTBT6hvlKKA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id BDF383858403 for ; Fri, 26 Aug 2022 15:14:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BDF383858403 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-274-iHwjOfK2PKmxxCI0aaDWlg-1; Fri, 26 Aug 2022 11:14:29 -0400 X-MC-Unique: iHwjOfK2PKmxxCI0aaDWlg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F0353811E9B; Fri, 26 Aug 2022 15:14:28 +0000 (UTC) Received: from localhost (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id B66D140CF916; Fri, 26 Aug 2022 15:14:28 +0000 (UTC) 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 Message-Id: <20220826151427.166021-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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&) 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 + 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 constexpr enable_if_t::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 also usable as chrono::__detail::ceil. @@ -399,23 +416,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif // C++17 - /// duration_values - template - 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