From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E8B223858D38; Tue, 23 Apr 2024 08:52:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8B223858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1713862349; bh=pCVBOf1YWHkFJSpO0s61gu7PUpTFptLCBM/RgK8b5UM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=CBYmAJsfal67JAuIKazLe1/2dgg078ZRF0EJfg7FqZxAPBdQExByzRfiykZEShUE4 B92xJzHBfzDG4wj5hOJArNda0dMAwKF4PwxKGvOkQ74JddwLcMTGojx5L8g+uNJ/ia tYlaMLXwhByOUAz+/OXDIVi4gR5jlvoWG9ZrrYtk= From: "hubicka at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/114821] _M_realloc_append should use memcpy instead of loop to copy data when possible Date: Tue, 23 Apr 2024 08:52:29 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: hubicka at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114821 --- Comment #2 from Jan Hubicka --- What I am shooting for is to optimize it later in loop distribution. We can recognize memcpy loop if we can figure out that source and destination memo= ry are different. We can help here with restrict, but I was bit lost in how to get them done. This seems to do the trick, but for some reason I get memmove diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index 7f84da31578..1a6223ea892 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -1130,7 +1130,58 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } return __result + __count; } + + template + _GLIBCXX20_CONSTEXPR + inline __enable_if_t::value, _Tp*> + __relocate_a(_Tp * __restrict __first, _Tp *__last, + _Tp * __restrict __result, _Allocator& __alloc) noexcept + { + ptrdiff_t __count =3D __last - __first; + if (__count > 0) + { +#ifdef __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + { + for (; __first !=3D __last; ++__first, (void)++__result) + { + // manually inline relocate_object_a to not lose restrict qualifiers + typedef std::allocator_traits<_Allocator> __traits; + __traits::construct(__alloc, __result, std::move(*__first= )); + __traits::destroy(__alloc, std::__addressof(*__first)); + } + return __result; + } #endif + __builtin_memcpy(__result, __first, __count * sizeof(_Tp)); + } + return __result + __count; + } +#endif + + template + _GLIBCXX20_CONSTEXPR +#if _GLIBCXX_HOSTED + inline __enable_if_t::value, _Tp*> +#else + inline _Tp * +#endif + __relocate_a(_Tp * __restrict __first, _Tp *__last, + _Tp * __restrict __result, _Allocator& __alloc) + noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc, + __result, std::move(*__first))) + && noexcept(std::allocator_traits<_Allocator>::destroy( + __alloc, std::__addressof(*__first)))) + { + for (; __first !=3D __last; ++__first, (void)++__result) + { + // manually inline relocate_object_a to not lose restrict qualifi= ers + typedef std::allocator_traits<_Allocator> __traits; + __traits::construct(__alloc, __result, std::move(*__first)); + __traits::destroy(__alloc, std::__addressof(*__first)); + } + return __result; + } template =