[libcxx-commits] [libcxx] [libc++] Optimize vector growing of trivially relocatable types (PR #76657)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 11 09:59:26 PST 2024


================
@@ -273,6 +274,9 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }
 };
 
+template <class _Tp>
+struct __libcpp_is_trivially_relocatable<unique_ptr<_Tp>> : true_type {};
+
----------------
ldionne wrote:

Also, I think we might need to care about user-defined specializations, since they are technically allowed. I might have been okay with saying "meh, let's ignore them", but consider something like this:

```c++
template <class T>
struct relative { T val; };

template <class T>
class std::unique_ptr<relative<T>> {
public:
    // same as unique_ptr

private:
    std::ptrdiff_t offset_from_this_;
};

std::unique_ptr<relative<int>> p = std::make_unique<relative<int>>({42});
// p can be stored in a memory mapped file
```

Is this a good idea? I don't know, but that's not too far fetched of a use case, and I'm pretty sure it's valid. However it would break *horribly* if we assume that this is trivially relocatable.

So I think we need to use the `__primary_template` here too.


https://github.com/llvm/llvm-project/pull/76657


More information about the libcxx-commits mailing list