Skip to content
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

Avoid resizing of Maps created by CollectionUtils #29190

Closed

Conversation

dreis2211
Copy link
Contributor

@dreis2211 dreis2211 commented Sep 22, 2022

Hi,

I noticed that smaller maps with a size of 1 and 2 will be resized if you put the respective number of elements in it. This PR aligns the initial capacity calculation with the one from the JDK and thus fixes the contract:

	 * Instantiate a new {@link HashMap} with an initial capacity
	 * that can accommodate the specified number of elements without
	 * any immediate resize/rehash operations to be expected.
	 * 

I didn't add a test because it would access internals of (Linked)HashMap which felt pretty ugly and would probably need the internals to be opened. But let me know if I should try adding some.

I should note that this probably makes other cases a tiny bit slower, because the size is sometimes 1 higher than before. But then again. The contract is specific about the resizing not happening.

(A backport to 5.3 is as usual highly appreciated ;-) )

Cheers,
Christoph

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 22, 2022
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 26, 2022
@snicoll snicoll self-assigned this Sep 26, 2022
@snicoll snicoll added this to the 5.3.24 milestone Sep 26, 2022
snicoll pushed a commit that referenced this pull request Sep 26, 2022
@snicoll snicoll closed this in 433a23a Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants