RFR: 8314571: GrowableArray should move its old data and not copy it

Axel Boldt-Christmas aboldtch at openjdk.org
Mon Aug 21 06:43:25 UTC 2023


On Fri, 18 Aug 2023 10:39:28 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:

> Given some `GrowableArray<E>` where `E` is non-copyable with a move constructor will currently  fail to compile. This is because `GrowableArray`'s expand and shrink calls the copy constructor. We cast the values to rvalues (akin to `std::move`) to instead call the move constructor if available. If there is no move constructor but there is a copy constructor, then that will be called instead.

I can just add that I was investigating adding some sort of UniquePtr like object that respected our different `[Any|Resource|CHeap]Obj` allocation strategies. That required me to add move semantics to growablearray.
Here is my patch https://github.com/openjdk/jdk/commit/20cb959e79a67f13d1cc9c7ec07f89bc86eefd3a
My implementation prefered copy where a copy constructor existed. However I also noted that the growable array iterators always make a copy, which means that for (auto& el : growableArray) will always make a copy. So this code also changed it so that the iterators return references, and a GrowableArrayConstIterator was added to make it work in const contexts.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15344#issuecomment-1685740120


More information about the hotspot-dev mailing list