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: 785 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp280901wro; Fri, 26 Aug 2022 08:15:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR4I5tH+fGiMoE7TVOKIkCNbc1GkTMgevFuxIbL9olmLIAKABUganhLtULwMtAVqioOjoli5 X-Received: by 2002:a17:906:a0d3:b0:73d:be5b:2b52 with SMTP id bh19-20020a170906a0d300b0073dbe5b2b52mr5804920ejb.727.1661526926250; Fri, 26 Aug 2022 08:15:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661526926; cv=none; d=google.com; s=arc-20160816; b=MC3CqiyAYmENttrtxujBSbtEWHd2GfcBlHTVj6w3BhTdUc4FhsZ4PaAZEsc+QenQju TGnLNIOIiOKaQRaOfY5a1vZ/ReBfPEBPmPvA3QAbXOvK7jBqLKb7jgD16aGRvT1jGYLl WMHNKWdhW0DP0WFrc0NQAcomis4MEyiPpHueZMLqLw0rA330pmuhfvjkKsBLCWkccZt6 d+/FmU5uYaodYBotHZiz5wrdOt1NAt3svNLaN2/uW0PShi6T3c0hJaajgMRwODdAo64s 0gERhY7H5rk+SRLjelmkejQUynwvT/7353wRn10SSnQRaSrjFK7xKfc+cn31Iq2gu+po Ltlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=+ffIIEvL/RnM1MQv4a73SXlY0ILDOQY/J2qWyJ6nlmg=; b=hI4SF3o0tAqSHTi2JolmrRbHDjl7skghZXEqz0HsAUfk6LoEzB+qpXSc9dusi02RoC Q3rgeYiyhtxvI8NL/XRQKIO5Q+9vpanT6tXp9WT9AIE40Ne+7TvYe1tJpiJHwC6svm3s GaIezzZKdg3XH0KC4Ry5Z5XoTMn0F8SRmZ4nrsscH+Qo3WoIDikpwY+QcrduZlT0FVGg JraXIq7ESXNW4Y/uWGe0Jyb9JIsp3zImoQ7EI2Lf2Dx1DDahDCMPdDPqukd/lvJmrrtZ jA+bM/zWP6OEw5yNidGcSuu0wXQFzu1KGyWuTQT/TwLtBTb8eoZwCMzno03EGorCIOQZ S39g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P07kfexT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dr3-20020a170907720300b0073d6c39d4bcsi1567082ejc.653.2022.08.26.08.15.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 08:15:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P07kfexT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59E993851156 for ; Fri, 26 Aug 2022 15:15:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59E993851156 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661526914; 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=P07kfexTMQamEJe+j4tMDMfqCGXJki+6/rDp204Dvzf6S4Yl5+G8YUSPpVXkUj6VG jEf/U3l08tzTSqoDuLaqJ8SFDyEbkVIY1tBAj86qC8X8sXkBcY7KlnFzWvpY7TskZO tYpa5lbO3y3Uy//q5sKp6H0WK6lT/hWEBZbxWNRg= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742237258180535871?= X-GMAIL-MSGID: =?utf-8?q?1742237258180535871?= 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