Skip to content

[7.x] Optimize allocations for List.copyOf and Set.copyOf #79395

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

Merged
merged 2 commits into from
Oct 19, 2021

Conversation

arteam
Copy link
Contributor

@arteam arteam commented Oct 18, 2021

We can remove the array allocation which required only to call List.of/Set.of and instead create copies directly
based the supplied collection.

We can optimize away the array allocation just
to call `List.of/Set.of` and instead create copies directly
based the supplied collection.
@arteam arteam added >non-issue :Core/Infra/Core Core issues without another label v7.16.0 labels Oct 18, 2021
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Oct 18, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

public static <T> java.util.Set<T> copyOf(Collection<? extends T> coll) {
return (java.util.Set<T>) Set.of(new HashSet<>(coll).toArray());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one looks especially bad, because we allocate a HashSet just to call toArray which exists for Collection.

@arteam arteam requested a review from danhermann October 18, 2021 20:39
Copy link
Contributor

@danhermann danhermann left a comment

Choose a reason for hiding this comment

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

LGTM

@arteam
Copy link
Contributor Author

arteam commented Oct 19, 2021

Thanks, Dan!

@arteam arteam merged commit afcefdd into elastic:7.x Oct 19, 2021
@arteam arteam deleted the optimize-copy-of branch October 19, 2021 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v7.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants