toArray with pre sized array

≡放荡痞女 提交于 2021-02-18 10:49:08

问题


when using ar.toArray(new String[ar.size()]) Android studio 3.2.1 warns about pre-sized array and recommends empty array:

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).

is it true for Android or just for java?

using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

because i think Android is non-HotSpot its virtual machine was Dalvik and now it is ART


回答1:


Great question.

https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_new_reflective_array

Bottom line: toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap for toArray(new T[size]), rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements in toArray APIs would follow the same logic as toArray(new T[0]) — the collection itself should create the appropriate storage.




回答2:


it reads since late updates of OpenJDK 6 and it does not matter which run-time is being used to run it - because the language-level of the code, which runs as compiled classes on Dalvik, might be Java 6, 7, 8. it only matters which language-level the project used to compile it. for example:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

setting JavaVersion.VERSION_1_6 might possibly even disable the inspection complaint... fixing performance issues on these dated devices is probably not worth the effort - and some/most might not even be affected, because only the "earlier updates" behave different than all what followed up.




回答3:


I am not a Java historian, but...

HotSpot appears to be essentially a brand name for a particular kind of JVM maintained and distributed by Oracle. It takes its name from the just-in-time compiler that can detect "hot spots" of frequently-executed code and optimize them on the fly.

The Android Runtime also has this JIT compiler behavior, as well as ahead-of-time compilation of java bytecode into native machine code at installation time.

This makes me think that ART falls into the same category as HotSpot (as far as this inspection is concerned), and therefore that you should use the "empty array" version of this call.


When in doubt, measure!

The best way to know for sure is to write a test program that executes both versions of the method and measures which one runs faster.


Sources:

  • https://en.wikipedia.org/wiki/HotSpot
  • https://en.wikipedia.org/wiki/Android_Runtime


来源:https://stackoverflow.com/questions/53284214/toarray-with-pre-sized-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!