{"id":295505,"date":"2019-12-17T16:17:51","date_gmt":"2019-12-17T16:17:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295505"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295505","title":{"rendered":"\u041d\u0435 \u0432\u0441\u0435 \u043f\u0430\u0442\u0447\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/433174\/\">\n<p>\u042d\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0440\u0430\u0437\u0431\u043e\u0440 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0442\u0430\u0442\u044c \u044f\u0432\u044c\u044e, \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 &#171;\u043d\u043e&#187;. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043e <code>StringBuilder<\/code>-\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f <a href=\"https:\/\/habr.com\/ru\/post\/436746\/\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e &#171;\u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439&#187;, \u043e\u0442\u043a\u043b\u043e\u043d\u0451\u043d\u043d\u044b\u0445 \u0438\u0437-\u0437\u0430 \u043d\u0435\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0435\u0439 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u044f\u0437\u044b\u043a\u0430, \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u0443\u0433\u043b\u043e\u0432\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u0438\u0447\u0438\u043d. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h4 id=\"kogda-nichto-ne-predveschaet-bedy\">\u041a\u043e\u0433\u0434\u0430 \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043f\u0440\u0435\u0434\u0432\u0435\u0449\u0430\u0435\u0442 \u0431\u0435\u0434\u044b<\/h4>\n<p>  <\/p>\n<p>\u0414\u0443\u043c\u0430\u044e, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0430\u0441 \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0441 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 <code>Collections.emptySet()<\/code> \/ <code>Collections.emptyList()<\/code>. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0443\u044e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0431\u0435\u0437 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0432 \u0432\u043d\u0443\u0442\u0440\u044c \u043a\u043b\u0430\u0441\u0441\u0430 <code>EmptyList<\/code> \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0442\u0430\u043c \u0432\u043e\u0442 \u044d\u0442\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">private static class EmptyList&lt;E&gt; {    public Iterator&lt;E&gt; iterator() {     return emptyIterator();   }    public Object[] toArray() {     return new Object[0];   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0438\u0434\u0438\u0442\u0435 \u043c\u043e\u0449\u043d\u044b\u0439 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f? \u041c\u0435\u0442\u043e\u0434 <code>EmptyList.iterator()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440 \u0438\u0437 \u043d\u0430\u043b\u0438\u0447\u0438\u044f, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>toArray()<\/code>?<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0415\u0441\u0442\u044c \u043e\u0434\u043d\u043e \u043d\u043e, \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/b><\/p>\n<div class=\"spoiler_text\">\n<blockquote><p>The returned array will be &#171;safe&#187; in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.<\/p><\/blockquote>\n<p><a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2017-September\/049170.html\" rel=\"nofollow\">http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2017-September\/049170.html<\/a><br \/>  <a href=\"http:\/\/hg.openjdk.java.net\/jdk\/jdk12\/file\/ffac5eabbf28\/src\/java.base\/share\/classes\/java\/util\/List.java#l185\" rel=\"nofollow\">http:\/\/hg.openjdk.java.net\/jdk\/jdk12\/file\/ffac5eabbf28\/src\/java.base\/share\/classes\/java\/util\/List.java#l185<\/a><\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u0435\u0442\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <strong>\u043d\u043e\u0432\u044b\u0439<\/strong> \u043c\u0430\u0441\u0441\u0438\u0432.<\/p>\n<p>  <\/p>\n<p>\u0412\u044b \u0441\u043a\u0430\u0436\u0435\u0442\u0435: &#171;\u041e\u043d \u0436\u0435 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0439! \u0427\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0439\u0442\u0438 \u043d\u0435 \u0442\u0430\u043a!?&#187;<\/p>\n<p>  <\/p>\n<p>\u041e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043c\u043e\u0433\u0443\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0437\u043d\u0430\u0442\u043e\u043a\u0438:<\/p>\n<p>  <\/p>\n<blockquote><p> \u2014 \u041a\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442?<br \/>   \u2014 \u041e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u043d\u0430\u0442\u043e\u043a\u0438 \u041f\u043e\u043b \u0421\u0430\u043d\u0434\u043e\u0437 \u0438 \u0422\u0430\u0433\u0438\u0440 \u0412\u0430\u043b\u0435\u0435\u0432<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0442\u0432\u0435\u0442 \u0437\u043d\u0430\u0442\u043e\u043a\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2017-September\/049171.html\" rel=\"nofollow\">http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2017-September\/049171.html<\/a><\/p>\n<p>  <\/p>\n<blockquote><p>Also note that this changes visible behavior. E. g. somebody might synchronize on the array object returned by toArray call, so this change might cause unwanted <strong>lock sharing<\/strong>.<\/p>\n<p>  Once I suggested similar improvement: to return EMPTY_LIST from Arrays.asList() when supplied array has zero length. It was declined with the same reason [1].<\/p>\n<p>  <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2015-September\/035197.html\" rel=\"nofollow\">http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2015-September\/035197.html<\/a><\/p>\n<p>  By the way, probably it&#8217;s reasonable then for Arrays.asList to check the array length like:  <\/p>\n<pre><code class=\"java\">public static &lt;T&gt; List&lt;T&gt; asList(T... a) { if(a.length == 0) return Collections.emptyList(); return new ArrayList&lt;&gt;(a); }<\/code><\/pre>\n<p>  \u0417\u0432\u0443\u0447\u0438\u0442 \u0440\u0430\u0437\u0443\u043c\u043d\u043e, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438? \u0417\u0430\u0447\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0435\u0441\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0439?<br \/>  There is a reason not to do this. At the moment Arrays.asList specifies no constraints on the identity of the returned List. Adding the micro-optimisation will change that. It\u2019s an edge case and a questionable use-case too, but considering that i would conservatively leave things as they are.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u044d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0443 \u0432\u0430\u0441 \u043d\u0435\u0434\u043e\u0443\u043c\u0435\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<blockquote><p>E. g. somebody might synchronize on the array object returned by toArray call, so this change might cause unwanted lock sharing.<\/p><\/blockquote>\n<p>\u0412\u044b \u0441\u043a\u0430\u0436\u0435\u0442\u0435: &#171;\u041a\u0442\u043e \u0432 \u0437\u0434\u0440\u0430\u0432\u043e\u043c \u0443\u043c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 (!) \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u043c (!!!) \u0438\u0437 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438!?&#187;<\/p>\n<p>  <\/p>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u043e, \u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044f\u0437\u044b\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0435\u043a\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e (\u0430 \u0442\u043e \u0438 \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b). \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0430 \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u2014 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a \u043f\u043e\u043b\u043e\u043c\u043a\u0435 \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 (\u043f\u043e\u0434\u0438 \u043f\u043e\u0442\u043e\u043c, \u043e\u0442\u043b\u043e\u0432\u0438 \u0435\u0451). \u0420\u0438\u0441\u043a \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d, \u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0439 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0441\u0442\u043e\u043b\u044c \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u0435\u043d, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0451 \u043a\u0430\u043a \u0435\u0441\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u043b\u044e\u0431\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435, \u043a\u043c\u043a, \u0431\u044b\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u044f\u0437\u044b\u043a\u0430. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0430\u044f \u0437\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e, \u0430 \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0432\u044b\u0448\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e, \u0442. \u043a. \u043d\u0430 \u043d\u0451\u043c \u043c\u043e\u0433\u0443\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0442\u0430\u043a\u043e\u0432\u0430: \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u2014 \u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u043e\u0432\u044b \u043e\u0441\u0442\u0440\u043e\u0432\u0430 \u042f\u0432\u0430. \u0414\u0430\u0436\u0435 \u043d\u0435 \u043f\u044b\u0442\u0430\u0439\u0442\u0435\u0441\u044c \u043f\u043e\u043a\u0443\u0448\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0438\u0445: \u0441\u0442\u0440\u0430\u0436\u0430 \u0441\u0442\u0440\u0435\u043b\u044f\u0435\u0442 \u0431\u0435\u0437 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<h4 id=\"staraeshsya-staraeshsya\">\u0421\u0442\u0430\u0440\u0430\u0435\u0448\u044c\u0441\u044f-\u0441\u0442\u0430\u0440\u0430\u0435\u0448\u044c\u0441\u044f&#8230;<\/h4>\n<p>  <\/p>\n<p>\u0412 JDK \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0438 \u0432\u0441\u0435 \u043e\u043d\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b <code>List.indexOf()<\/code> \u0438 <code>List.lastIndexOf()<\/code>:<\/p>\n<p>  <\/p>\n<ul>\n<li>java.util.ArrayList<\/li>\n<li>java.util.Arrays$ArrayList<\/li>\n<li>java.util.Vector<\/li>\n<li>java.util.concurrent.CopyOnWriteArrayList<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041a\u043e\u0434 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u044d\u0442\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u043e\u0434\u0438\u043d \u043a \u043e\u0434\u043d\u043e\u043c\u0443. \u0421\u0432\u043e\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0436\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0442\u0430\u043a\u0436\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/hibernate\/hibernate-orm\/blob\/0a2a5c622e3eb30724e80bc8661c0ac55ebfb2be\/hibernate-core\/src\/main\/java\/org\/hibernate\/bytecode\/enhance\/internal\/tracker\/SimpleFieldTracker.java#L40\" rel=\"nofollow\">org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/JetBrains\/intellij-community\/blob\/200f59d7cf3b0f66feb3a4abebbb90864dc5edc7\/platform\/util\/src\/com\/intellij\/util\/ArrayUtil.java#L726\" rel=\"nofollow\">com.intellij.util.ArrayUtil<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/JetBrains\/intellij-community\/blob\/200f59d7cf3b0f66feb3a4abebbb90864dc5edc7\/platform\/util-rt\/src\/com\/intellij\/util\/ArrayUtilRt.java#L63\" rel=\"nofollow\">com.intellij.util.ArrayUtilRt<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/blob\/5f4d1a4628513ab34098fa3f92ba03aa20fc4204\/spring-oxm\/src\/main\/java\/org\/springframework\/oxm\/jibx\/JibxMarshaller.java#L256\" rel=\"nofollow\">org.springframework.oxm.jibx.JibxMarshaller<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0438\u043c\u0435\u0435\u043c \u0431\u0440\u043e\u0434\u044f\u0447\u0438\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c (\u043f\u0440\u0438 \u0447\u0451\u043c \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043c\u0435\u0441\u0442\u043e \u0432 ReserverCodeCache, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u0447\u0443\u0435\u0442 \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441 \u043f\u043e\u0447\u0442\u0438 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043e\u0447\u0435\u043d\u044c \u043b\u044e\u0431\u044f\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">int i = Arrays.asList(array).indexOf(obj);  \/\/ \u0438\u043b\u0438 boolean contains = Arrays.asList(array).contains(obj);  \/\/ \u043f\u0440\u0438 \u0442\u044f\u0436\u0451\u043b\u043e\u043c \u0441\u0442\u0440\u0438\u043c\u043e\u0437\u0435 boolean contains = Arrays.stream(names).anyMatch(nm -&gt; nm.equals(name));<\/code><\/pre>\n<p>  <\/p>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0432\u0432\u0435\u0441\u0442\u0438 \u0432 JDK \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434\u044b, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0432\u0441\u044e\u0434\u0443, \u0447\u0442\u043e \u0438 \u0431\u044b\u043b\u043e <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2018-March\/051968.html\" rel=\"nofollow\">\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043e<\/a>. <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/attachments\/20180310\/f0ddb552\/indexOf.patch\" rel=\"nofollow\">\u0417\u0430\u043f\u043b\u0430\u0442\u043a\u0430<\/a> \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043a\u0430\u043a \u0434\u0432\u0435 \u043a\u043e\u043f\u0435\u0439\u043a\u0438:<\/p>\n<p>  <\/p>\n<p>1) \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>List.indexOf()<\/code> \u0438 <code>List.lastIndexOf()<\/code> \u043f\u0435\u0440\u0435\u0435\u0437\u0436\u0430\u044e\u0442 \u0432 <code>java.util.Arrays<\/code><br \/>  2) \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0438\u0445 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, <code>Arrays.indexOf()<\/code> \u0438 <code>Arrays.lastIndexOf()<\/code><\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0439\u0442\u0438 \u043d\u0435 \u0442\u0430\u043a? \u0412\u044b\u0438\u0433\u0440\u044b\u0448 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 [\u0432\u0440\u043e\u0434\u0435 \u0431\u044b] \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d. \u041d\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u043e \u043f\u0440\u043e\u0432\u0430\u043b\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0443\u043c\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0439\u0442\u0438 \u043d\u0435\u0442 \u0442\u0430\u043a.<\/p>\n<p>  <\/p>\n<blockquote><p> \u2014 \u041a\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442?<br \/>   \u2014 \u041e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u043d\u0430\u0442\u043e\u043a\u0438 \u041c\u0430\u0440\u0442\u0438\u043d \u0411\u0443\u0445\u0433\u043e\u043b\u044c\u0446 \u0438 \u041f\u043e\u043b \u0421\u0430\u043d\u0434\u043e\u0437<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418\u043c\u0445\u043e, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0442\u044f\u043d\u0443\u0442\u043e, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u041c\u0430\u0440\u0442\u0438\u043d \u0411\u0443\u0445\u0433\u043e\u043b\u044c\u0446:<\/p>\n<p>  <\/p>\n<blockquote><p>\u0421\u0435\u0440\u0433\u0435\u0439, I&#8217;m sort of maintaining all those collection classes, and I have on occasion also wanted to have indexOf methods in Array.java. But:<\/p>\n<p>  Arrays are generally discouraged. Any new static methods on Arrays (or, where I actually want them, on the array object itself! Requires a java language change!) will meet resistance.<\/p>\n<p>  We&#8217;ve come to regret supporting nulls in collections, so newer collection classes like ArrayDeque don&#8217;t support them.<\/p>\n<p>  Another variant users might want is what kind of equality comparison to use.<\/p>\n<p>  We&#8217;ve come to regret having ArrayList with zero-based start index \u2014 it would have been better to have ArrayDeque&#8217;s circular array behavior in from day one.<\/p>\n<p>  The code to search array slices is very small, so you&#8217;re not saving much. It&#8217;s easy to make an off-by-one error, but that&#8217;s true for an Arrays API as well.<\/p><\/blockquote>\n<p>\u041f\u043e\u043b \u0421\u0430\u043d\u0434\u043e\u0437:<\/p>\n<p>  <\/p>\n<blockquote><p>I would not go so far as to say arrays are discouraged, i would positively spin it as &#171;use with care\u201d as they are prickly e.g. always mutable. They could certainly be improved. I would be very happy to see arrays implement a common array\u2019ish interface, we might be able to make some progress after value types sediment.<\/p>\n<p>  Any new additions to Arrays would be met with some resistance though, by me at least \ud83d\ude42 It\u2019s never adding just one or two methods, many others want come along for the ride as well (all primitives plus range variants). So any new feature needs to be sufficiently beneficial and in this case i don\u2019t think the benefits are strong enough (such as a possible reduction code cache pressure).<\/p><\/blockquote>\n<p>Paul.<\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u043f\u0438\u0441\u043a\u0430: <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2018-March\/051968.html\" rel=\"nofollow\">http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2018-March\/051968.html<\/a><\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0442\u0430\u043a\u043e\u0432\u0430: \u0432\u0430\u0448 \u0433\u0435\u043d\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0442\u0447 \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0440\u0435\u0437\u0430\u0442\u044c \u043d\u0430 \u0440\u0435\u0432\u044c\u044e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043d\u0435 \u0443\u0432\u0438\u0434\u044f\u0442 \u0432 \u043d\u0451\u043c \u043e\u0441\u043e\u0431\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438. \u041d\u0443 \u0434\u0430, \u0435\u0441\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u043a\u043e\u0434, \u043d\u043e \u043e\u043d \u043d\u0438\u043a\u043e\u043c\u0443 \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0443\u0441\u0442\u044c \u0436\u0438\u0432\u0451\u0442.<\/p>\n<p>  <\/p>\n<h4 id=\"uluchsheniya-dlya-arraylist-a-ih-est-u-menya\">\u0423\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f ArrayList-a? \u0418\u0445 \u0435\u0441\u0442\u044c \u0443 \u043c\u0435\u043d\u044f<\/h4>\n<p>  <\/p>\n<p><del>\u041c\u043e\u043f\u0435\u0434<\/del> \u043f\u0430\u0442\u0447 \u043d\u0435 \u043c\u043e\u0439, \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0443 \u0434\u043b\u044f \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c. \u0421\u0430\u043c\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043e\u0437\u0432\u0443\u0447\u0435\u043d\u043e <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2018-December\/057546.html\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>, \u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0441\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<p><a href=\"http:\/\/cr.openjdk.java.net\/~sgroeger\/perf\/arraylist\/webrev.00\/src\/java.base\/share\/classes\/java\/util\/ArrayList.java.sdiff.html\" rel=\"nofollow\">\u0421\u0441\u044b\u043b\u043a\u0430<\/a><\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041d\u0435\u0432\u043e\u043e\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u043c \u0433\u043b\u0430\u0437\u043e\u043c \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043e\u0447\u0435\u043d\u044c \u0438 \u043e\u0447\u0435\u043d\u044c \u043b\u043e\u0433\u0438\u0447\u043d\u043e. \u0418\u0437\u043c\u0435\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e <a href=\"http:\/\/cr.openjdk.java.net\/~sgroeger\/perf\/arraylist\/ArrayListBenchmark.java\" rel=\"nofollow\">\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@State(Scope.Benchmark) public class ArrayListBenchmark {    @State(Scope.Benchmark)   public static class Data {      @Param({\"10\", \"100\", \"1000\", \"10000\"})     public int size;      ArrayList&lt;Integer&gt; arrayRandom = new ArrayList&lt;Integer&gt;(size);      @Setup(Level.Invocation)     public void initArrayList() {       Random rand = new Random();       rand.setSeed(System.currentTimeMillis());       \/\/ Populate the ArrayList with size-5 elements        for (int i = 0; i &lt; size - 5; i++) {         Integer r = rand.nextInt() % 256;         arrayRandom.add(r);       }     }   }    @Benchmark   public ArrayList construct_new_array_list(Data d) {     ArrayList al = new ArrayList(d.arrayRandom);     \/\/ once a new ArrayList is created add a new element     al.add(new Integer(900));     return al;   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u0442\u043e\u0433\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Benchmark                 (size)   Mode  Cnt    Score    Error  Units  \u0414\u043e  construct_new_array_list      10  thrpt   25  388.212 \u00b1 23.110  ops\/s construct_new_array_list     100  thrpt   25   90.208 \u00b1  7.995  ops\/s construct_new_array_list    1000  thrpt   25   23.289 \u00b1  1.687  ops\/s construct_new_array_list   10000  thrpt   25    7.659 \u00b1  0.560  ops\/s  \u041f\u043e\u0441\u043b\u0435  construct_new_array_list      10  thrpt   25  562.678 \u00b1 37.370  ops\/s construct_new_array_list     100  thrpt   25  119.791 \u00b1 13.232  ops\/s construct_new_array_list    1000  thrpt   25   33.811 \u00b1  3.812  ops\/s construct_new_array_list   10000  thrpt   25   10.889 \u00b1  0.564  ops\/s<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u043a\u0430\u043a \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u0413\u043b\u0430\u0432\u043d\u043e\u0435, \u0447\u0442\u043e \u043f\u043e\u0434\u0432\u043e\u0445\u043e\u0432-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u043d\u0435\u0442. \u0427\u0435\u0441\u0442\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043c\u0430\u0441\u0441\u0438\u0432, \u0447\u0435\u0441\u0442\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u0440\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c. \u0412\u043e\u0442 \u0441\u0435\u0439\u0447\u0430\u0441 \u043f\u0430\u0442\u0447 \u0442\u043e\u0447\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043d\u044f\u0442\u044c!<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u043e \u043d\u0435 \u0442\u0443\u0442-\u0442\u043e \u0431\u044b\u043b\u043e<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u041c\u0430\u0440\u0442\u0438\u043d \u0411\u0443\u0445\u0433\u043e\u043b\u044c\u0446:<\/p>\n<p>  <\/p>\n<blockquote><p>This is no question that we can optimize the case of ArrayList -&gt; ArrayList, but what about all the other Collection implementations? ArrayDeque and CopyOnWriteArrayList come to mind.<br \/>  ArrayList is a popular class to use for making copies of Collections. Where do you stop?<\/p>\n<p>  A pathological subclass of ArrayList could decide to not store elements in the backing array, with ensuing breakage.<\/p>\n<p>  The blessed solution to the list copy problem is probably List.copyOf <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/List.html#copyOf(java.util.Collection\" rel=\"nofollow\">https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/List.html#copyOf(java.util.Collection<\/a>) which might do the optimization you&#8217;re hoping for.<\/p><\/blockquote>\n<p>\u0410\u043b\u0430\u043d \u0411\u0435\u0439\u0442\u043c\u0430\u043d<\/p>\n<p>  <\/p>\n<blockquote><p>ArrayList is not final so it&#8217;s possible someone has extended it to use something other than elementData. It might be safer to use the class identity rather than instanceof.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041d\u0438\u0447\u0442\u043e \u043d\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u043c\u043d\u0435 \u043e\u0442\u043f\u043e\u0447\u043a\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>ArrayList<\/code>-\u0430, \u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435. \u0422\u043e\u0433\u0434\u0430 <code>c instanceof ArrayList<\/code> \u0432\u0435\u0440\u043d\u0451\u0442 \u0438\u0441\u0442\u0438\u043d\u0443, \u043c\u044b \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u043d\u0430 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0431\u043b\u0430\u0433\u043e\u043f\u043e\u043b\u0443\u0447\u043d\u043e \u0441\u0432\u0430\u043b\u0438\u043c\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0442\u0430\u043a\u043e\u0432\u0430: \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439 \u043e\u0442\u043a\u0430\u0437\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c <em>\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435<\/em> \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0443\u043c\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u0436\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u0430\u0431\u0441\u0443\u0440\u0434\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u044f\u0437\u044b\u043a\u0430. \u0418 \u0434\u0430, \u0432\u0441\u0451 \u043c\u043e\u0433\u043b\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0435\u0441\u043b\u0438 \u0431\u044b <code>ArrayList<\/code> \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u044a\u044f\u0432\u0438\u043b\u0438 <code>final<\/code>.<\/p>\n<p>  <\/p>\n<h4 id=\"snova-specifikaciya\">\u0421\u043d\u043e\u0432\u0430 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h4>\n<p>  <\/p>\n<p>\u041e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u044f \u0441\u0432\u043e\u0451 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0440\u043e\u0432\u0430\u043b\u0438\u043b\u0441\u044f \u0432 \u043a\u0438\u0448\u043a\u0438 \u0421\u043f\u0440\u0438\u043d\u0433\u0430 \u0438 \u043d\u0430\u0448\u0435\u043b \u0442\u0430\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 <a href=\"https:\/\/github.com\/stsypanov\/spring-framework\/blob\/7574062e43a23c7e401b1879ca8c3657e8eb2275\/spring-beans\/src\/main\/java\/org\/springframework\/beans\/factory\/support\/ConstructorResolver.java#L204\" rel=\"nofollow\">\u043a\u043e\u0434<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 paramTypes  for (Constructor&lt;?&gt; candidate : candidates) {   Class&lt;?&gt;[] paramTypes = candidate.getParameterTypes();    if (constructorToUse != null &amp;&amp; argsToUse != null &amp;&amp; argsToUse.length &gt; paramTypes.length) {     \/\/ Already found greedy constructor that can be satisfied -&gt;     \/\/ do not look any further, there are only less greedy constructors left.     break;   }   if (paramTypes.length &lt; minNrOfArgs) {     continue;   }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e \u0441\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u043e\u043c\u0443 \u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044e \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432, \u0437\u0430\u0439\u0434\u044f \u0432\u043d\u0443\u0442\u0440\u044c <code>java.lang.reflect.Constructor.getParameterTypes()<\/code> \u044f \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u0438\u043b \u043a\u043e\u0434 \u0447\u0443\u0442\u044c \u043d\u0438\u0436\u0435 \u0438 \u043d\u0430\u0448\u0435\u043b \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Override public Class&lt;?&gt;[] getParameterTypes() {   return parameterTypes.clone(); }  \/**  * {@inheritDoc}  * @since 1.8  *\/ public int getParameterCount() { return parameterTypes.length; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435, \u0434\u0430? \u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430\/\u043c\u0435\u0442\u043e\u0434\u0430, \u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>java.lang.reflect.Method.getParameterCount()<\/code> \u0438 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u0438\u0433\u0440\u0430 \u0441\u0432\u0435\u0447 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443 \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class MethodToStringBenchmark {   private Method method;    @Setup   public void setup() throws Exception { method = getClass().getMethod(\"toString\"); }    @Benchmark   public int getParameterCount() { return method.getParameterCount(); }    @Benchmark   public int getParameterTypes() { return method.getParameterTypes().length; } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430 \u043c\u043e\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u0438 \u0441 JDK 11 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Benchmark                                       Mode  Cnt     Score    Error   Units  getParameterCount                               avgt   25     2,528 \u00b1  0,085   ns\/op getParameterCount:\u00b7gc.alloc.rate                avgt   25    \u2248 10\u207b\u2074           MB\/sec getParameterCount:\u00b7gc.alloc.rate.norm           avgt   25    \u2248 10\u207b\u2077             B\/op getParameterCount:\u00b7gc.count                     avgt   25       \u2248 0           counts  getParameterTypes                               avgt   25     7,299 \u00b1  0,410   ns\/op getParameterTypes:\u00b7gc.alloc.rate                avgt   25  1999,454 \u00b1 89,929  MB\/sec getParameterTypes:\u00b7gc.alloc.rate.norm           avgt   25    16,000 \u00b1  0,001    B\/op getParameterTypes:\u00b7gc.churn.G1_Eden_Space       avgt   25  2003,360 \u00b1 91,537  MB\/sec getParameterTypes:\u00b7gc.churn.G1_Eden_Space.norm  avgt   25    16,030 \u00b1  0,045    B\/op getParameterTypes:\u00b7gc.churn.G1_Old_Gen          avgt   25     0,004 \u00b1  0,001  MB\/sec getParameterTypes:\u00b7gc.churn.G1_Old_Gen.norm     avgt   25    \u2248 10\u207b\u2075             B\/op getParameterTypes:\u00b7gc.count                     avgt   25  2380,000           counts getParameterTypes:\u00b7gc.time                      avgt   25  1325,000               ms<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441 \u044d\u0442\u0438\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c? \u041c\u043e\u0436\u0435\u043c \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <code>Method.getParameterTypes().length<\/code> \u0432 JDK (\u043f\u043e \u043c\u0435\u043d\u044c\u0448\u0435\u0439 \u043c\u0435\u0440\u0435 \u0432 <code>java.base<\/code>) \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0442\u0430\u043c, \u0433\u0434\u0435 \u044d\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b:<\/p>\n<p>  <\/p>\n<p><code>java.lang.invoke.MethodHandleProxies<\/code><\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/l_\/3j\/h1\/l_3jh1hu3swdedxqktlxboz0tu0.png\"><\/p>\n<p>  <\/p>\n<p><code>java.util.concurrent.ForkJoinTask<\/code><\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/0o\/rt\/q7\/0ortq71mnf3-bwsa4wyi-jmzf5y.png\"><\/p>\n<p>  <\/p>\n<p><code>java.lang.reflect.Executable<\/code><\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sm\/_h\/pm\/sm_hpm8eahlmqavobjqg9gejvse.png\"><\/p>\n<p>  <\/p>\n<p><code>sun.reflect.annotation.AnnotationType<\/code><\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yw\/bv\/il\/ywbvil_doi3fzy6uzkpcm42g6zu.png\"><\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/attachments\/20191109\/a66db498\/getParamCount.patch\" rel=\"nofollow\">\u041f\u0430\u0442\u0447<\/a> \u0431\u044b\u043b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/core-libs-dev\/2019-November\/063344.html\" rel=\"nofollow\">\u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u0438\u0441\u044c\u043c\u043e\u043c<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0412\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u043a\u0430\u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 <a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8029019\" rel=\"nofollow\">\u043f\u043e\u0445\u043e\u0436\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430<\/a> \u0438 \u043f\u043e \u043d\u0435\u0439 \u0434\u0430\u0436\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u044b <a href=\"http:\/\/cr.openjdk.java.net\/~plevart\/jdk9-dev\/8029019_Annotations.optimization\/webrev.01\/\" rel=\"nofollow\">\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/a>. \u0412 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043e\u0442\u043c\u0435\u0447\u0435\u043d \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u043b\u0438\u0447\u043d\u044b\u0439, \u043a\u0430\u043a \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041e\u0434\u043d\u0430\u043a\u043e, \u0438 \u043e\u043d\u0438, \u0438 \u043c\u043e\u0439 \u043f\u0430\u0442\u0447 \u043f\u043e\u043a\u0430 \u0443\u0431\u0440\u0430\u043d\u044b \u043f\u043e\u0434 \u0441\u0443\u043a\u043d\u043e \u0438 \u043b\u0435\u0436\u0430\u0442 \u0431\u0435\u0437 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u041f\u043e\u0447\u0435\u043c\u0443? \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0437\u0430\u043d\u044f\u0442\u044b \u0431\u043e\u043b\u0435\u0435 \u043d\u0443\u0436\u043d\u044b\u043c\u0438 \u0432\u0435\u0449\u0430\u043c\u0438 \u0438 \u0443 \u043d\u0438\u0445 \u0442\u0443\u043f\u043e \u043d\u0435 \u0434\u043e\u0445\u043e\u0434\u044f\u0442 \u0440\u0443\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0442\u0430\u043a\u043e\u0432\u0430: \u0432\u0430\u0448\u0438 \u0433\u0435\u043d\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u0432\u0438\u0441\u043d\u0443\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437-\u0437\u0430 \u043d\u0435\u0445\u0432\u0430\u0442\u043a\u0438 \u0440\u0430\u0431\u043e\u0447\u0438\u0445 \u0440\u0443\u043a.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u043a\u043e\u043d\u0435\u0446! \u0412 \u0445\u043e\u0434\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043c\u0435\u043d\u044b \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0438 \u0432\u044b\u0434\u0432\u0438\u043d\u0443\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435: \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043c\u0435\u043d\u0443 <code>Method.getParameterTypes().length -&gt; Method.getParameterCount()<\/code> \u0440\u0443\u043a\u0430\u043c\u0438, \u0430 \u043f\u043e\u0440\u0443\u0447\u0438\u0442\u044c \u044d\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443? \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u044d\u0442\u043e \u0438 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u044d\u0442\u043e &#171;\u0437\u0430\u043a\u043e\u043d\u043d\u044b\u043c&#187;?<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0441\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Test void arrayClone() {   final Object[] objects = new Object[3];   objects[0] = \"azaza\";   objects[1] = 365;   objects[2] = 9876L;    final Object[] clone = objects.clone();    assertEquals(objects.length, clone.length);   assertSame(objects[0], clone[0]);   assertSame(objects[1], clone[1]);   assertSame(objects[2], clone[2]); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0442\u043e \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u0442. \u043a. \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0438\u0437 \u0435\u0433\u043e \u044f\u0447\u0435\u0435\u043a \u0438\u043b\u0438 \u043f\u043e\u043b\u044e <code>length<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0430.<\/p>\n<p>  <\/p>\n<p>\u0423\u043c\u0435\u0435\u0442 \u043b\u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e JDK? \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class ArrayAllocationEliminationBenchmark {      private int length = 10;      @Benchmark     public int baseline() {         return new int[length].length;     }      @Benchmark     public int baselineClone() {         return new int[length].clone().length;     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0434\u043b\u044f JDK 13:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Benchmark                                Mode  Cnt     Score     Error   Units  baseline                                 avgt   50     6,135 \u00b1   0,140   ns\/op baseline:\u00b7gc.alloc.rate.norm             avgt   50    56,000 \u00b1   0,001    B\/op  clone                                    avgt   50    18,359 \u00b1   0,619   ns\/op clone:\u00b7gc.alloc.rate.norm                avgt   50   112,000 \u00b1   0,001    B\/op<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0445\u043e\u0434\u0438\u0442, \u0447\u0442\u043e openjdk \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c <code>new int[length]<\/code>, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <a href=\"https:\/\/github.com\/oracle\/graal\/issues\/1847\" rel=\"nofollow\">\u0413\u0440\u0430\u0430\u043b\u044f<\/a>, \u0445\u0435-\u0445\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Benchmark                                Mode  Cnt     Score     Error   Units  baseline                                 avgt   25     2,470 \u00b1   0,156   ns\/op baseline:\u00b7gc.alloc.rate.norm             avgt   25     0,005 \u00b1   0,008    B\/op  \u0441lone                                    avgt   25    13,086 \u00b1   1,059   ns\/op \u0441lone:\u00b7gc.alloc.rate.norm                avgt   25   112,113 \u00b1   0,115    B\/op<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 openjdk, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043c\u043e\u0433 \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e \u0436\u0435, \u0447\u0442\u043e \u0443\u043c\u0435\u0435\u0442 \u0413\u0440\u0430\u0430\u043b\u044c. \u0422. \u043a. \u0432\u043b\u0435\u0437\u0442\u044c \u0432 \u043f\u043b\u044e\u0441\u043e\u0432\u044b\u0439 \u0430\u0434\u042a \u0432 \u043a\u043e\u0434\u0435 \u0412\u041c \u0438 \u043d\u0430\u043f\u0438\u043b\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u043c\u043e\u0433\u0443\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u0438\u0448\u044c \u0432\u0441\u0435, \u0442\u043e \u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043b\u0441\u044f <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2019-November\/036003.html\" rel=\"nofollow\">\u043f\u0438\u0441\u044c\u043c\u043e\u043c<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0438\u0437\u043b\u043e\u0436\u0438\u043b \u0441\u0432\u043e\u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0438 \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0435\u0439. \u0412\u043b\u0430\u0434\u0438\u043c\u0438\u0440 \u0418\u0432\u0430\u043d\u043e\u0432 <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2019-November\/036043.html\" rel=\"nofollow\">\u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442<\/a>, \u0447\u0442\u043e:<\/p>\n<p>  <\/p>\n<blockquote><p>Considering there&#8217;s no way to grow\/shrink Java arrays,<br \/>  &#171;cloned_array.length =&gt; original_array.length&#187; transformation is correct<br \/>  irrespective of whether cloned variant escapes or not.<\/p>\n<p>  Moreover, the transformation is already there:<\/p>\n<p>  <a href=\"http:\/\/hg.openjdk.java.net\/jdk\/jdk\/file\/tip\/src\/hotspot\/share\/opto\/memnode.cpp#l2388\" rel=\"nofollow\">http:\/\/hg.openjdk.java.net\/jdk\/jdk\/file\/tip\/src\/hotspot\/share\/opto\/memnode.cpp#l2388<\/a><\/p>\n<p>  I haven&#8217;t looked into the benchmarks you mentioned, but it looks like<br \/>  cloned_array.length access is not the reason why cloned array is still<br \/>  there.<\/p>\n<p>  Regarding your other ideas, redirecting accesses from cloned instance to<br \/>  original is problematic (in general case) since compiler has to prove<br \/>  there were no changes in both versions and indexed accesses make it even<br \/>  harder. And safepoints cause problems as well (for rematerialization).<\/p>\n<p>  But I agree that it would be nice to cover (at least) simple cases of<br \/>  defensive copying.<\/p><\/blockquote>\n<p>\u0422. \u0435. \u0433\u0440\u043e\u0445\u043d\u0443\u0442\u044c \u043a\u043b\u043e\u043d \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u0438 \u043c\u043e\u0436\u043d\u043e, \u0438 \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u0410 \u0432\u043e\u0442 \u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <\/p>\n<p>  <\/p>\n<pre><code class=\"java\">int len = new int[arrayLength].length;<\/code><\/pre>\n<p>  <\/p>\n<p>&#8212;&gt;<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">int len = arrayLength;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2019-November\/036046.html\" rel=\"nofollow\">\u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438<\/a>:<\/p>\n<p>  <\/p>\n<blockquote><p>We don&#8217;t eliminate array allocations that doesn&#8217;t have a known length<br \/>  because they might cause a NegativeArraySize Exception. In this case we<br \/>  should be able to prove that the length is positive though.<\/p>\n<p>  Anyway \u2014 I have an almost finished patch that replace unused array<br \/>  allocations with a proper guard.<\/p><\/blockquote>\n<p>\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0432\u0437\u044f\u0442\u044c \u0438 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442. \u043a \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e <a href=\"https:\/\/docs.oracle.com\/javase\/specs\/jls\/se13\/html\/jls-15.html#jls-15.10.2\" rel=\"nofollow\">\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/a> \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 <em>\u043e\u0431\u044f\u0437\u0430\u043d\u043e<\/em> \u0431\u0440\u043e\u0441\u0438\u0442\u044c <code>NegativeArraySizeException<\/code> \u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043c\u044b \u0441 \u044d\u0442\u0438\u043c \u043f\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Test void arrayWithNwgativeSize() {   int length = 0;   try {     int newLen = -3;     length = new Object[newLen].length; \/\/ \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c new Object[newLen] \u043d\u0435\u043b\u044c\u0437\u044f     fail();   } catch (NegativeArraySizeException e) {     assert length == 0;   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u0413\u0440\u0430\u0430\u043b\u044c \u0441\u043c\u043e\u0433? \u0414\u0443\u043c\u0430\u044e, \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f <code>length<\/code> \u0432 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0435 \u0432\u044b\u0448\u0435 \u0431\u044b\u043b\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c \u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0432\u043d\u044f\u043b\u043e\u0441\u044c 10, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0441\u0430\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u043e\u043f\u0440\u0430\u0432\u044c\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u0435\u0441\u043b\u0438 \u044f \u043e\u0448\u0438\u0431\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0441\u0451 \ud83d\ude42 \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f.<\/p>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/433174\/\"> https:\/\/habr.com\/ru\/post\/433174\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/433174\/\">\n<p>\u042d\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0440\u0430\u0437\u0431\u043e\u0440 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0442\u0430\u0442\u044c \u044f\u0432\u044c\u044e, \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 &#171;\u043d\u043e&#187;. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043e <code>StringBuilder<\/code>-\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f <a href=\"https:\/\/habr.com\/ru\/post\/436746\/\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e &#171;\u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439&#187;, \u043e\u0442\u043a\u043b\u043e\u043d\u0451\u043d\u043d\u044b\u0445 \u0438\u0437-\u0437\u0430 \u043d\u0435\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0435\u0439 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u044f\u0437\u044b\u043a\u0430, \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u0443\u0433\u043b\u043e\u0432\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u0438\u0447\u0438\u043d. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-295505","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295505"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295505\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}