public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: 刘可 <liuke.gehry@bytedance.com>
Cc: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>,
	gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [External] Re: [PATCH] libstdc++: Optimize 'to_string<int>' with numeric_limits instead of __to_chars_len
Date: Wed, 15 Sep 2021 20:44:57 +0100	[thread overview]
Message-ID: <CACb0b4k+OkMZhu_LBhEZ3E2ENVmqANWvVJ4+kT6Ynu387rCXqQ@mail.gmail.com> (raw)
In-Reply-To: <CALf+iit=6Te5ib9KUStJCb+Bz+7AtrjQjO9v49e=jc1-_7CqrA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 503 bytes --]

N.B. Please CC *both* the libstdc++ list and the gcc-patches list, as
per https://gcc.gnu.org/lists.html

On Wed, 15 Sept 2021 at 14:02, 刘可 wrote:
>
> Thank you for your review, and I apologize for my mistake. I have updated and tested it!

Hmm, it doesn't work though. How did you test it?

For to_string(int) the string will be padded with '-' characters, i.e.
std::to_string(1) returns "1---------" and to_string(-1) returns
"-1---------" and to_string(100) returns "1--------00" !

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2702 bytes --]

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index b61fe05efcf..e1fd42aea1a 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -3716,41 +3716,82 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
   // DR 1261. Insufficent overloads for to_string / to_wstring
 
+  namespace __detail
+  {
+    template<typename _Tp>
+      inline unsigned
+      __to_string_len(_Tp __val) noexcept
+      {
+#if _GLIBCXX_USE_CXX11_ABI
+	// Any 32-bit integer value fits in the 15-byte SSO buffer,
+	// so don't bother counting how many chars are needed.
+	if _GLIBCXX17_CONSTEXPR (sizeof(_Tp) * __CHAR_BIT_  <= 32)
+	  return 9; // std::numeric_limits<uint32_t>::digits10
+	else
+#endif
+	return __detail::__to_chars_len(__uval);
+      }
+
+    inline void
+    __to_string_trim(string& __s) noexcept
+    {
+#if _GLIBCXX_USE_CXX11_ABI
+      ???
+#endif
+    }
+  }
+
   inline string
   to_string(int __val)
+#if _GLIBCXX_USE_CXX11_ABI
+  noexcept
+#endif
   {
     const bool __neg = __val < 0;
     const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
-    const auto __len = __detail::__to_chars_len(__uval);
+    const auto __len = __detail::__to_string_len(__uval);
     string __str(__neg + __len, '-');
     __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
+    __detail::__to_string_trim(__str);
     return __str;
   }
 
   inline string
   to_string(unsigned __val)
+#if _GLIBCXX_USE_CXX11_ABI
+  noexcept
+#endif
   {
-    string __str(__detail::__to_chars_len(__val), '\0');
+    string __str(__detail::__to_string_len(__val), '\0');
     __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
+    __detail::__to_string_trim(__str);
     return __str;
   }
 
   inline string
   to_string(long __val)
+#if _GLIBCXX_USE_CXX11_ABI && __SIZEOF_LONG__ == __SIZEOF_INT__
+  noexcept
+#endif
   {
     const bool __neg = __val < 0;
     const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
-    const auto __len = __detail::__to_chars_len(__uval);
+    const auto __len = __detail::__to_string_len(__uval);
     string __str(__neg + __len, '-');
     __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
+    __detail::__to_string_trim(__str);
     return __str;
   }
 
   inline string
   to_string(unsigned long __val)
+#if _GLIBCXX_USE_CXX11_ABI && __SIZEOF_LONG__ == __SIZEOF_INT__
+  noexcept
+#endif
   {
-    string __str(__detail::__to_chars_len(__val), '\0');
+    string __str(__detail::__to_string_len(__val), '\0');
     __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
+    __detail::__to_string_trim(__str);
     return __str;
   }
 

      reply	other threads:[~2021-09-15 19:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-15 13:02 刘可
2021-09-15 19:44 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CACb0b4k+OkMZhu_LBhEZ3E2ENVmqANWvVJ4+kT6Ynu387rCXqQ@mail.gmail.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=liuke.gehry@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).