From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by sourceware.org (Postfix) with ESMTPS id BF6F83987C16; Fri, 26 Feb 2021 16:00:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BF6F83987C16 IronPort-SDR: NMiEfSTR8MH3aDPY7Ta1zqezbfcLXEDdJ1BUjdwooGDEpi73k+0fLUq4uxWVg+XH/g295sI/CS RmtP1gaEKzTw== X-IronPort-AV: E=McAfee;i="6000,8403,9907"; a="247332263" X-IronPort-AV: E=Sophos;i="5.81,208,1610438400"; d="scan'208";a="247332263" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2021 08:00:51 -0800 IronPort-SDR: rny8fMUeif17TugNIFe/wfIXqsXjwZihbo/kD/BnCdie2wTHXhOV+6p5PBz6Kqg+jcAn7U5oFo h4qjFcXKxYIg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,208,1610438400"; d="scan'208";a="434413579" Received: from tjmaciei-desk.jf.intel.com (HELO tjmaciei-ctnr.jf.intel.com) ([10.54.75.8]) by fmsmga002.fm.intel.com with ESMTP; 26 Feb 2021 08:00:41 -0800 From: Thiago Macieira To: libstdc++@gcc.gnu.org Cc: gcc-patches@gcc.gnu.org Subject: [PATCH 5/5] barrier: optimise by not having the hasher in a loop Date: Fri, 26 Feb 2021 07:59:37 -0800 Message-Id: <20210226155937.621324-5-thiago.macieira@intel.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210226155937.621324-1-thiago.macieira@intel.com> References: <1968544.UC5HiB4uFJ@tjmaciei-mobl1> <20210226155937.621324-1-thiago.macieira@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Fri, 26 Feb 2021 16:00:56 -0000 Our thread's ID does not change so we don't have to get it every time and hash it every time. --- libstdc++-v3/include/std/barrier | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/std/barrier b/libstdc++-v3/include/std/barrier index ae058bd3dc3..eb31a89b175 100644 --- a/libstdc++-v3/include/std/barrier +++ b/libstdc++-v3/include/std/barrier @@ -101,12 +101,10 @@ It looks different from literature pseudocode for two main reasons: } bool - _M_arrive(__barrier_phase_t __old_phase) + _M_arrive(__barrier_phase_t __old_phase, size_t __current) { size_t __current_expected = _M_expected; - std::hash __hasher; - size_t __current = __hasher(std::this_thread::get_id()) - % ((_M_expected + 1) >> 1); + __current %= ((__current_expected + 1) >> 1); const auto __half_step = _S_add_to_phase(__old_phase, 1); const auto __full_step = _S_add_to_phase(__old_phase, 2); @@ -167,11 +165,13 @@ It looks different from literature pseudocode for two main reasons: [[nodiscard]] arrival_token arrive(ptrdiff_t __update) { + std::hash __hasher; + size_t __current = __hasher(std::this_thread::get_id()); __atomic_phase_ref_t __phase(_M_phase); const auto __old_phase = __phase.load(memory_order_relaxed); for(; __update; --__update) { - if(_M_arrive(__old_phase)) + if(_M_arrive(__old_phase, __current)) { _M_completion(); _M_expected += _M_expected_adjustment.load(memory_order_relaxed); -- 2.30.1