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

refactor: fix code inspection - collection to array #2343

Merged
merged 2 commits into from
Aug 6, 2018
Merged

refactor: fix code inspection - collection to array #2343

merged 2 commits into from
Aug 6, 2018

Conversation

zielint0
Copy link
Contributor

@zielint0 zielint0 commented Aug 2, 2018

Use modern way to convert collection to array.

old: (mSourceClasspath.toArray(new String[mSourceClasspath.size()]));
new: (mSourceClasspath.toArray(new String[0]));

benefits:

  • more readable
  • faster to understand
  • a bit faster

I am ready to merge.

@zielint0 zielint0 changed the title refactor: fix code inspection: collection to array refactor: fix code inspection - collection to array Aug 3, 2018
@surli
Copy link
Collaborator

surli commented Aug 6, 2018

Here again I'm ok to merge but I want some more sources: I'm not entirely sure it's more readable as you loose the concept of size of the array. A beginner might think the array will be empty at the end. But ok let's say now everyone knows that the array won't be empty.

What about the performance?
I'd think that as the method doesn't have to compute the size of the array it would be faster when passing an array with the proper size: so why would it be faster this way?

@zielint0
Copy link
Contributor Author

zielint0 commented Aug 6, 2018

Performance | 'Collection.toArray()' call style

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

@surli
Copy link
Collaborator

surli commented Aug 6, 2018

ok thanks

@surli surli merged commit 0baea22 into INRIA:master Aug 6, 2018
@monperrus monperrus mentioned this pull request Sep 20, 2018
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants