RFR: 8331193: Return references when possible in GrowableArray [v3]
Kim Barrett
kbarrett at openjdk.org
Mon Apr 29 23:01:05 UTC 2024
On Fri, 26 Apr 2024 14:46:08 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:
>> Hi,
>>
>> This PR introduces the possibility of using references more often when using GrowableArray, where as previously this was only possible when using the `at()` method. This lets us avoid copying and redundant method calls and makes the API more streamlined. After the patch, we can use `at_grow` just like `at` works. The same goes for `top`, `first`, and `last`.
>>
>>
>> Some example code:
>> ```c++
>> // Before this patch this worked:
>> GrowableArray<int> arr(8,8,-1); // Pre-fill with 8 -1s
>> int& x = arr.at(7);
>> if (x == -1) {
>> x = 2;
>> }
>> assert(arr.at(7) == 2, "this holds");
>> // but this was forbidden
>> int& x = arr.at_grow(9, -1); // Compilation error! at_grow returns E, not E&
>> // so we had to do
>> int x = arr.at_grow(9, -1);
>> if (x == -1) {
>> arr.at_put(9, 2);
>> }
>>
>>
>> Thanks.
>
> Johan Sjölen has updated the pull request incrementally with one additional commit since the last revision:
>
> Small mistakes - FIXED!
src/hotspot/share/utilities/growableArray.hpp line 153:
> 151: E* adr_at(int i) const {
> 152: assert(0 <= i && i < _len, "illegal index %d for length %d", i, _len);
> 153: return &_data[i];
(GitHub won't let me put comment on the `adr_at` signature.)
I think there should similarly be const and non-const adr_at, returning pointer to const and non-const respectively.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/18975#discussion_r1583880213
More information about the hotspot-dev
mailing list