toArray with pre sized array












3















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










share|improve this question





























    3















    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










    share|improve this question



























      3












      3








      3


      2






      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










      share|improve this question
















      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







      java android arrays virtual-machine dalvik






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 15:45







      BAHMAN

















      asked Nov 13 '18 at 15:23









      BAHMANBAHMAN

      1,2062616




      1,2062616
























          3 Answers
          3






          active

          oldest

          votes


















          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.







          share|improve this answer

































            0














            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






            share|improve this answer































              0














              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.






              share|improve this answer

























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284214%2ftoarray-with-pre-sized-array%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                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.







                share|improve this answer






























                  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.







                  share|improve this answer




























                    1












                    1








                    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.







                    share|improve this answer















                    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.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 16 '18 at 20:57









                    leonardkraemer

                    3,34911532




                    3,34911532










                    answered Nov 13 '18 at 16:29









                    WaxhawWaxhaw

                    274




                    274

























                        0














                        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






                        share|improve this answer




























                          0














                          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






                          share|improve this answer


























                            0












                            0








                            0







                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 15:42









                            Ben P.Ben P.

                            23.3k31948




                            23.3k31948























                                0














                                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.






                                share|improve this answer






























                                  0














                                  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.






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    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.






                                    share|improve this answer















                                    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.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 16 '18 at 20:54

























                                    answered Dec 16 '18 at 20:48









                                    Martin ZeitlerMartin Zeitler

                                    15.1k33963




                                    15.1k33963






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284214%2ftoarray-with-pre-sized-array%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Bressuire

                                        Vorschmack

                                        Quarantine