RFR: 8331193: Return references when possible in GrowableArray [v7]
    Johan Sjölen 
    jsjolen at openjdk.org
       
    Wed May 22 15:34:17 UTC 2024
    
    
  
> 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:
  Add test
-------------
Changes:
  - all: https://git.openjdk.org/jdk/pull/18975/files
  - new: https://git.openjdk.org/jdk/pull/18975/files/7a575e5a..f71e2ce2
Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=18975&range=06
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=18975&range=05-06
  Stats: 14 lines in 1 file changed: 14 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/18975.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/18975/head:pull/18975
PR: https://git.openjdk.org/jdk/pull/18975
    
    
More information about the hotspot-dev
mailing list