On Mon, Aug 19, 2019 at 10:25 AM Yusuke Suzuki <ysuzuki@apple.com> wrote:
Hello WebKit folks!

I would like to announce that I’ve just landed the patch which introduces `WTF::makeUnique<T>` and `WTF::makeUniqueWithoutFastMallocCheck<T>` in https://trac.webkit.org/changeset/248846.
They are drop-in-replacement to std::make_unique<T>, and we should not use std::make_unique<T> after that patch is introduced.
I’m planning to add cpplint check for `std::make_unique` to avoid the use of that.

The motivation behind this change is the following.

1. Our typical way of allocating heap memory is three-fold. Using containers (Vector etc.), RefCounted, and std::unique_ptr.
2. Containers and RefCounted are covered well by FastMalloc.
3. But std::unique_ptr case, we missed using FastMalloc in many places so far.

Even in very recently written code, we missed FastMalloc annotation. For example, we sometimes create a data structure just like a struct, and allocate it with make_unique.

struct XXXData {
    ...
};

m_data = std::make_unique<XXXData>();

We missed WTF_MAKE_STRUCT_FAST_ALLOCATED annotation in XXXData so frequently so that the allocation of XXXData ends up being allocated from system-malloc.

This WTF::makeUnique adds one `static_assert` over std::make_unique: the static_assert ensures T is FastMalloced or IsoHeap-allocated.
Otherwise, we see compile-error.

Could WTF::makeUnique simply use FastMalloc by default? We could then remove most of these messy annotations.

This would require replacing std::unique_ptr with a type that knows how to free the objects correctly (bring back OwnPtr!) but that doesn't seem like a big deal. It wouldn't play well with mixed use of OwnPtr and new/delete but that should be avoided in any case.


   antti
 
This mechanism surprisingly found so many classes that do not have WTF_MAKE_FAST_ALLOCATED / WTF_MAKE_STRUCT_FAST_ALLOCATED in our code base.

If the type T comes from ThirdParty and if we cannot annotate T with FAST_ALLOCATED, we can use WTF::makeUniqueWithoutFastMallocCheck explicitly as a fallback.

More detailed explanation behind why we took this design (instead of allocating FastMalloced-memory automatically when using makeUnique<T>() etc.) is described in ChangeLog in https://trac.webkit.org/changeset/248846/webkit.
I already annotated missed structs / classes with WTF_MAKE_FAST_ALLOCATED in https://trac.webkit.org/changeset/248762. So, now I think 99% of allocations in WebKit-itself are handled well by FastMalloc.

-Yusuke
_______________________________________________
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev