Skip to content

8355013: GrowableArray default constructor should not allocate #24748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

merykitty
Copy link
Member

@merykitty merykitty commented Apr 18, 2025

Hi,

This patch changes the default constructors of GrowableArray so that it does not allocate. This is helpful because sometimes we create a GrowableArray and append another into it immediately, or create a GrowableArray to merge the value from several branches. In these cases, the default allocation is not needed. This also aligns the behaviour with that of std::vector, which does not allocate for default construction.

Please take a look and leave your reviews, thanks a lot.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8355013: GrowableArray default constructor should not allocate (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24748/head:pull/24748
$ git checkout pull/24748

Update a local copy of the PR:
$ git checkout pull/24748
$ git pull https://git.openjdk.org/jdk.git pull/24748/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24748

View PR using the GUI difftool:
$ git pr show -t 24748

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24748.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 18, 2025

👋 Welcome back qamai! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 18, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@merykitty merykitty force-pushed the vectordefaultconstructor branch from 3c27eec to af6f640 Compare April 18, 2025 05:36
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 18, 2025
@openjdk
Copy link

openjdk bot commented Apr 18, 2025

@merykitty The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk
Copy link

openjdk bot commented Apr 18, 2025

@merykitty Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information.

@mlbridge
Copy link

mlbridge bot commented Apr 18, 2025

Webrevs

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

@@ -435,6 +441,10 @@ class GrowableArrayTest : public ::testing::Test {
}
};

// static empty vector
const GrowableArray<int> empty_array(mtTest);
const GrowableArrayCHeap<int, mtTest> empty_cheap_array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reviews, these are additional to ensure that GrowableArray instances can be created in static context. I have added a test to check these are truly empty.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HotSpot denigrates non-local objects with non-trivial ctors. We shouldn't have such instances.
https://github.com/openjdk/jdk/blame/4dd64b49716144cc697fb461ff88860e2cbcaaea/doc/hotspot-style.md#L1200-L1205
See also #24689

Copy link
Member

@stefank stefank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that none of the current usages of GrowableArray() relies on the initial capacity to be 2?

I've added a couple of suggestions for layout changes so that we can keep the structure of the constructors consistent. I think it makes it easier to see what this particular constructor is doing and how it differs from the other.

Also, previously, the constructors were order so that the CHeap version directly followed the Resource version:

GrowableArray() // Resource allocated

GrowableArray(int) // Resource allocated
GrowableArray(int, MemTag) // CHeap allocated

GrowableArray(int, int, const E&) // Resource allocated
GrowableArray(int, int, const E&, MemTag) // CHeap allocated

It would be nice to retain that structure. That is, just move the new GrowableArray(MemTag) up one step. Or, alternatively, completely separate the two groups of constructors into to sections.

Comment on lines +769 to +774
explicit GrowableArray(MemTag mem_tag) :
GrowableArrayWithAllocator<E, GrowableArray>(nullptr, 0),
_metadata(mem_tag) {
init_checks();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
explicit GrowableArray(MemTag mem_tag) :
GrowableArrayWithAllocator<E, GrowableArray>(nullptr, 0),
_metadata(mem_tag) {
init_checks();
}
explicit GrowableArray(MemTag mem_tag) :
GrowableArrayWithAllocator<E, GrowableArray>(
nullptr,
0),
_metadata(mem_tag) {
init_checks();
}

Comment on lines +757 to +759
GrowableArray() : GrowableArrayWithAllocator<E, GrowableArray>(nullptr, 0), _metadata() {
init_checks();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GrowableArray() : GrowableArrayWithAllocator<E, GrowableArray>(nullptr, 0), _metadata() {
init_checks();
}
GrowableArray() :
GrowableArrayWithAllocator<E, GrowableArray>(
nullptr,
0),
_metadata() {
init_checks();
}

@@ -825,7 +833,9 @@ class GrowableArrayCHeap : public GrowableArrayWithAllocator<E, GrowableArrayCHe
}

public:
GrowableArrayCHeap(int initial_capacity = 0) :
GrowableArrayCHeap() : GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, MT>>(nullptr, 0) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GrowableArrayCHeap() : GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, MT>>(nullptr, 0) {}
GrowableArrayCHeap() :
GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, MT>>(
nullptr,
0) {}

@@ -825,7 +833,9 @@ class GrowableArrayCHeap : public GrowableArrayWithAllocator<E, GrowableArrayCHe
}

public:
GrowableArrayCHeap(int initial_capacity = 0) :
GrowableArrayCHeap() : GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, MT>>(nullptr, 0) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't necessary since the allocator doesn't allocate if you pass in 0. But maybe you want this change for clarity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

4 participants