From patchwork Mon Sep 13 12:47:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?5YiY5Y+v?= X-Patchwork-Id: 44946 X-Patchwork-Delegate: jwakely.gcc@gmail.com 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 828BA3857823 for ; Mon, 13 Sep 2021 12:47:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 828BA3857823 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1631537267; bh=j0oWY7iQ/LHpWBP5Hqt06di2YVrMrxM80WTtzZnAouI=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TCmxZWSBcXl/D88zlZIsgFwV+B7udR3l8ogJQ08wdMOiZu/cb/CLDR4HIMnteEY+9 qloLw/s1287JAALsjpt88Zf72a4ROTOZ9mZwscGOaaNYxYJrU1PnnCuA9xAVpq1Bsy +wtxCoQcl+MnrbAKq3jWr5Oj47iQ+edjTtD6nwxM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id D7AD83858C60 for ; Mon, 13 Sep 2021 12:47:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D7AD83858C60 Received: by mail-lj1-x235.google.com with SMTP id i28so17067858ljm.7 for ; Mon, 13 Sep 2021 05:47:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:date:message-id:subject:to; bh=l5KGEVY/w9IqFPjSgfvhqPrAAFlH9X5KDpwfXM4NX7M=; b=UhQrbmmUBt1Z5vV2j2GyW8x+7bh+gaI4FogQDPe+SGZV8kSmocVK1Gb2Bn7UNkT/pk GvKQejYaYWJAfgHd0V/UXitTLLhqOAWygUzMESRUMyNy+VWHkyXHjo6VWfzROxDGLK2T N2siG6Ed9HxWJOJzRVBo8TSoOfca5Vb/WqSNotkqox7mHrQWQt7yRE8Yzz9O+/DSLxjz Ha9kssi+olvt2akR9UgakMuGVa93XdvmpKMml9sV4hiQ8bDOov/lCi//dEjNRmTmILSc NSmHVLbIKnPQl/Zf73+uqsXYiB5GgpsEGF0QD2Mg3EpsOFqPhM5hrCewNHK8p/Cg/y/s ZXbQ== X-Gm-Message-State: AOAM531wYp8+DhEiiGLqYe+aMx3vCn7KIJBC2Y4NYM3m4P6waN8aJpQe XCNGDSxcrUyajUkw1SgAtRnRAsM/x1yMHkZRYeoD3v3SLqc= X-Google-Smtp-Source: ABdhPJwYT2Yyip7HtPmsdVyxo3H084Fv87oKQE30ZgHNJW5LXWOkDZu4TaPphJWEUMmS2SvaL9TiRbC2Wg79qog12MU= X-Received: by 2002:a2e:a591:: with SMTP id m17mr10573199ljp.113.1631537234028; Mon, 13 Sep 2021 05:47:14 -0700 (PDT) Received: from 349319672217 named unknown by gmailapi.google.com with HTTPREST; Mon, 13 Sep 2021 05:47:13 -0700 Mime-Version: 1.0 Date: Mon, 13 Sep 2021 05:47:13 -0700 Message-ID: Subject: [PATCH] libstdc++-v3: Optimize 'to_string' with numeric_limits instead of __to_chars_len To: "gcc-patches@gcc.gnu.org" X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, HTML_FONT_LOW_CONTRAST, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: =?utf-8?b?5YiY5Y+vIHZpYSBHY2MtcGF0Y2hlcw==?= From: =?utf-8?b?5YiY5Y+v?= Reply-To: =?utf-8?b?5YiY5Y+v?= Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! Gcc5 has implemented 'SSO'. The length of small string local buffer is 15, which is enough to store an integer. So we can use 'numeric_limits::digits+1' to get the max length of int instead of dynamically obtaining the length of the integer through __to_chars_len. In this way, I will get a performance improvement of about 15%. Before optimization: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------------------------- # to_string Int2String 191785 ns 191780 ns 3645 # to_string Unsigned2String 159605 ns 159599 ns 4367 After optimization: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------------------------- # to_string Int2String 159382 ns 159381 ns 4354 # to_string Unsigned2String 136744 ns 136742 ns 5144 2020-09-13 Liuke libstdc++-v3/ChangeLog: * include/bits/basic_string.h: Use std::numeric_limits::digits10 instead of __to_chars_len. Diff: diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b61fe05efcf..5cbec537b2f 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -39,6 +39,7 @@ #include #include #include +#include #if __cplusplus >= 201103L #include @@ -3721,7 +3722,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { const bool __neg = __val < 0; const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val; - const auto __len = __detail::__to_chars_len(__uval); + const auto __len = std::numeric_limits::digits10 + 1; string __str(__neg + __len, '-'); __detail::__to_chars_10_impl(&__str[__neg], __len, __uval); return __str; @@ -3730,7 +3731,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline string to_string(unsigned __val) { - string __str(__detail::__to_chars_len(__val), '\0'); + string __str(std::numeric_limits::digits10 + 1;, '\0'); __detail::__to_chars_10_impl(&__str[0], __str.size(), __val); return __str; }