{"id":2226663,"url":"http://patchwork.ozlabs.org/api/patches/2226663/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.1@forge-stage.sourceware.org/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.1@forge-stage.sourceware.org>","list_archive_url":null,"date":"2026-04-22T18:03:45","name":"[v1,1/1] libstdc++: optimize weak_ptr converting constructor/assignment","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"550c3ee5c0bad5b9d456c0fffcf670d13f80e516","submitter":{"id":93222,"url":"http://patchwork.ozlabs.org/api/people/93222/?format=json","name":"peppe via Sourceware Forge","email":"forge-bot+peppe@forge-stage.sourceware.org"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.1@forge-stage.sourceware.org/mbox/","series":[{"id":501065,"url":"http://patchwork.ozlabs.org/api/series/501065/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=501065","date":"2026-04-22T18:03:44","name":"WIP: libstdc++: use is_virtual_base_of in std::weak_ptr operations","version":1,"mbox":"http://patchwork.ozlabs.org/series/501065/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2226663/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2226663/checks/","tags":{},"related":[],"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org; dmarc=none (p=none dis=none)\n header.from=forge-stage.sourceware.org","sourceware.org;\n spf=pass smtp.mailfrom=forge-stage.sourceware.org","server2.sourceware.org;\n arc=none smtp.remote-ip=38.145.34.39"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g16h93RxJz1y2d\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 23 Apr 2026 04:10:01 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id A70D74B920F6\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 18:09:59 +0000 (GMT)","from forge-stage.sourceware.org (vm08.sourceware.org [38.145.34.39])\n by sourceware.org (Postfix) with ESMTPS id 2BD5B4BBC0A9\n for <gcc-patches@gcc.gnu.org>; Wed, 22 Apr 2026 18:05:21 +0000 (GMT)","from forge-stage.sourceware.org (localhost [IPv6:::1])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256)\n (No client certificate requested)\n by forge-stage.sourceware.org (Postfix) with ESMTPS id EE3AC433CB\n for <gcc-patches@gcc.gnu.org>; Wed, 22 Apr 2026 18:05:06 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org A70D74B920F6","OpenDKIM Filter v2.11.0 sourceware.org 2BD5B4BBC0A9"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 2BD5B4BBC0A9","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 2BD5B4BBC0A9","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776881121; cv=none;\n b=mVnkLqR8sQrFq1XUBist9n0UzhUQBTjWDRrUoJ0MqS5fn/h1lUST0tE+IHta3L4uEKDDtBUIFum2Kx5dSfh/7w/vjUUrdeQ845IWzbPmbCh+r6SXM/RMC0lOCgEg4rKfbc0lahb7b9xiMmweFQzSGEKblVtYDiuGFTaw++sulLo=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776881121; c=relaxed/simple;\n bh=UNcXQ5x2cobR8LIakRWRH7pjg5amT/zQuE/BMYZTYy8=;\n h=From:Date:Subject:To:Message-ID;\n b=bPQXWtBZWteIThgK4ly2k17rIabd4MJW8yxtsn8+bKiSVrIcf2eBSRzC220lpQtHR3nDa1C04AHEFRJ6Obs9CIF39Z/UcRaOPx3andJpa4BD2YeihtrlZrp1VkFxLEqVy8QSvSMfF2CbYjJ9eWcX5kdk3kdkuO6j5r/VQbjzoEk=","ARC-Authentication-Results":"i=1; server2.sourceware.org","From":"peppe via Sourceware Forge <forge-bot+peppe@forge-stage.sourceware.org>","Date":"Wed, 22 Apr 2026 18:03:45 +0000","Subject":"[PATCH v1 1/1] libstdc++: optimize weak_ptr converting\n constructor/assignment","To":"gcc-patches mailing list <gcc-patches@gcc.gnu.org>","Message-ID":"\n <bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.1@forge-stage.sourceware.org>","X-Mailer":"batrachomyomachia","X-Pull-Request-Organization":"gcc","X-Pull-Request-Repository":"gcc-TEST","X-Pull-Request":"https://forge.sourceware.org/gcc/gcc-TEST/pulls/41","References":"\n <bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.0@forge-stage.sourceware.org>","In-Reply-To":"\n <bmm.hhunvhjcqm.gcc.gcc-TEST.peppe.41.1.0@forge-stage.sourceware.org>","X-Patch-URL":"\n https://forge.sourceware.org/peppe/gcc-TEST/commit/a081ba7fda52fe2a74923166a4e97976292a9155","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Reply-To":"gcc-patches mailing list <gcc-patches@gcc.gnu.org>,\n peppe@noreply.localhost","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"From: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>\n\nConverting a weak_ptr<Derived> to a weak_ptr<Base> requires calling\nlock() on the source object in the general case.\n\nAlthough the source weak_ptr<Derived> does contain a raw pointer to\nDerived, we can't just get it and (up)cast it to Base, as that will\ndereference the pointer in case Base is a virtual base class of Derived.\n\nWe don't know if the managed object is still alive, and therefore if\nthis operation is safe to do; we therefore temporarily lock() the source\nweak_ptr, do the cast using the resulting shared_ptr, and then discard\nthis shared_ptr. Simply checking the strong counter isn't sufficient,\nbecause if multiple threads are involved then we'd have a race / TOCTOU\nproblem; the object may get destroyed after we check the strong counter\nand before we cast the pointer.\n\nHowever lock() is not necessary if we know that Base is *not* a virtual\nbase class of Derived; in this case we can avoid the relatively\nexpensive call to lock() and just cast the pointer. This commit uses\nthe newly added builtin to detect this case and optimize std::weak_ptr's\nconverting constructors and assignment operations.\n\nApart from non-virtual bases, there's also another couple of interesting\ncases where we can also avoid locking. Specifically:\n\n1) converting a weak_ptr<T[N]> to a weak_ptr<T cv[]>;\n2) converting a weak_ptr<T*> to a weak_ptr<T const * const> or similar.\n\nSince this logic is going to be used by multiple places, I've\ncentralized it in a new static helper.\n\nlibstdc++-v3/ChangeLog:\n\n\t* include/bits/shared_ptr_base.h (__weak_ptr): Avoid calling\n\tlock() when converting or assigning a weak_ptr<Derived> to\n\ta weak_ptr<Base> in case Base is not a virtual base of Derived.\n\tThis logic is centralized in _S_safe_upcast, called by the\n\tvarious converting constructors/assignment operators.\n\t(_S_safe_upcast): New helper function.\n\t* testsuite/20_util/weak_ptr/cons/virtual_bases.cc: New test.\n---\n libstdc++-v3/include/bits/shared_ptr_base.h   | 52 ++++++++++--\n .../20_util/weak_ptr/cons/virtual_bases.cc    | 80 +++++++++++++++++++\n 2 files changed, 125 insertions(+), 7 deletions(-)\n create mode 100644 libstdc++-v3/testsuite/20_util/weak_ptr/cons/virtual_bases.cc","diff":"diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h\nindex b4be1b49e4da..5f3ae33848a6 100644\n--- a/libstdc++-v3/include/bits/shared_ptr_base.h\n+++ b/libstdc++-v3/include/bits/shared_ptr_base.h\n@@ -1986,6 +1986,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n   template<typename _Tp, _Lock_policy _Lp>\n     class __weak_ptr\n     {\n+    public:\n+      using element_type = typename remove_extent<_Tp>::type;\n+\n+    private:\n       template<typename _Yp, typename _Res = void>\n \tusing _Compatible = typename\n \t  enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;\n@@ -1994,9 +1998,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n       template<typename _Yp>\n \tusing _Assignable = _Compatible<_Yp, __weak_ptr&>;\n \n-    public:\n-      using element_type = typename remove_extent<_Tp>::type;\n+#pragma GCC diagnostic push\n+#pragma GCC diagnostic ignored \"-Wc++17-extensions\" // if constexpr\n+      // Helper for construction/assignment:\n+      template<typename _Yp>\n+\tstatic element_type* _S_safe_upcast(const __weak_ptr<_Yp, _Lp>& __r)\n+\t{\n+\t  // We know that _Yp and _Tp are compatible, that is, either\n+\t  // _Yp* is convertible to _Tp* or _Yp is U[N] and _Tp is U cv [].\n+\n+\t  // If _Yp is the same as _Tp after removing extents and cv\n+\t  // qualifications, there's no pointer adjustments to do. This\n+\t  // also allows us to support incomplete types.\n+\t  using _At = typename remove_cv<typename remove_extent<_Tp>::type>::type;\n+\t  using _Bt = typename remove_cv<typename remove_extent<_Yp>::type>::type;\n+\t  if constexpr (is_same<_At, _Bt>::value)\n+\t    return __r._M_ptr;\n+\t  // If they're not the same type, but they're both scalars,\n+\t  // we again don't need any adjustment. This allows us to support e.g.\n+\t  // pointers to a differently cv qualified type X.\n+\t  else if constexpr (__and_<is_scalar<_At>, is_scalar<_Bt>>::value)\n+\t    return __r._M_ptr;\n+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)\n+\t  // If _Tp is not a virtual base class of _Yp, the pointer\n+\t  // conversion does not require dereferencing __r._M_ptr; just\n+\t  // rely on the implicit conversion.\n+\t  else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))\n+\t    return __r._M_ptr;\n+#endif\n+\t  // Expensive path; must lock() and do the pointer conversion while\n+\t  // a shared_ptr keeps the pointee alive (because we may need\n+\t  // to dereference).\n+\t  else\n+\t    return __r.lock().get();\n+\t}\n+#pragma GCC diagnostic pop\n \n+    public:\n       constexpr __weak_ptr() noexcept\n       : _M_ptr(nullptr), _M_refcount()\n       { }\n@@ -2021,8 +2059,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n       // in multithreaded programs __r._M_ptr may be invalidated at any point.\n       template<typename _Yp, typename = _Compatible<_Yp>>\n \t__weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept\n-\t: _M_refcount(__r._M_refcount)\n-        { _M_ptr = __r.lock().get(); }\n+\t: _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)\n+        { }\n \n       template<typename _Yp, typename = _Compatible<_Yp>>\n \t__weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept\n@@ -2035,7 +2073,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \n       template<typename _Yp, typename = _Compatible<_Yp>>\n \t__weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept\n-\t: _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))\n+\t: _M_ptr(_S_safe_upcast(__r)), _M_refcount(std::move(__r._M_refcount))\n         { __r._M_ptr = nullptr; }\n \n       __weak_ptr&\n@@ -2045,7 +2083,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \t_Assignable<_Yp>\n \toperator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept\n \t{\n-\t  _M_ptr = __r.lock().get();\n+\t  _M_ptr = _S_safe_upcast(__r);\n \t  _M_refcount = __r._M_refcount;\n \t  return *this;\n \t}\n@@ -2070,7 +2108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \t_Assignable<_Yp>\n \toperator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept\n \t{\n-\t  _M_ptr = __r.lock().get();\n+\t  _M_ptr = _S_safe_upcast(__r);\n \t  _M_refcount = std::move(__r._M_refcount);\n \t  __r._M_ptr = nullptr;\n \t  return *this;\ndiff --git a/libstdc++-v3/testsuite/20_util/weak_ptr/cons/virtual_bases.cc b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/virtual_bases.cc\nnew file mode 100644\nindex 000000000000..ac3e4bce9da5\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/virtual_bases.cc\n@@ -0,0 +1,80 @@\n+// { dg-do run { target c++11 } }\n+\n+#include <memory>\n+#include <testsuite_hooks.h>\n+\n+struct BaseBase { virtual ~BaseBase() = default; };\n+struct Base : BaseBase { virtual ~Base() = default; };\n+struct Derived1 : Base { virtual ~Derived1() = default; };\n+struct Derived2 : virtual Base { virtual ~Derived2() = default; };\n+struct Derived3 : virtual Base { virtual ~Derived3() = default; };\n+struct Derived4 : Derived2, Derived3 { virtual ~Derived4() = default; };\n+struct Derived5 : Derived4 { virtual ~Derived5() = default; };\n+\n+template<typename T>\n+void test01()\n+{\n+  std::shared_ptr<T> ptr(new T);\n+  VERIFY(ptr);\n+\n+  std::weak_ptr<T> wptr1 = ptr;\n+  VERIFY(wptr1.lock());\n+\n+  std::weak_ptr<Base> wptr2 = ptr;\n+  VERIFY(wptr2.lock());\n+\n+  std::weak_ptr<Base> wptr3 = wptr1;\n+  VERIFY(wptr3.lock());\n+\n+  std::weak_ptr<BaseBase> wptr4 = ptr;\n+  VERIFY(wptr4.lock());\n+\n+  std::weak_ptr<BaseBase> wptr5 = std::move(wptr1);\n+  VERIFY(wptr5.lock());\n+\n+  ptr.reset();\n+\n+  VERIFY(!wptr1.lock());\n+  VERIFY(!wptr2.lock());\n+  VERIFY(!wptr3.lock());\n+  VERIFY(!wptr4.lock());\n+  VERIFY(!wptr5.lock());\n+}\n+\n+template<typename T>\n+void test02()\n+{\n+  std::shared_ptr<T> ptr(new T);\n+  VERIFY(ptr);\n+\n+  std::weak_ptr<T> wptr1 = ptr;\n+  VERIFY(wptr1.lock());\n+\n+  std::weak_ptr<Base> wptr2 = ptr;\n+  VERIFY(wptr2.lock());\n+\n+  ptr.reset();\n+\n+  std::weak_ptr<Base> wptr3 = wptr1;\n+  std::weak_ptr<BaseBase> wptr4 = wptr1;\n+  std::weak_ptr<BaseBase> wptr5 = std::move(wptr1);\n+\n+  VERIFY(!wptr1.lock());\n+  VERIFY(!wptr2.lock());\n+  VERIFY(!wptr3.lock());\n+  VERIFY(!wptr4.lock());\n+  VERIFY(!wptr5.lock());\n+}\n+\n+int main()\n+{\n+  test01<Derived1>();\n+  test01<Derived2>();\n+  test01<Derived4>();\n+  test01<Derived5>();\n+\n+  test02<Derived1>();\n+  test02<Derived2>();\n+  test02<Derived4>();\n+  test02<Derived5>();\n+}\n","prefixes":["v1","1/1"]}