public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Add noexcept to std::to_string overloads that don't allocate
Date: Thu, 16 Sep 2021 23:05:55 +0100	[thread overview]
Message-ID: <YUO/wzZlAitEXoAS@redhat.com> (raw)

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

When the values is guaranteed to fit in the SSO buffer we know the
string won't allocate, so the function can be noexcept. For 32-bit
integers, we know they need no more than 9 bytes (or 10 with a minus
sign) and the SSO buffer is 15 bytes.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	* include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI]
	(to_string): Add noexcept if the type width is 32 bits or less.

Tested x86_64-linux. Committed to trunk.


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

commit 9d813ddd978aff75001d53fe55ff15e9167bb4d0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Sep 15 21:40:20 2021

    libstdc++: Add noexcept to std::to_string overloads that don't allocate
    
    When the values is guaranteed to fit in the SSO buffer we know the
    string won't allocate, so the function can be noexcept. For 32-bit
    integers, we know they need no more than 9 bytes (or 10 with a minus
    sign) and the SSO buffer is 15 bytes.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI]
            (to_string): Add noexcept if the type width is 32 bits or less.

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index b61fe05efcf..24c454d863a 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -3718,6 +3718,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
   inline string
   to_string(int __val)
+#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
+  noexcept // any 32-bit value fits in the SSO buffer
+#endif
   {
     const bool __neg = __val < 0;
     const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
@@ -3729,6 +3732,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
   inline string
   to_string(unsigned __val)
+#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
+  noexcept // any 32-bit value fits in the SSO buffer
+#endif
   {
     string __str(__detail::__to_chars_len(__val), '\0');
     __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
@@ -3737,6 +3743,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
   inline string
   to_string(long __val)
+#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
+  noexcept // any 32-bit value fits in the SSO buffer
+#endif
   {
     const bool __neg = __val < 0;
     const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
@@ -3748,6 +3757,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 
   inline string
   to_string(unsigned long __val)
+#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
+  noexcept // any 32-bit value fits in the SSO buffer
+#endif
   {
     string __str(__detail::__to_chars_len(__val), '\0');
     __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);

                 reply	other threads:[~2021-09-16 22:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=YUO/wzZlAitEXoAS@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /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).