From patchwork Fri Mar 8 00:10:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 86948 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 CF0133857356 for ; Fri, 8 Mar 2024 00:11:04 +0000 (GMT) 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 1FD103858D35 for ; Fri, 8 Mar 2024 00:10:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1FD103858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1FD103858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709856623; cv=none; b=bmotBJgv60HAr6HEVD8g6QJtPZEri+WI+qeQ1bRZRA7xhduCIN76Bn4O4ivvso5Fh56VT1MkBNkuebCL5nQDThbiLJSPyPPxnLCK+y1hkUdijwVsPt1sKWqsUgNUW06QBxM5wxLGUTmAMqHTXDEDIdqAeQS6ACg31F4+ER7Ptig= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709856623; c=relaxed/simple; bh=2Dg9fDgFMBfbrCWzhDji/0Q+c04Eiw8YiOJ+Qlb1DKA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VvRF19s9GirjaRB6wui3MyNa5jtR+79dwYrBiaDQIZAIKtUDbVW5deMbLUVo/MKe1eBlQXSRogzCjR15iPmnY9z+cXaIcY1/5qoWFbvsvsnEJAKRT6RfwXZIpNRs5W8C/UOJ3t1SQTOklVgRPt0gLAnkkdbOBPLagWxK7ZXwwB8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709856617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LDd+wWww2Q6ptGYLXLiRoTx/CEtHteSaZ0gDHydAtDo=; b=bGQUGj1oVElPBdp3XmouX7ieMPThPU/oLje+U04boMZSTPHogOKmrHLc1iNLiivJwFlUFh sJiCADJOQM0ehWcOvVF9VSbo17uRMgB+uBChd4PglpTVvnv0A5e1IXlU3HbRzMsmW1MGL0 aLGSA5ySdKLEW8omcaWyVl6xPPEWCOY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-320-25EaVV3cOQq11-fD22Oq2Q-1; Thu, 07 Mar 2024 19:10:16 -0500 X-MC-Unique: 25EaVV3cOQq11-fD22Oq2Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E2291800264; Fri, 8 Mar 2024 00:10:15 +0000 (UTC) Received: from localhost (unknown [10.42.28.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF318492BC6; Fri, 8 Mar 2024 00:10:15 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use std::from_chars to speed up parsing subsecond durations Date: Fri, 8 Mar 2024 00:10:00 +0000 Message-ID: <20240308001015.684795-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- With std::from_chars we can parse subsecond durations much faster than with std::num_get, as shown in the microbenchmarks below. We were using std::num_get and std::numpunct in order to parse a number with the locale's decimal point character. But we copy the chars from the input stream into a new buffer anyway, so we can replace the locale's decimal point with '.' in that buffer, and then we can use std::from_chars on it. Benchmark Time CPU Iterations ---------------------------------------------------------- from_chars_millisec 158 ns 158 ns 4524046 num_get_millisec 192 ns 192 ns 3644626 from_chars_microsec 164 ns 163 ns 4330627 num_get_microsec 205 ns 205 ns 3413452 from_chars_nanosec 173 ns 173 ns 4072653 num_get_nanosec 227 ns 227 ns 3105161 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (_Parser::operator()): Use std::from_chars to parse fractional seconds. --- libstdc++-v3/include/bits/chrono_io.h | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index b8f0657bee9..412e8b83fb7 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -37,6 +37,7 @@ #include // ostringstream #include // setw, setfill #include +#include // from_chars #include @@ -3597,13 +3598,17 @@ namespace __detail __err |= ios_base::eofbit; else { - auto& __np = use_facet>(__loc); - auto __dp = __np.decimal_point(); + _CharT __dp = '.'; + if (__loc != locale::classic()) + { + auto& __np = use_facet>(__loc); + __dp = __np.decimal_point(); + } _CharT __c = _Traits::to_char_type(__i); if (__c == __dp) { (void) __is.get(); - __buf.put(__c); + __buf.put('.'); int __prec = hh_mm_ss<_Duration>::fractional_width; do @@ -3618,14 +3623,17 @@ namespace __detail } } - if (!__is_failed(__err)) + if (!__is_failed(__err)) [[likely]] { - auto& __ng = use_facet>(__loc); - long double __val; - ios_base::iostate __err2{}; - __ng.get(__buf, {}, __buf, __err2, __val); - if (__is_failed(__err2)) [[unlikely]] - __err |= __err2; + long double __val{}; + string __str = std::move(__buf).str(); + auto __first = __str.data(); + auto __last = __first + __str.size(); + using enum chars_format; + auto [ptr, ec] = std::from_chars(__first, __last, + __val, fixed); + if ((bool)ec || ptr != __last) [[unlikely]] + __err |= ios_base::failbit; else { duration __fs(__val);