RFR: 8314571: GrowableArray should move its old data and not copy it
Johan Sjölen
jsjolen at openjdk.org
Sat Aug 19 23:45:27 UTC 2023
On Sat, 19 Aug 2023 10:04:36 GMT, Quan Anh Mai <qamai 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.
>
> src/hotspot/share/utilities/growableArray.hpp line 545:
>
>> 543: if (len > 0) {
>> 544: new_data = static_cast<Derived*>(this)->allocate();
>> 545: for (int i = 0; i < len; ++i) ::new (&new_data[i]) E(static_cast<E&&>(old_data[i]));
>
> Why not use `std::move`? Thanks.
Hi, in short: Yes, I believe that I can switch and that `std::move` is better.
In long: I can't find any mention of `std::move` not being allowed in the style guide, using it seems to be in the same spirit as other std library usage (`#include <new>` for example) and since it is just a better version of what I wrote here let's do the switch.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15344#discussion_r1299279909
More information about the hotspot-dev
mailing list