Integrated: 8331193: Return references when possible in GrowableArray

Johan Sjölen jsjolen at openjdk.org
Tue Jun 4 10:57:10 UTC 2024


On Fri, 26 Apr 2024 11:58:43 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.

This pull request has now been integrated.

Changeset: 0f4154a9
Author:    Johan Sjölen <jsjolen at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/0f4154a9e9805534595feccc53a4a1abf20f99ae
Stats:     46 lines in 2 files changed: 41 ins; 0 del; 5 mod

8331193: Return references when possible in GrowableArray

Reviewed-by: stefank, kbarrett, epeter

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

PR: https://git.openjdk.org/jdk/pull/18975


More information about the hotspot-dev mailing list