]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize net::ip::address_v4::to_string()
authorJonathan Wakely <jwakely@redhat.com>
Thu, 23 Feb 2023 17:39:33 +0000 (17:39 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 24 Feb 2023 14:23:35 +0000 (14:23 +0000)
This is an order of magnitude faster than calling inet_ntop (and not
only because we now avoid allocating a string that is one byte larger
than the SSO buffer).

libstdc++-v3/ChangeLog:

* include/experimental/internet (address_v4::to_string):
Optimize.
* testsuite/experimental/net/internet/address/v4/members.cc:
Check more addresses.

libstdc++-v3/include/experimental/internet
libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc

index 707370d5611cbc73ee4a38e288c4bbba7164213e..08bd0db4bb27fe3d64fe99d7fa1ca52504f3e6a3 100644 (file)
@@ -44,6 +44,7 @@
 #include <sstream>
 #include <cstdint>
 #include <experimental/string_view>
+#include <bits/charconv.h>
 #ifdef _GLIBCXX_HAVE_UNISTD_H
 # include <unistd.h>
 #endif
@@ -241,17 +242,28 @@ namespace ip
       __string_with<_Allocator>
       to_string(const _Allocator& __a = _Allocator()) const
       {
-#ifdef _GLIBCXX_HAVE_ARPA_INET_H
+       auto __write = [__addr = to_uint()](char* __p, size_t __n) {
+         auto __to_chars = [](char* __p, uint8_t __v) {
+           unsigned __n = __v >= 100u ? 3 : __v >= 10u ? 2 : 1;
+           std::__detail::__to_chars_10_impl(__p, __n, __v);
+           return __p + __n;
+         };
+         const auto __begin = __p;
+         __p = __to_chars(__p, uint8_t(__addr >> 24));
+         for (int __i = 2; __i >= 0; __i--) {
+           *__p++ = '.';
+           __p = __to_chars(__p, uint8_t(__addr >> (__i * 8)));
+         }
+         return __p - __begin;
+       };
        __string_with<_Allocator> __str(__a);
-       __str.resize(INET_ADDRSTRLEN);
-       if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
-         __str.erase(__str.find('\0'));
-       else
-         __str.resize(0);
-       return __str;
+#if __cpp_lib_string_resize_and_overwrite
+       __str.resize_and_overwrite(15, __write);
 #else
-       std::__throw_system_error((int)__unsupported_err());
+       __str.resize(15);
+       __str.resize(__write(&__str.front(), 15));
 #endif
+       return __str;
       }
 
     // static members:
index df19b11804d964608871a8a1aaf0211408a3f4a2..c40a8103664b55be91b9013c895c6b13022e42ee 100644 (file)
@@ -22,6 +22,7 @@
 #include <experimental/internet>
 #include <sstream>
 #include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
 
 using std::experimental::net::ip::address_v4;
 
@@ -100,6 +101,16 @@ test04()
   VERIFY( address_v4::any().to_string() == "0.0.0.0" );
   VERIFY( address_v4::loopback().to_string() == "127.0.0.1" );
   VERIFY( address_v4::broadcast().to_string() == "255.255.255.255" );
+  using b = address_v4::bytes_type;
+  VERIFY( address_v4(b(1, 23, 45, 67)).to_string() == "1.23.45.67" );
+  VERIFY( address_v4(b(12, 34, 56, 78)).to_string() == "12.34.56.78" );
+  VERIFY( address_v4(b(123, 4, 5, 6)).to_string() == "123.4.5.6" );
+  VERIFY( address_v4(b(123, 234, 124, 235)).to_string() == "123.234.124.235" );
+
+  __gnu_test::uneq_allocator<char> alloc(123);
+  auto str = address_v4(b(12, 34, 56, 78)).to_string(alloc);
+  VERIFY(str.get_allocator().get_personality() == alloc.get_personality());
+  VERIFY( str == "12.34.56.78" );
 }
 
 void