public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-6324] libstdc++: Optimize net::ip::address_v4::to_string() Date: Fri, 24 Feb 2023 14:26:09 +0000 (GMT) [thread overview] Message-ID: <20230224142609.4164E3850851@sourceware.org> (raw) https://gcc.gnu.org/g:363f0ef50bd143fce77a1c7905b0ed707f4a9358 commit r13-6324-g363f0ef50bd143fce77a1c7905b0ed707f4a9358 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Feb 23 17:39:33 2023 +0000 libstdc++: Optimize net::ip::address_v4::to_string() 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. Diff: --- libstdc++-v3/include/experimental/internet | 28 +++++++++++++++------- .../net/internet/address/v4/members.cc | 11 +++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 707370d5611..08bd0db4bb2 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -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: diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc index df19b11804d..c40a8103664 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc @@ -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
reply other threads:[~2023-02-24 14:26 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=20230224142609.4164E3850851@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).