{"id":321720,"date":"2021-04-19T15:00:58","date_gmt":"2021-04-19T15:00:58","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=321720"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=321720","title":{"rendered":"\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c: \u043d\u044e\u0430\u043d\u0441\u044b \u043f\u0440\u043e\u0442\u0438\u0432 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u0435\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e, \u044d\u0442\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043c\u043e\u0435\u0433\u043e \u0434\u043e\u043a\u043b\u0430\u0434\u0430 \u043d\u0430 <a href=\"https:\/\/jpoint.ru\/2021\/talks\/1ui7ta2itmzurm0vaqmyjw\/\" rel=\"noopener noreferrer nofollow\">JPoint-2021<\/a>. \u041a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0443\u043f\u043e\u0440 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0438 \u0438\u0437 \u0436\u0438\u0437\u043d\u0438 \u0438 \u043d\u0430 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u044b\u0435 \u0432\u0435\u0449\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u043a\u0430\u0436\u0434\u044b\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c. \u041f\u043e\u0434 \u043a\u0430\u0442 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e\u0442\u0441\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u0438\u043c \u0441\u043e\u0447\u0443\u0432\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043d\u044e\u0430\u043d\u0441?<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u0438\u0442\u044c <a href=\"https:\/\/slovar.cc\/enc\/bolshoy\/2105945.html\" rel=\"noopener noreferrer nofollow\">\u0411\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u044d\u043d\u0446\u0438\u043a\u043b\u043e\u043f\u0435\u0434\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0430\u0440\u044e<\/a>, \u0442\u043e \u043d\u044e\u0430\u043d\u0441 (\u0444\u0440\u0430\u043d\u0446. nuance) &#8212; \u044d\u0442\u043e \u043e\u0442\u0442\u0435\u043d\u043e\u043a, \u0442\u043e\u043d\u043a\u043e\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435, \u0435\u0434\u0432\u0430 \u0437\u0430\u043c\u0435\u0442\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 (\u0432 \u0446\u0432\u0435\u0442\u0435, \u043c\u044b\u0441\u043b\u0438, \u0437\u0432\u0443\u043a\u0435 \u0438 \u0442. \u043f.). \u041e\u0447\u0435\u043d\u044c \u043f\u043e\u0448\u043b\u043e\u0435 (\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0435\u043c \u043e\u0447\u0435\u043d\u044c \u0432\u0435\u0440\u043d\u043e\u0435) \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043e \u0432 \u0432\u0438\u0434\u0435 \u0430\u043d\u0435\u043a\u0434\u043e\u0442\u0430 <a href=\"https:\/\/www.anekdot.ru\/id\/101180\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h3>\u041e\u0442\u043a\u0443\u0434\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u043d\u044e\u0430\u043d\u0441\u044b?<\/h3>\n<p>\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e: \u043f\u043e\u0447\u0435\u043c\u0443 \u043e\u0442\u043d\u044e\u0434\u044c \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0433\u043b\u044f\u0434\u044f \u043d\u0430 \u043a\u043e\u0434 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u043e \u0442\u043e\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c? \u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0442\u0432\u0435\u0442: \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e Java \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u0430. \u0420\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442: \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u043e\u0434\u0435\u043b\u044b\u0432\u0430\u044f \u043f\u0443\u0442\u044c \u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0434\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0432\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0442\u0435\u0440\u043f\u0435\u0442\u044c \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0435\u0451 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e. \u0412 \u043e\u0431\u0449\u0438\u0445 \u0447\u0435\u0440\u0442\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<ul>\n<li>\n<p>\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434<\/p>\n<\/li>\n<li>\n<p>\u0431\u0430\u0439\u0442-\u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 (\u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439) \u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043e\u0434 \u043d\u0430 \u043b\u0435\u0442\u0443 \u043f\u043e \u043c\u0435\u0440\u0435 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u044e\u0430\u043d\u0441\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u044d\u0442\u0430\u043f\u043e\u0432, \u0433\u0434\u0435-\u0442\u043e \u043e\u043d\u0438 \u0431\u043e\u043b\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b, \u0433\u0434\u0435-\u0442\u043e \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b \u0432\u043e\u0432\u0441\u0435.<\/p>\n<h3>\u041a\u0430\u043a\u0438\u0435 \u0431\u044b\u0432\u0430\u044e\u0442 \u043d\u044e\u0430\u043d\u0441\u044b?<\/h3>\n<ul>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438\/\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u0441 \u043a\u043e\u043d\u0446\u0430.<\/p>\n<h3>\u041d\u044e\u0430\u043d\u0441\u044b \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u0447\u0442\u043e \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0434\u0430\u0451\u0442 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f <em>\u043b\u043e\u0436\u043d\u044b\u0439<\/em> \u0438, \u043f\u043e \u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e, <strong>\u043e\u043f\u0430\u0441\u043d\u044b\u0439<\/strong> \u0432\u044b\u0432\u043e\u0434: \u0435\u0441\u043b\u0438 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0442\u043e \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043d\u0435\u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e <em>\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439<\/em> \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u0438 \u0442\u0435\u0445 \u0438\u043b\u0438 \u0438\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043a\u043e\u0434\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a\u043e\u0439 \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"java\">byte[] bunn;   for (int i = 0; i &lt; bunn.length; i++) { \/\/ \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \"\u043f\u043e \u0441\u0442\u0430\u0440\u043e\u043c\u0443 \u0441\u0442\u0438\u043b\u044e\"   use(bunn[i]); }  for (byte bunny : bunn) {               \/\/ \u043f\u043e\u0434\u0441\u043b\u0430\u0441\u0442\u0438\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441   use(bunny); }<\/code><\/pre>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/546\/00b\/8de\/54600b8de75b4bb99e407b6cfddb2c59.gif\" width=\"477\" height=\"272\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0434\u0435\u043b\u0430\u044e\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e <code>for-each<\/code>\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u0441\u0443\u0442\u0438 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0430\u0445\u0430\u0440\u043e\u043c \u0434\u043b\u044f <code>for-i<\/code>. \u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441 \ud83d\ude42<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class LoopyBenchmarks {   @Param({ \"32\", \"1024\", \"32768\" })   int size;    byte[] bunn;    @Setup   public void prepare() {     bunn = new byte[size];   }    @Benchmark   public void goodOldLoop(Blackhole fox) {     for (int y = 0; y &lt; bunn.length; y++) {       fox.consume(bunn[y]);     }   }    @Benchmark   public void sweetLoop(Blackhole fox) {     for (byte bunny : bunn) {       fox.consume(bunny);     }   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043c\u043e\u043f\u0435\u0434 \u043d\u0435 \u043c\u043e\u0439, \u043d\u0438\u0436\u0435 \u044f \u0434\u0430\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0430, \u0430 \u043f\u043e\u043a\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u043e\u0433\u043e\u043d:<\/p>\n<pre><code>Benchmark              (size)     Score        Error  Units  goodOldLoop               32     46.630        0.097  ns\/op goodOldLoop             1024   1199.338        0.705  ns\/op goodOldLoop            32768  37813.600       56.081  ns\/op  sweetLoop                 32     19.304        0.010  ns\/op sweetLoop               1024    475.141        1.227  ns\/op sweetLoop              32768  14295.800       36.071  ns\/op<\/code><\/pre>\n<p>\u0412\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u0435\u0442, \u043f\u0440\u0430\u0432\u0434\u0430?<\/p>\n<blockquote>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430 \u0434\u0430\u043b\u043e \u043f\u043e\u0447\u0442\u0438 \u0434\u0432\u0443\u043a\u0440\u0430\u0442\u043d\u044b\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 \u043d\u0430 \u043f\u0443\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435! \u041d\u0443\u0436\u043d\u043e \u0441\u0440\u043e\u0447\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430!<\/p>\n<\/blockquote>\n<p>\u0422\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438 \u044d\u043d\u0442\u0443\u0437\u0438\u0430\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 (\u0442\u0430\u043a \u043a\u043e\u0433\u0434\u0430-\u0442\u043e \u0434\u0443\u043c\u0430\u043b \u0438 \u044f). \u0411\u043e\u043b\u0435\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u043d\u0435 \u0442\u043e\u0440\u043e\u043f\u044f\u0442\u0441\u044f \u0441 \u0432\u044b\u0432\u043e\u0434\u0430\u043c\u0438, \u0432\u0435\u0434\u044c \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0443 \u0440\u043e\u0437\u043d\u044c. \u0411\u044b\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0432 \u043d\u0430\u0448\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043a\u0440\u0430\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043e\u0434\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430? \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e \u0434\u043b\u044f \u043d\u0430\u0441, \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 JMH \u043e\u043f\u0438\u0441\u0430\u043b\u0438 <a href=\"https:\/\/hg.openjdk.java.net\/code-tools\/jmh\/file\/tip\/jmh-samples\/src\/main\/java\/org\/openjdk\/jmh\/samples\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0436\u0438\u0437\u043d\u0438<\/a>, \u0441\u0440\u0435\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0445\u043e\u0434\u0438\u043c <a href=\"https:\/\/hg.openjdk.java.net\/code-tools\/jmh\/file\/523c524305e2\/jmh-samples\/src\/main\/java\/org\/openjdk\/jmh\/samples\/JMHSample_34_SafeLooping.java#l144\" rel=\"noopener noreferrer nofollow\">\u043d\u0443\u0436\u043d\u044b\u0439<\/a>:<\/p>\n<pre><code class=\"java\">@Fork(3) @State(Scope.Thread) @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class JMHSample_34_SafeLooping {   @Param({\"1\", \"10\", \"100\", \"1000\"})   int size;    int[] xs;    @Setup   public void setup() {     xs = new int[size];     for (int c = 0; c &lt; size; c++) {       xs[c] = c;     }   }    @Benchmark   public void measureRight_1(Blackhole bh) {     for (int x : xs) {       bh.consume(work(x));     }   }      static final int BASE = 42;    static int work(int x) {     return BASE + x;   } }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043a\u0430\u043a \u0432 \u043b\u0443\u0447\u0448\u0438\u0445 \u0434\u043e\u043c\u0430\u0445 \u041f\u043e\u0442\u0441\u0434\u0430\u043c\u0430 \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u0432\u0435\u0440\u043d\u044b, \u043d\u043e \u0438\u0437 \u043d\u0438\u0445 \u0441\u0434\u0435\u043b\u0430\u043d \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434. \u0410 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0441\u0434\u0435\u043b\u0430\u043b \u041d\u0438\u0446\u0430\u043d \u0412\u0430\u043a\u0430\u0440\u0442 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"http:\/\/psy-lob-saw.blogspot.com\/2014\/08\/the-volatile-read-suprise.html\" rel=\"noopener noreferrer nofollow\">The volatile read surprise<\/a>:<\/p>\n<pre><code class=\"java\">\/\/ \u0441\u0442\u0430\u0440\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 org.openjdk.jmh.infra.Blackhole (\u0434\u043e JMH 1.3)  public volatile byte b1, b2; public volatile BlackholeL2 nullBait = null;  \/**  * Consume object. This call provides a side effect  * preventing JIT to eliminate dependent computations.  *  * @param b object to consume.  *\/ public final void consume(byte b) {   if (b == b1 &amp; b == b2) {     \/\/ SHOULD NEVER HAPPEN     nullBait.b1 = b; \/\/ implicit null pointer exception   } }<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 <code>Blackhole::consume<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0434\u0432\u0430 \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u044d\u0444\u0444\u0435\u043a\u0442 \u0447\u0451\u0440\u043d\u043e\u0433\u043e \u044f\u0449\u0438\u043a\u0430, \u043d\u0435 \u0434\u0430\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 <code>b<\/code>, \u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u043e\u043b\u044e <code>bunn<\/code> \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0441\u0442\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u043d\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0438 \u043f\u0440\u043e\u0445\u043e\u0434 \u043f\u043e \u043d\u0435\u0439 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438:<\/p>\n<pre><code class=\"java\">@Benchmark public void goodOldLoopReturns(Blackhole fox) {   byte[] sunn = bunn; \/\/ make a local copy of the field   for (int y = 0; y &lt; sunn.length; y++) {     fox.consume(sunn[y]);   } }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>goodOldLoopReturns<\/code>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435 \u0431\u044b\u0441\u0442\u0440\u043e, \u043a\u0430\u043a \u0438 <code>for-each<\/code><\/p>\n<pre><code>Benchmark              (size)     Score        Error  Units  goodOldLoopReturns        32     19.306        0.045  ns\/op goodOldLoopReturns      1024    476.493        1.190  ns\/op goodOldLoopReturns     32768  14292.286       16.046  ns\/op  sweetLoop                 32     19.304        0.010  ns\/op sweetLoop               1024    475.141        1.227  ns\/op sweetLoop              32768  14295.800       36.071  ns\/op<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, <code>for-each<\/code> \u0432\u044b\u0438\u0433\u0440\u044b\u0432\u0430\u043b \u043b\u0438\u0448\u044c \u0438\u0437-\u0437\u0430 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e\u043b\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e:<\/p>\n<pre><code class=\"java\">@Benchmark public void sweetLoop(Blackhole fox) {   for (byte bunny : bunn) {     fox.consume(bunny);   } }<\/code><\/pre>\n<p>\u043f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432<\/p>\n<pre><code class=\"java\">@Benchmark public void sweetLoop(Blackhole fox) {   byte[] var2 = this.bunn;                  \/\/ &lt;---   int var3 = var2.length;    for(int var4 = 0; var4 &lt; var3; ++var4) {     byte bunny = var2[var4];     fox.consume(bunny);   } }<\/code><\/pre>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0438 \u043a \u0432\u044b\u0432\u043e\u0434\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430:<\/p>\n<blockquote>\n<p>REMEMBER: <strong>The numbers below are just data.<\/strong> To gain reusable insights, you need to follow up on why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial experiments, perform baseline and negative tests that provide experimental control, make sure the benchmarking environment is safe on JVM\/OS\/HW level, ask for reviews from the domain experts. <strong>Do not assume the numbers tell you what you want them to tell.<\/strong><\/p>\n<\/blockquote>\n<p>\u041d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0441\u0438\u043d \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e <strong>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0442\u043e\u043b\u043a\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u044c \u0436\u0435 \u0432\u0430\u0436\u043d\u043e, \u043a\u0430\u043a \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430<\/strong>. \u0426\u0438\u0444\u0440\u044b \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u043d\u0438\u0447\u0442\u043e, \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 &#8212; \u0432\u0441\u0451.<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435 \u043a Blackhole <\/summary>\n<div class=\"spoiler__content\">\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 1.3 \u0438 \u0434\u043e 1.28 \u043a\u043b\u0430\u0441\u0441 Blackhole \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u0434\u043d\u043e\u0435 \u0432\u043e\u0437\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430:<\/p>\n<pre><code class=\"java\">public volatile byte b1;  \/\/ \u0442\u043e\u043b\u044c\u043a\u043e b1 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u043a\u0430\u043a volatile public byte b2;  public final void consume(byte b) {   byte b1 = this.b1;   byte b2 = this.b2;   if (b == b1 &amp; b == b2) {     nullBait.b1 = b;   } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0441\u043a\u0440\u043e\u043c\u043d\u0435\u0435:<\/p>\n<pre><code>                    (size)      Score      Error  Units goodOldLoop             32     70.307 \u00b1    0.752  ns\/op goodOldLoop           1024   2239.560 \u00b1   21.611  ns\/op goodOldLoop          32768  70890.224 \u00b1 3475.436  ns\/op  sweetLoop               32    103.163 \u00b1   39.564  ns\/op sweetLoop             1024   1812.092 \u00b1    4.234  ns\/op sweetLoop            32768  57918.608 \u00b1  249.921  ns\/op  goodOldLoopReturns      32     59.757 \u00b1    0.748  ns\/op goodOldLoopReturns    1024   1843.383 \u00b1   37.246  ns\/op goodOldLoopReturns   32768  57704.539 \u00b1  217.073  ns\/op<\/code><\/pre>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u044d\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u043f\u0440\u0435\u0434\u0435\u043b (\u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0435 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u0431\u044b\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e). \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 1.29 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0451\u0440\u043d\u043e\u0433\u043e \u044f\u0449\u0438\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043d\u044b\u0445 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"java\">private static final boolean COMPILER_BLACKHOLE = Boolean.getBoolean(\"compilerBlackholesEnabled\");  public final void consume(byte b) {   if (COMPILER_BLACKHOLE) {     consumeCompiler(b);   } else {     consumeFull(b); \/\/ \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e   } }  \/\/ Compiler blackholes block: let compilers figure out how to deal with it. private static void consumeCompiler(byte v) {}  \/\/ Full blackholes block: confuse compilers to get blackholing effects. \/\/ See implementation comments at the top to understand what this code is doing. private void consumeFull(byte b) {     byte b1 = this.b1; \/\/ volatile read     byte b2 = this.b2;     if ((b ^ b1) == (b ^ b2)) {         nullBait.b1 = b; \/\/ implicit null pointer exception     } }<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041a\u0440\u0438\u0442\u0438\u043a \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0441\u043a\u0430\u0436\u0435\u0442: &#171;\u041d\u043e \u044d\u0442\u043e \u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440!&#187;. \u041d\u0430 \u0434\u0435\u043b\u0435, \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0443\u0436 \u0438 \u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439:<\/p>\n<pre><code class=\"java\">byte[] bunn;  @Benchmark public void goodOldLoop(Blackhole fox) {   for (int y = 0; y &lt; bunn.length; y++) {     fox.consume(bunn[y]);   } }  \/\/-------------------  E[] elements;  @Override public void forEach(Consumer&lt;E&gt; consumer) {   for (int i = 0; i &lt; elements.length; i++) {     consumer.accept(elements[i]);   } }<\/code><\/pre>\n<p>\u0414\u0430-\u0434\u0430, \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 \u043a\u0430\u043a \u0434\u0432\u0435 \u043a\u0430\u043f\u043b\u0438 \u0432\u043e\u0434\u044b \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>Iterable.forEach(Consumer)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0441\u0442\u044c \u0443 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0412\u044b\u0448\u0435 \u043c\u044b \u0443\u0431\u0435\u0434\u0438\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u0432 99 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0437 100 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u043d\u043e \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043e\u0442\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0432\u0441\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043b\u0438\u0431\u043e <code>for-each<\/code>, \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u044f\u0432\u043d\u0443\u044e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 \u0441\u0442\u0435\u043a\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>j.u.ArrayList<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">@Override public void forEach(Consumer&lt;? super E&gt; action) {   Objects.requireNonNull(action);   final int expectedModCount = modCount;   final Object[] es = elementData;   final int size = this.size;   for (int i = 0; modCount == expectedModCount &amp;&amp; i &lt; size; i++)     action.accept(elementAt(es, i));   if (modCount != expectedModCount)     throw new ConcurrentModificationException(); }<\/code><\/pre>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>j.u.Arrays$ArrayList<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">@Override public void forEach(Consumer&lt;? super E&gt; action) {   Objects.requireNonNull(action);   for (E e : a) {     action.accept(e);   } }<\/code><\/pre>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>j.u.c.CopyOnWriteArrayList<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">public void forEach(Consumer&lt;? super E&gt; action) {   Objects.requireNonNull(action);   for (Object x : getArray()) {     @SuppressWarnings(\"unchecked\") E e = (E) x;     action.accept(e);   } }<\/code><\/pre>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>j.u.ArrayDeque<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">public void forEach(Consumer&lt;? super E&gt; action) {   Objects.requireNonNull(action);   final Object[] es = elements;   for (int i = head, end = tail, to = (i &lt;= end) ? end : es.length;          ; i = 0, to = end) {     for (; i &lt; to; i++)       action.accept(elementAt(es, i));     if (to == end) {       if (end != tail) throw new ConcurrentModificationException();         break;     }   } }<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c  \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u0438\u0434\u0435\u043d \u043a\u0440\u0443\u0433\u043e\u0432\u043e\u0440\u043e\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u043d\u044e\u0430\u043d\u0441\u043e\u0432 \u0432 \u043f\u0440\u0438\u0440\u043e\u0434\u0435:<\/p>\n<ul>\n<li>\n<p>\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0443\u044e \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u043c\u0435\u0436\u0434\u0443 for-i \u0438 for-each<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441: \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 JMH<\/p>\n<\/li>\n<li>\n<p>\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432 99 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0437 100 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441: \u0435\u0441\u0442\u044c \u0440\u0435\u0434\u043a\u0438\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0451 \u0436\u0435 \u0432\u043b\u0438\u044f\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<h3>\u041d\u044e\u0430\u043d\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/blob\/ff234568d20330e79ba0d9f0149f00bee211c528\/spring-core\/src\/main\/java\/org\/springframework\/util\/StringUtils.java#L763\" rel=\"noopener noreferrer nofollow\">\u043c\u0435\u0442\u043e\u0434<\/a>  <code>o.s.util.StringUtils.uriDecode(String, Charset)<\/code>. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u0432\u0440\u043e\u0434\u0435<\/p>\n<pre><code class=\"xml\">https%3A%2F%2Fru.wikipedia.org%2Fwiki%2F%D0%9E%D1%80%D0%B3%D0%B0%D0%BD%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F_%D0%9E%D0%B1%D1%8A%D0%B5%D0%B4%D0%B8%D0%BD%D1%91%D0%BD%D0%BD%D1%8B%D1%85_%D0%9D%D0%B0%D1%86%D0%B8%D0%B9<\/code><\/pre>\n<p>\u0432 \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043e\u0447\u0438\u0442\u0430\u0435\u043c\u0443\u044e<\/p>\n<pre><code>https:\/\/ru.wikipedia.org\/wiki\/\u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f_\u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u0445_\u041d\u0430\u0446\u0438\u0439<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0439, \u0430 \u043d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0441\u0442\u0440\u043e\u043a\u0430:<\/p>\n<pre><code class=\"java\">public static String uriDecode(String source, Charset charset) {   int length = source.length();   if (length == 0) {     return source;   }   Assert.notNull(charset, \"Charset must not be null\");   ByteArrayOutputStream bos = new ByteArrayOutputStream(length);   boolean changed = false;   for (int i = 0; i &lt; length; i++) {     \/\/ ...   }   return changed ? new String(bos.toByteArray(), charset) : source; }<\/code><\/pre>\n<p>\u0410 \u0432 \u043d\u0435\u0439 &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>ByteArrayOutputStream.toByteArray()<\/code>:<\/p>\n<pre><code class=\"java\">return changed ? new String(bos.toByteArray(), charset) : source;<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0433\u043e\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 <code>ByteArrayOutputStream<\/code>:<\/p>\n<pre><code class=\"java\">public synchronized byte[] toByteArray() {   return Arrays.copyOf(this.buf, this.count); }<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u043b\u043e\u0433\u0438\u0447\u043d\u043e: \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0438\u0437 \u043a\u043e\u043f\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>ByteArrayOutputStream.toString(String charsetName)<\/code>:<\/p>\n<pre><code class=\"java\">public synchronized String toString(String charsetName) throws Exception {   return new String(buf, 0, count, charsetName); }<\/code><\/pre>\n<p>\u041e\u043d \u0434\u0435\u043b\u0430\u0435\u0442 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>new String(bos.toByteArray(), charset)<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0435\u0437 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438 <code>ByteArrayOutputStream.toString()<\/code>, \u0438 <code>ByteArrayOutputStream.toByteArray()<\/code> \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u044b, \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 <code>byte[]<\/code>, \u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>new String(bos.toByteArray(), charset)<\/code> \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c <code>bos.toString(charset)<\/code>, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u043f\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0417\u0434\u0440\u0430\u0432\u044b\u0439 \u0441\u043c\u044b\u0441\u043b \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442, \u0447\u0442\u043e \u0438\u0437\u0431\u0430\u0432\u0438\u0432\u0448\u0438\u0441\u044c \u043e\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \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. <a href=\"https:\/\/github.com\/stsypanov\/strings\/blob\/master\/src\/jmh\/java\/tsypanov\/strings\/baos\/ByteArrayOutputStreamBenchmark.java\" rel=\"noopener noreferrer nofollow\">\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/a> \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442 \u0434\u043e\u0433\u0430\u0434\u043a\u0443:<\/p>\n<pre><code>                                 (length)     Score     Error   Units newString                               0    66.084 \u00b1   2.121   ns\/op newString                              10    53.232 \u00b1   1.569   ns\/op newString                             100    96.067 \u00b1   1.466   ns\/op newString                            1000   667.188 \u00b1  33.071   ns\/op  toString                                0    44.601 \u00b1   0.656   ns\/op toString                               10    45.151 \u00b1   0.397   ns\/op toString                              100    77.924 \u00b1   0.645   ns\/op toString                             1000   511.302 \u00b1   1.730   ns\/op  newString:\u00b7gc.alloc.rate.norm           0    96.000 \u00b1   0.001    B\/op newString:\u00b7gc.alloc.rate.norm          10   136.000 \u00b1   0.001    B\/op newString:\u00b7gc.alloc.rate.norm         100   400.000 \u00b1   0.001    B\/op newString:\u00b7gc.alloc.rate.norm        1000  3096.001 \u00b1   0.001    B\/op  toString:\u00b7gc.alloc.rate.norm            0    40.000 \u00b1   0.001    B\/op toString:\u00b7gc.alloc.rate.norm           10    64.000 \u00b1   0.001    B\/op toString:\u00b7gc.alloc.rate.norm          100   240.000 \u00b1   0.001    B\/op toString:\u00b7gc.alloc.rate.norm         1000  2040.001 \u00b1   0.001    B\/op<\/code><\/pre>\n<p>\u041f\u043e\u0445\u043e\u0436\u0435, \u043d\u0430\u0448 \u0440\u0430\u0441\u0447\u0451\u0442 \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043b\u0441\u044f: \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0435\u0441\u0442\u044c \u043a\u0430\u043a \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0442\u0430\u043a \u0438 \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u0422\u0435\u043f\u0435\u0440\u044c <a href=\"https:\/\/gist.github.com\/stsypanov\/600692e23c215607cb5e66dd73139b38\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c<\/a>, \u0443\u043b\u0443\u0447\u0448\u0438\u043b\u0430\u0441\u044c \u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <code>StringUtils.uriDecode(String, Charset)<\/code>:<\/p>\n<pre><code>                                            Score    Error   Units before uriDecode                                 766.285 \u00b1 22.110   ns\/op uriDecode:\u00b7gc.alloc.rate.norm             816.001 \u00b1  0.001    B\/op  after uriDecode                                 720.208 \u00b1  5.968   ns\/op uriDecode:\u00b7gc.alloc.rate.norm             712.001 \u00b1  0.001    B\/op<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u043e\u0441\u044c \u043d\u0430 6%, \u0430 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 &#8212; \u043d\u0430 12,75%. \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u043c\u0435\u043b\u043e\u0447\u044c, \u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u043e, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0447\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0443 <a href=\"https:\/\/youtrack.jetbrains.com\/issue\/IDEA-238645\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0436\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/a>.<\/p>\n<h4>\u0413\u0434\u0435 \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u0430\u043c \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u0436\u0435 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u043f\u0440\u043e\u0447\u0438\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a <code>java.io.CharArrayWriter<\/code>:<\/p>\n<pre><code class=\"java\">public char[] toCharArray() {   synchronized(this.lock) {     return Arrays.copyOf(this.buf, this.count);   } }  public String toString() {   synchronized(this.lock) {     return new String(this.buf, 0, this.count);   } }<\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/stsypanov\/strings\/blob\/master\/src\/jmh\/java\/tsypanov\/strings\/character\/CharArrayWriterBenchmark.java\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c<\/a>, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434 <code>toString()<\/code> \u0432\u044b\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0443 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430:<\/p>\n<pre><code>                                 (length)     Score   Error   Units toCharArray                             5    19.767 \u00b1 0.134   ns\/op toCharArray                            10    18.664 \u00b1 0.128   ns\/op toCharArray                            50    21.823 \u00b1 0.264   ns\/op toCharArray                           100    29.041 \u00b1 0.145   ns\/op toCharArray                          1000   250.129 \u00b1 2.446   ns\/op  toString                                5    15.361 \u00b1 0.752   ns\/op toString                               10    14.472 \u00b1 0.251   ns\/op toString                               50    16.048 \u00b1 0.102   ns\/op toString                              100    19.245 \u00b1 0.137   ns\/op toString                             1000    82.624 \u00b1 1.824   ns\/op  toCharArray:\u00b7gc.alloc.rate.norm         5    80.000 \u00b1 0.001    B\/op toCharArray:\u00b7gc.alloc.rate.norm        10    96.000 \u00b1 0.001    B\/op toCharArray:\u00b7gc.alloc.rate.norm        50   216.000 \u00b1 0.001    B\/op toCharArray:\u00b7gc.alloc.rate.norm       100   360.000 \u00b1 0.001    B\/op toCharArray:\u00b7gc.alloc.rate.norm      1000  3056.000 \u00b1 0.001    B\/op  toString:\u00b7gc.alloc.rate.norm            5    48.000 \u00b1 0.001    B\/op toString:\u00b7gc.alloc.rate.norm           10    56.000 \u00b1 0.001    B\/op toString:\u00b7gc.alloc.rate.norm           50    96.000 \u00b1 0.001    B\/op toString:\u00b7gc.alloc.rate.norm          100   144.000 \u00b1 0.001    B\/op toString:\u00b7gc.alloc.rate.norm         1000  1040.000 \u00b1 0.001    B\/op<\/code><\/pre>\n<p>\u0412 JDK \u0435\u0441\u0442\u044c \u0434\u0430\u0436\u0435 \u043f\u043e\u0434\u043e\u043f\u044b\u0442\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 &#8212; <code>java.net.URLEncoder<\/code>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 <code>CharArrayWriter<\/code> \u0432 <a href=\"https:\/\/hg.openjdk.java.net\/jdk\/jdk\/file\/01c5097cf1cd\/src\/java.base\/share\/classes\/java\/net\/URLEncoder.java#l220\" rel=\"noopener noreferrer nofollow\">\u043c\u0435\u0442\u043e\u0434\u0435<\/a> <code>encode()<\/code> \u0441\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"java\">String str = new String(charArrayWriter.toCharArray());<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0437\u0434\u0435\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430\u044f \u0437\u0430\u043c\u0435\u043d\u0430:<\/p>\n<pre><code class=\"java\">String str = charArrayWriter.toString();<\/code><\/pre>\n<p>\u0421\u043e\u0431\u0435\u0440\u0451\u043c JDK \u0438 \u0438\u0437\u043c\u0435\u0440\u0438\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class UrlEncoderBenchmark {   Charset charset = Charset.defaultCharset();  String utf8Url = \"https:\/\/ru.wikipedia.org\/wiki\/\u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f_\u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u0445_\u041d\u0430\u0446\u0438\u0439\";   @Benchmark  public String encodeUtf8() {    return URLEncoder.encode(utf8Url, charset);  } }<\/code><\/pre>\n<p>\u0418 \u0432\u0434\u0440\u0443\u0433:<\/p>\n<pre><code>                                       Score      Error   Units  before                              1108.668 \u00b1    6.226   ns\/op after                               1454.647 \u00b1    6.067   ns\/op   &lt;-- !!!  before:\u00b7gc.alloc.rate.norm          1712.202 \u00b1    0.003    B\/op after:\u00b7gc.alloc.rate.norm           1528.219 \u00b1    0.007    B\/op<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u043e, \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043f\u043e\u0447\u0442\u0438 \u043d\u0430 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044c! \u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u0432 \u043e\u0434\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0441\u0451 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u0430 \u0432 \u043f\u043e\u0445\u043e\u0436\u0435\u043c \u043a\u0430\u043a \u0434\u0432\u0435 \u043a\u0430\u043f\u043b\u0438 \u0432\u043e\u0434\u044b \u043c\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043f\u0440\u0438\u0440\u043e\u0441\u0442\u0430, \u043d\u043e \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442 &#8212; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443\u0445\u0443\u0434\u0448\u0438\u043b\u0430\u0441\u044c? \u0421 \u044d\u0442\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u044f \u043f\u043e\u0448\u0435\u043b \u0432 \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0443 <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2020-July\/039230.html\" rel=\"noopener noreferrer nofollow\">hotspot-compiler-dev<\/a>, \u0442. \u043a. \u0431\u044b\u043b \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u0440\u0435\u0447\u044c \u0438\u0434\u0451\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430. \u0422\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043b\u043e\u0433 \u0432\u043a\u043b\u0435\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u043e \u043c\u043e\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<pre><code>   @ 186  j.i.CharArrayWriter::flush (1 bytes)   inline (hot) !m @ 195  j.i.CharArrayWriter::toCharArray (26 bytes)   inline (hot)      @ 15  j.u.Arrays::copyOf (19 bytes)   inline (hot)        @ 11  j.l.Math::min (11 bytes)   (intrinsic)        @ 14  j.l.System::arraycopy (0 bytes)   (intrinsic)    @ 198  j.l.String::&lt;init&gt; (10 bytes)   inline (hot)      @ 6  j.l.String::&lt;init&gt; (74 bytes)   inline (hot)        @ 1  j.l.Object::&lt;init&gt; (1 bytes)   inline (hot)        @ 36  j.l.StringUTF16::compress (20 bytes)   inline (hot)          @ 9  j.l.StringUTF16::compress (50 bytes)   (intrinsic)        @ 67  j.l.StringUTF16::toBytes (34 bytes)   (intrinsic)<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0438\u043c \u043e\u043d \u0441\u0442\u0430\u043b \u043f\u043e\u0441\u043b\u0435 (\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0432\u0442\u043e\u0440\u0443\u044e \u0438 \u0441\u0435\u0434\u044c\u043c\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0438):<\/p>\n<pre><code>   @ 186 j.i.CharArrayWriter::flush (1 bytes)  inline (hot) !m @ 191 j.i.CharArrayWriter::toString (31 bytes) already compiled into a big method    @ 199 j.l.String::getBytes (25 bytes)   inline (hot)     @ 14 j.l.String::coder (15 bytes)   inline (hot) !   @ 21 j.l.StringCoding::encode (324 bytes)   inline (hot)      @ 10 j.l.StringCoding::encodeUTF8 (132 bytes)   inline (hot)       @ 7 j.l.StringCoding::encodeUTF8_UTF16 (369 bytes) hot method too big       @ 15 j.l.StringCoding::hasNegatives (25 bytes)   (intrinsic)       @ 24 j.u.Arrays::copyOf (19 bytes)   inline (hot)        @ 11 j.l.Math::min (11 bytes)   (intrinsic)        @ 14 j.l.System::arraycopy (0 bytes)   (intrinsic) <\/code><\/pre>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e <code>CharArrayWriter::toString<\/code> \u0438 <code>StringCoding::encodeUTF8_UTF16<\/code> \u043d\u0435 \u0431\u044b\u043b\u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u0432 \u0442\u043e\u0447\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u0437-\u0437\u0430 \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 (\u0445\u043e\u0442\u044f \u043e\u0431\u0430 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e). \u0422\u0430\u043a\u0436\u0435 \u0432 \u043b\u043e\u0433\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c (\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0441\u0442\u0440\u043e\u043a\u0443):<\/p>\n<pre><code class=\"xml\">&lt;method id=\"1166\" holder=\"1154\" name=\"toString\" return=\"1032\" flags=\"1\" bytes=\"31\" compile_id=\"1062\" compiler=\"c2\" level=\"4\" iicount=\"11163\"&gt; &lt;dependency type=\"unique_concrete_method\" ctxk=\"1154\" x=\"1166\"&gt; &lt;call method=\"1166\" count=\"75859\" prof_factor=\"1,000000\" inline=\"1\"&gt; &lt;inline_fail reason=\"already compiled into a big method\"&gt;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0434\u044b\u0440\u0430, \u0433\u0434\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0441\u0442\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430, \u0447\u0442\u043e \u043b\u043e\u043c\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u0435\u0434\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0430\u0441 \u0436\u0434\u0451\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0447\u0451\u0440\u043d\u043e\u0433\u043e \u044f\u0449\u0438\u043a\u0430 (\u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 <code>Blackhole<\/code> \u043b\u043e\u043c\u0430\u043b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435).<\/p>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0451\u043c\u0430 \u043a\u043e\u0434\u0430 \u0432\u044b\u0437\u0432\u0430\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430:<\/p>\n<pre><code class=\"java\">\/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043f\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 public String(char value[]) {   this(value, 0, value.length, null); }  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 CharArrayWriter.toString() public String(char[] value, int offset, int count) {   this(value, offset, count, rangeCheck(value, offset, count)); }  \/\/ \u0437\u0434\u0435\u0441\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 String(char[] value, int off, int len, Void sig) {...}<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>char[]<\/code>-&gt; <code>byte[]<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0430 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0433\u0440\u0430\u043d\u0438\u0446 \u0432 \u0441\u043a\u0440\u043e\u043c\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 <code>rangeCheck(char[], int, int)<\/code>:<\/p>\n<pre><code class=\"java\">private static Void rangeCheck(char[] value, int offset, int count) {   checkBoundsOffCount(offset, count, value.length);   return null; }  static void checkBoundsOffCount(int offset, int count, int length) {   if (offset &lt; 0 || count &lt; 0 || offset &gt; length - count) {     throw new StringIndexOutOfBoundsException(       \"offset \" + offset + \", count \" + count + \", length \" + length);   } }<\/code><\/pre>\n<p>\u043d\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2020-July\/039233.html\" rel=\"noopener noreferrer nofollow\">\u0412\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c<\/a>, \u0447\u0442\u043e<\/p>\n<blockquote>\n<p>C2 uses a bunch of of heuristics. Here, it&#8217;s detected that <code>CharArrayWriter::toString<\/code>is large and has already been compiled so there&#8217;s no sense inlining another copy of it. This isn&#8217;t necessarily true, but it&#8217;s a good guess. Try playing with <code>InlineSmallCode<\/code> start with = 1000, and increases it from there to see if it helps.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/www.oracle.com\/java\/technologies\/javase\/vmoptions-jsp.html\" rel=\"noopener noreferrer nofollow\">\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435<\/a> -XX:InlineSmallCode \u0437\u0432\u0443\u0447\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<blockquote>\n<p>Inline a previously compiled method only if its generated native code size is less than this. The default value varies with the platform on which the JVM is running.<\/p>\n<\/blockquote>\n<p>\u0411\u043e\u043b\u0435\u0435 \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u043e\u0435 (\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435, \u043a\u0430\u043a \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f) \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043e \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/536514\/\" rel=\"noopener noreferrer nofollow\">&#171;Java HotSpot JIT \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u2014 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e, \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 (\u0447\u0430\u0441\u0442\u044c 2)&#187;<\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043a \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044e:<\/p>\n<blockquote>\n<p>\u0412\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u044e \u043d\u0435 \u043f\u043e\u0434\u043b\u0435\u0436\u0430\u0442 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0435 \u0431\u043e\u043b\u0435\u0435 1000 \u0431\u0430\u0439\u0442 \u043f\u0440\u0438 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 2000 \u0431\u0430\u0439\u0442 \u043f\u0440\u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u0439<\/p>\n<\/blockquote>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0432\u0441\u0451 \u0432\u044b\u0448\u0435\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u044f \u0441\u043e\u0437\u0434\u0430\u043b <a href=\"https:\/\/github.com\/openjdk\/jdk\/pull\/1598\" rel=\"noopener noreferrer nofollow\">\u041f\u0420<\/a> \u0438 \u043f\u0440\u043e\u0442\u043e\u043b\u043a\u043d\u0443\u043b \u0441\u0432\u043e\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/c_\/vl\/ez\/c_vlezagmokym-90f5qdxxyjiuu.png\" width=\"1396\" height=\"421\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u041f\u0420, \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u0441 \u043c\u043e\u0438\u043c: <a href=\"https:\/\/github.com\/openjdk\/jdk\/pull\/705\" rel=\"noopener noreferrer nofollow\">8254913: Increase InlineSmallCode default from 2000 to 2500 for x64<\/a>.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0435\u0433\u043e \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0436\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442:<\/p>\n<pre><code>original                                1166.3    ns\/op patched                                 1058.8    ns\/op<\/code><\/pre>\n<p>\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u0430 \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u0432\u043a\u043b\u0435\u0438\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0451 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0446\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0430? <a href=\"https:\/\/mail.openjdk.java.net\/pipermail\/hotspot-compiler-dev\/2020-July\/039315.html\" rel=\"noopener noreferrer nofollow\">\u041e\u0442\u0432\u0435\u0442<\/a>:<\/p>\n<blockquote>\n<p>If you never share code you ends up with several giga bytes of assembly codes which destroy your perf because you start to have a lot of cache miss on the instructions.   So there is a trade off between a theoritical fully inlined program and a never inlined program.<\/p>\n<\/blockquote>\n<p>\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u043a\u043b\u0435\u0435\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443: \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0446\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0430. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u043d\u044c\u0448\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0442\u0435\u043b\u043e \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c <code>String.isEmpty()<\/code>, \u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u0435\u043b\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043a\u043b\u0435\u0435\u043d\u043e \u0432 <strong>\u043a\u0430\u0436\u0434\u0443\u044e<\/strong> \u0442\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0451\u043c \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438\u0437-\u0437\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u0440\u0430\u0441\u0442\u0435\u0442 \u043b\u0430\u0432\u0438\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e.  \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u0441\u044f \u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043a\u0443\u0441\u043e\u043a \u0432 \u043a\u044d\u0448 \u0446\u0435\u043b\u0438\u043a\u043e\u043c? \u0412\u0440\u044f\u0434 \u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a \u043f\u0440\u043e\u043c\u0430\u0445\u0430\u043c \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043a\u044d\u0448\u0443 (cache-miss), \u0430 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0431\u0438\u0442\u044c \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0432\u0435\u0434\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0443 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438\u0437 \u041e\u0417\u0423 \u0432 \u043a\u044d\u0448. <\/p>\n<p>\u0412\u0432\u0438\u0434\u0443 \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0412\u041c \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441\u0443 \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c. <\/p>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p>\u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f &#171;\u043f\u043e\u0434 \u043a\u043e\u043f\u0438\u0440\u043a\u0443&#187; \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u0441\u043b\u0435 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/p>\n<\/li>\n<\/ul>\n<h3>\u041d\u044e\u0430\u043d\u0441\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u043e \u0432\u0432\u043e\u0434\u043e\u043c-\u0432\u044b\u0432\u043e\u0434\u043e\u043c \u0437\u043d\u0430\u044e\u0442, \u0447\u0442\u043e \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f &#8212; \u044d\u0442\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0434\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\/\u0438\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u043e\u0432 \u0441\u0442\u043e\u044f\u0442 \u0434\u043e\u0440\u043e\u0433\u043e:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class BufferedReaderBenchmark {   private File file;    @Setup   public void setUp() throws Exception {     String path = \"tsypanov\/strings\/buffered\/BufferReaderBenchmark.class\";     URL resource = getClass()       .getClassLoader()       .getResource(path);     file = new File(resource.getFile());   }    @Benchmark   public void readFromFile(Blackhole bh) throws Exception {     int value;     try (var is = new FileInputStream(file)) {       while ((value = is.read()) != -1) {         bh.consume(value);       }     }   } } <\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>readFromFile()<\/code>\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u0430 \u0431\u0430\u0439\u0442 \u0437\u0430 \u0431\u0430\u0439\u0442\u043e\u043c:<\/p>\n<pre><code>Benchmark                                    Score    Error  Units  readFromFile                              2561.488 \u00b1 16.869  us\/op readFromFile:\u00b7gc.alloc.rate.norm           148.550 \u00b1  2.144   B\/op<\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">@Benchmark public void readBufferedFromFile(Blackhole bh) throws Exception {   int value;   try (var is = new BufferedInputStream(new FileInputStream(file))) {     while ((value = is.read()) != -1) {       bh.consume(value);     }   } }<\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u043e\u0441\u044c \u0432 \u0441\u043a\u0440\u043e\u043c\u043d\u044b\u0445 20 \u0440\u0430\u0437:<\/p>\n<pre><code>Benchmark                                    Score    Error   Units  readFromFile                              2561.488 \u00b1 16.869   us\/op readBufferedFromFile                        92.418 \u00b1  3.145   us\/op  readFromFile:\u00b7gc.alloc.rate.norm           148.550 \u00b1  2.144    B\/op readBufferedFromFile:\u00b7gc.alloc.rate.norm  8393.668 \u00b1  0.264    B\/op<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0437\u0430 \u0441\u0447\u0451\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u0443\u0434\u0430 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u0430\u0447\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0434\u0435\u0448\u0435\u0432\u043b\u044f\u044f \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0447\u0442\u0435\u043d\u0438\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f, \u043e \u0447\u0451\u043c \u0441\u0432\u0438\u0434\u0435\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0437\u0440\u044b\u0432\u043d\u043e\u0439 \u0440\u043e\u0441\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u0418 \u0432\u0441\u0451 \u0436\u0435 \u043e\u0431\u0449\u0430\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0433\u043e\u043d \u0441 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code>Benchmark                                    Score     Error   Units  readFromFile                               388.860 \u00b1   0.960   ops\/s readBufferedFromFile                      4762.997 \u00b1 609.229   ops\/s<\/code><\/pre>\n<p>\u0414\u0430\u0436\u0435 \u0432\u044b\u0440\u043e\u0441\u0448\u0435\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 (\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 GC) \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u0447\u0442\u0435\u043d\u0438\u044e \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e\u0435.<\/p>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e. \u0413\u0434\u0435 \u0436\u0435 \u043d\u044e\u0430\u043d\u0441? \u0410 \u043d\u044e\u0430\u043d\u0441 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class BufferedReaderBenchmark {    private URL url;    @Setup   public void setUp() throws Exception {     String path = \"tsypanov\/strings\/buffered\/BufferReaderBenchmark.class\";     URL resource = getClass()       .getClassLoader()       .getResource(path);     url = resource.toURI().toURL();   }    @Benchmark   public void readFromURL(Blackhole bh) throws Exception {     int value;     try (var is = url.openStream()) {       while ((value = is.read()) != -1) {         bh.consume(value);       }     }   }    @Benchmark   public void readBufferedFromURL(Blackhole bh) throws Exception {     int value;     try (var is = new BufferedInputStream(url.openStream())) {       while ((value = is.read()) != -1) {         bh.consume(value);       }     }   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0437\u0430 \u043e\u0434\u043d\u0438\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c: \u0437\u0434\u0435\u0441\u044c \u0447\u0438\u0442\u0430\u0435\u043c \u043d\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0430 \u0438\u0437 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043d\u0435\u0433\u043e. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code>Benchmark                                    Score      Error   Units  readFromURL                                 93.332 \u00b1    3.190   us\/op readBufferedFromURL                         93.802 \u00b1    7.419   us\/op  readFromURL:\u00b7gc.alloc.rate.norm           8592.120 \u00b1    3.636    B\/op readBufferedFromURL:\u00b7gc.alloc.rate.norm  16835.122 \u00b1    0.246    B\/op<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0430 \u0447\u0442\u0435\u043d\u0438\u0435, \u043d\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043f\u043e\u0447\u0442\u0438 \u0432 2 \u0440\u0430\u0437\u0430. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>FileURLConnection.connect()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 <code>FileURLConnection.getInputStream()<\/code>:<\/p>\n<pre><code class=\"java\">public void connect() throws IOException {   if (!connected) {     try {       filename = file.toString();       isDirectory = file.isDirectory();       if (isDirectory) {         \/\/...       } else {         is = new BufferedInputStream(new FileInputStream(filename)); \/\/ &lt;--       }     } catch (IOException e) {       throw e;     }     connected = true;   } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0443\u0436\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0447\u0442\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043f\u0435\u0440\u0435\u043b\u0438\u0432\u0430\u043d\u0438\u044e \u0438\u0437 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0432 \u043f\u043e\u0440\u043e\u0436\u043d\u0435\u0435, \u0447\u0442\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code>Benchmark                                 Score     Error   Units  readBufferedFromURL                   11840.985 \u00b1  37.790   ops\/s readBufferedFromURL:\u00b7gc.count          1281.000            counts readBufferedFromURL:\u00b7gc.time            711.000                ms  readFromURL                           12106.742 \u00b1  40.567   ops\/s readFromURL:\u00b7gc.count                   667.000            counts readFromURL:\u00b7gc.time                    435.000                ms<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0440\u043e\u0441\u0448\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0431\u043e\u0440\u043e\u043a \u043c\u0443\u0441\u043e\u0440\u0430 \u0438 \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u043d\u0438\u0445 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u044d\u0442\u043e &#8212; \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043d\u0435\u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0430, \u043a\u043e\u0440\u043d\u044f\u043c\u0438 \u0443\u0445\u043e\u0434\u044f\u0449\u0430\u044f \u0432 1996 \u0433\u043e\u0434 \u0441 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u043c \u0436\u0435\u043b\u0435\u0437\u043e\u043c \u0438 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 JDK \u0440\u0435\u0448\u0438\u043b\u0438 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u0432\u0432\u043e\u0434\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u0412 2021 \u0433\u043e\u0434\u0443 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0440\u0435\u0434\u0438\u0442:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class FileInputStreamBenchmark {    private final String file = \"\/home\/s.tsypanov\/.bashrc\"; \/\/ 4362 \u0431\u0430\u0439\u0442\u0430 \/\/private final String file = \"\/home\/s.tsypanov\/Downloads\/IMAG2549.jpg\";\/\/2,7 \u041c\u0411    @Benchmark   public Object readAllBytes() throws IOException {     try (var fileInputStream = new FileInputStream(file)) {       return fileInputStream.readAllBytes();     }   }    @Benchmark   public Object readAllBytesBuffered() throws IOException {     try (var in = new FileInputStream(file)) {       return new BufferedInputStream(in).readAllBytes();     }   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>InputStream.readAllBytes<\/code>\u0434\u043b\u044f \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430 (\u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u0432 9 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0437 10):<\/p>\n<pre><code>                                            Score    Error   Units read                                          5.9 \u00b1   0.03   us\/op readBuffered                                  7.3 \u00b1   0.01   us\/op  read:\u00b7gc.alloc.rate.norm                  12782.4 \u00b1   3.92    B\/op readBuffered:\u00b7gc.alloc.rate.norm          21030.4 \u00b1   3.92    B\/op<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435, \u043a\u0430\u043a \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f: \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0444\u0430\u0439\u043b\u0430 \u0438\u0437 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442, \u043d\u043e \u0438 \u043c\u0435\u0448\u0430\u0435\u0442. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e (\u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0431\u0443\u0444\u0435\u0440\u0430) \u0444\u0430\u0439\u043b\u0430 (\u0441\u043c. \u0437\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0448\u0435):<\/p>\n<pre><code>                                            Score    Error   Units read                                       1068.1 \u00b1  19.95   us\/op readBuffered                               1068.1 \u00b1  10.99   us\/op  read:\u00b7gc.alloc.rate.norm                5478584.1 \u00b1   0.17   B\/op readBuffered:\u00b7gc.alloc.rate.norm        5486832.1 \u00b1   0.17   B\/op<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0437\u0430\u043c\u0435\u0434\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435: \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440 \u0432\u043d\u0443\u0442\u0440\u0438 <code>BufferedInputStream<\/code>:<\/p>\n<pre><code class=\"java\">private void fill() throws IOException {   byte[] buffer = getBufIfOpen();   if (markpos &lt; 0)     pos = 0;                     \/* no mark: throw away the buffer *\/   else if (pos &gt;= buffer.length) \/* no room left in buffer *\/   \/* ... *\/   count = pos;   int n = getInIfOpen().read(buffer, pos, buffer.length - pos); \/\/ &lt;---   if (n &gt; 0)     count = n + pos; }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>fill()<\/code>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>InputStream.read(byte[], int, int)<\/code>\u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0430\u0447\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0440\u043e\u0432\u043d\u043e \u044d\u0442\u043e \u0436\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0434\u0435\u043b\u0430\u0435\u0442 <code>InputStream.readNBytes(int)<\/code>:<\/p>\n<pre><code class=\"java\">public byte[] readNBytes(int len) throws IOException {   \/\/...      int total = 0;   int remaining = len;   int n;   do {     byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];     int nread = 0;      \/\/ read to EOF which may read more or less than buffer size     while ((n = read(buf, nread,             Math.min(buf.length - nread, remaining))) &gt; 0) {       nread += n;       remaining -= n;     }     \/\/...   }      \/\/...      return result; }<\/code><\/pre>\n<p>\u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u044f\u0432\u043d\u043e\u0439 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 (\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0442\u0435\u043f\u0435\u0440\u044c \u0443\u0436\u0435 \u043d\u0435 \u0432\u044b\u043f\u0438\u043b\u0438\u0442\u044c) \u0441\u0442\u043e\u0438\u043b\u043e \u0431\u044b \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d, \u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 <code>Files.newInputStream(Path)<\/code>:<\/p>\n<pre><code class=\"java\">\/**   * The stream will not be buffered,   * and is not required to support the mark or reset methods.   *\/ public static InputStream newInputStream(Path path, OpenOption... options) {}<\/code><\/pre>\n<p>\u0413\u0440\u0430\u043c\u043e\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0442\u043e\u0436\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435\/\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0432\u0432\u043e\u0434\u0430\/\u0432\u044b\u0432\u043e\u0434\u0430, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0432 <code>org.apache.commons.io.IOUtils<\/code>:<\/p>\n<pre><code class=\"java\">\/**  * All the methods in this class that read a stream are buffered internally.  * This means that there is no cause to use a BufferedInputStream  * or BufferedReader. The default buffer size of 4K has been shown  * to be efficient in tests.  *   * Applications can re-use buffers by using the underlying methods directly.  * This may improve performance for applications that need to do a lot of copying.  *\/ public class IOUtils {     \/**    * This method buffers the input internally, so there is no need    * to use a BufferedInputStream.    *\/   List&lt;String&gt; readLines(InputStream input, Charset encoding) {} }<\/code><\/pre>\n<h4>\u041f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/h4>\n<p>\u0412 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0435\u0441\u0442\u044c 2 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u0430\u044e\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438. <strong>\u041f\u0435\u0440\u0432\u044b\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043d\u0435\u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0438\u043c\u043e \u0434\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u0443 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u0442\u043e\u0440\u043e\u0439 &#8212; \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u043d\u0435\u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0438\u043c\u043e \u0434\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0435\u0451 \u0432\u0440\u0435\u0434. \u041a\u0430\u043a\u043e\u043c\u0443 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0443 \u0432\u0435\u0440\u0438\u0442\u044c?<\/strong> \u041e\u0431\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0430 <strong>\u0434\u043b\u044f \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/strong>.<\/p>\n<p>\u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0430 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430 \u0432\u0440\u0435\u0434\u043d\u0430, \u0441\u043c.<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/openjdk\/jdk\/pull\/2992\/files\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/openjdk\/jdk\/pull\/2992\/files<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8263599\" rel=\"noopener noreferrer nofollow\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8263599<\/a><\/p>\n<\/li>\n<\/ul>\n<h4>\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u044b \u0438\u0437 \u0441\u0430\u043a\u0440\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u043d\u0438\u044f<\/h4>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 <code>o.s.core.type.classreading.SimpleMetadataReader<\/code>,  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u0434\u043b\u044f \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432 &#171;\u0421\u043f\u0440\u0438\u043d\u0433\u0435&#187;:<\/p>\n<pre><code class=\"java\">private static ClassReader getClassReader(Resource resource) throws IOException {   try (InputStream is = new BufferedInputStream(resource.getInputStream())) {     try {       return new ClassReader(is);     }     catch (IllegalArgumentException ex) {       throw new NestedIOException(\"...\" + resource, ex);     }   } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c <code>resource.getInputStream()<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 <code>BufferedInputStream<\/code> \u0438 \u0432\u044b, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0435\u0441\u044c, \u043a\u0443\u0434\u0430 \u0432\u0435\u0442\u0435\u0440 \u0434\u0443\u0435\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/_l\/un\/fk\/_lunfkwxm6am9dmb6fxpkbbhsyy.png\" width=\"955\" height=\"369\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 <code>o.s.asm.ClassReader<\/code>:<\/p>\n<pre><code class=\"java\">static byte[] readStream(InputStream is, boolean close) throws IOException {   if (is == null) {     throw new IOException(\"Class not found\");   }   try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {     byte[] data = new byte[INPUT_STREAM_DATA_CHUNK_SIZE];     int bytesRead;     while ((bytesRead = is.read(data, 0, data.length)) != -1) {       outputStream.write(data, 0, bytesRead);     }     outputStream.flush();     return outputStream.toByteArray();   } finally {     if (close) {       is.close();     }   } }<\/code><\/pre>\n<p>\u0412\u043d\u043e\u0432\u044c \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u043e\u0439\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430, \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class MetadataReaderBenchmark {   final MetadataReaderFactory factory = new SimpleMetadataReaderFactory();    @Benchmark   public Object read() throws IOException {     String className = AnnotatedComponent.class.getName();     return factory       .getMetadataReader(className)       .getAnnotationMetadata();   }    \/\/ \u043f\u043e\u0434\u043e\u043f\u044b\u0442\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442   @Component(\"myComp\")   @Scope(BeanDefinition.SCOPE_PROTOTYPE)   private static class AnnotatedComponent implements Serializable {     private final Dependency dep;      @Autowired     public AnnotatedComponent(@Qualifier(\"myDep\") Dependency dep) {       this.dep = dep;     }      private static class Dependency {     }   } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u0435\u0433\u043e \u0434\u0432\u0430\u0436\u0434\u044b (\u0441 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:<\/p>\n<pre><code>Java 8  original                                         Score     Error   Units read                                  122.041 \u00b1   1.286   us\/op read:\u00b7gc.alloc.rate.norm            50795.798 \u00b1  13.941    B\/op  patched                                         Score     Error   Units read                                  119.524 \u00b1   1.171   us\/op read:\u00b7gc.alloc.rate.norm            42635.578 \u00b1  10.866    B\/op  Java 11  original                                         Score      Error   Units read                                  114.142 \u00b1    3.338   us\/op read:\u00b7gc.alloc.rate.norm            32761.715 \u00b1   29.115    B\/op  patched                                         Score      Error   Units read                                  108.903 \u00b1    1.281   us\/op read:\u00b7gc.alloc.rate.norm            24652.976 \u00b1   32.380    B\/op<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u0440\u043e\u0441\u0442\u043e <a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/pull\/24946\" rel=\"noopener noreferrer nofollow\">\u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0432 \u043d\u0435\u043d\u0443\u0436\u043d\u0443\u044e \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e<\/a> \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0435\u043f\u043b\u043e\u0445\u0443\u044e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044e \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043d\u0438\u0436\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0411\u0443\u0434\u0435\u0442 \u043b\u0438 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0442\u043e\u043b\u043a \u0432 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f? \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0421\u043f\u0440\u0438\u043d\u0433 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <a href=\"https:\/\/github.com\/stsypanov\/spring-boot-benchmark\" rel=\"noopener noreferrer nofollow\">\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u043c<\/a>:<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.SingleShotTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Fork(jvmArgsAppend = {\"-Xms2g\", \"-Xmx2g\"}) public class SpringBootApplicationBenchmark {    private ConfigurableApplicationContext context;    @Benchmark   public Object startUp() {     return context = SpringApplication.run(BenchmarkApplication.class);   }    @TearDown(Level.Invocation)   public void close() {     context.close();   } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0437\u043c\u0435\u0440\u044f\u0435\u043c \u0432\u0440\u0435\u043c\u044f, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0421\u043f\u0440\u0438\u043d\u0433-\u0411\u0443\u0442\u0435:<\/p>\n<pre><code>original  startUp                             760.1 \u00b1    13.5   ms\/op startUp:\u00b7gc.alloc.rate.norm    50429174.6 \u00b1 18132.0    B\/op  patched  startUp                             718.7 \u00b1    20.5   ms\/op startUp:\u00b7gc.alloc.rate.norm    49776228.7 \u00b1 19919.8    B\/op<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u0435\u0442, \u043d\u043e \u0434\u0430\u0436\u0435 \u0442\u0430\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u0451\u0442 \u043f\u0440\u0438\u0440\u043e\u0441\u0442.<\/p>\n<p>\u041a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u044d\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0441\u0447\u0443\u0440 \u0440\u0435\u0437\u043a\u0438\u043c, \u0432\u0435\u0434\u044c \u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 <code>Resource<\/code> \u0435\u0441\u0442\u044c 44 (!) \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ia\/6x\/ji\/ia6xji43cyyqqsi7nhi74jnindy.png\" width=\"731\" height=\"225\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043c \u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0449\u0430\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0430-\u043b\u044f \u041a\u043e\u0442\u043b\u0438\u043d:<\/p>\n<pre><code class=\"kotlin\">\/\/ \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0432\u0432\u043e\u0434\u0430 return File(name).inputStream().buffered();  \/\/ \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f InputStream.buffered() @kotlin.internal.InlineOnly fun InputStream.buffered(size: Int = DEFAULT_BUFFER_SIZE) =   if (this is BufferedInputStream)     this   else     BufferedInputStream(this, size)<\/code><\/pre>\n<p>\u0420\u0430\u0437 \u0432\u0441\u0451 \u0434\u0435\u043b\u043e \u0432 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u043c \u0437\u0430\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0438 \u0432 <code>BufferedInputStream<\/code>, \u0442\u043e \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043d\u0430\u043c \u0441\u043e\u043e\u0440\u0443\u0434\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code class=\"java\">static InputStream buffered(InputStream is) {   return is instanceof BufferedInputStream     ? is     : new BufferedInputStream(is); }<\/code><\/pre>\n<p>\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 <code>SimpleMetadataReader<\/code>-\u0435.<\/p>\n<p>\u041d\u043e \u0438 \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441\u044b, \u0432\u0435\u0434\u044c \u0443 <code>InputStream<\/code>a \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>ByteArrayInputStream<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>BufferedInputStream<\/code>-\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0451\u0440\u043d\u0443\u0442:<\/p>\n<pre><code class=\"java\">public class ByteArrayInputStream extends InputStream {   protected byte buf[];      public synchronized int read() {     return pos &lt; count ? buf[pos++] &amp; 0xff : -1;   }      public synchronized int read(byte b[], int off, int len) {     \/\/...     System.arraycopy(buf, pos, b, off, len);   } }<\/code><\/pre>\n<p>\u0423\u0436\u0435 \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>BufferedInputStream<\/code>-\u0430, \u0447\u0442\u043e \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u043c:<\/p>\n<pre><code class=\"java\">@BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class ByteArrayInputStreamBenchmark {    @Benchmark   public Object read(Data data) {     return data.bais.readAllBytes();   }    @Benchmark   public Object readBuffered(Data data) throws IOException {     return new BufferedInputStream(data.bais).readAllBytes();   }    @State(Scope.Thread)   public static class Data {      @Param({\"8\", \"128\", \"512\", \"1024\"})     private int length;      private byte[] bytes;     private ByteArrayInputStream bais;      @Setup(Level.Iteration)     public void setUp() {       bytes = new byte[length];       ThreadLocalRandom.current().nextBytes(bytes);     }      @Setup(Level.Invocation)     public void setUpBais() {       bais = new ByteArrayInputStream(bytes);     }   } }<\/code><\/pre>\n<p>\u0434\u0430\u044e\u0449\u0438\u043c \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0441\u043d\u043e\u0433\u0441\u0448\u0438\u0431\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code>                                  length read                                   8       35.673 \u00b1  0.591   ns\/op read                                 128       38.245 \u00b1  0.918   ns\/op read                                 512       46.110 \u00b1  1.652   ns\/op read                                1024       60.487 \u00b1  0.687   ns\/op  readBuffered                           8      869.221 \u00b1  3.573   ns\/op readBuffered                         128      876.611 \u00b1 10.665   ns\/op readBuffered                         512      890.678 \u00b1  3.481   ns\/op readBuffered                        1024      914.731 \u00b1  8.759   ns\/op  read:\u00b7gc.alloc.rate.norm               8       56.000 \u00b1  0.001    B\/op read:\u00b7gc.alloc.rate.norm             128      176.000 \u00b1  0.001    B\/op read:\u00b7gc.alloc.rate.norm             512      560.000 \u00b1  0.001    B\/op read:\u00b7gc.alloc.rate.norm            1024     1072.000 \u00b1  0.001    B\/op  readBuffered:\u00b7gc.alloc.rate.norm       8    16512.001 \u00b1  0.001    B\/op readBuffered:\u00b7gc.alloc.rate.norm     128    16632.001 \u00b1  0.001    B\/op readBuffered:\u00b7gc.alloc.rate.norm     512    17016.001 \u00b1  0.001    B\/op readBuffered:\u00b7gc.alloc.rate.norm    1024    17528.001 \u00b1  0.001    B\/op<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0440\u0430\u0441\u0445\u043e\u0434 \u043f\u0430\u043c\u044f\u0442\u0438: 17 \u043a\u0438\u043b\u043e\u0431\u0430\u0439\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f 1. \u0412\u0441\u0451 \u043e\u0447\u0435\u043d\u044c \u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0430\u0437\u0434\u043d\u0438\u0447\u043d\u043e.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u043c \u043d\u0430\u0448\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"java\">public static InputStream buffered(InputStream is) {   return requiresBuffer(is) ? is : new BufferedInputStream(is); } private static boolean requiresBuffer(InputStream is) {   return is instanceof BufferedInputStream       || is instanceof ByteArrayInputStream; }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0439\u0434\u0451\u043c \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412\u043e\u0442 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/um\/zc\/mi\/umzcmin1tsvbhzu_xpn5ac6avku.png\" width=\"1127\" height=\"321\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u043e \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u0434\u043d\u043e\u0439. <\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) public class ResourceLoadBenchmark {    private Resource resource;    @Setup   public void setUp(){     String str = \"classpath:org\/springframework\/boot\/autoconfigure\/orm\"                 + \"\/jpa\/DataSourceInitializedPublisher$Registrar.class\";     DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader();     resource = defaultResourceLoader.getResource(str);   }    @Benchmark   public Object read() throws IOException {     try (InputStream is = resource.getInputStream()) {       return new ClassReader(is);     }   }    @Benchmark   public Object readBuffered() throws IOException {     try (InputStream is = new BufferedInputStream(resource.getInputStream())) {       return new ClassReader(is);     }   } }<\/code><\/pre>\n<pre><code>read                                    23.095 \u00b1  0.548   us\/op readBuffered                            23.044 \u00b1  0.229   us\/op  read:\u00b7gc.alloc.rate.norm             36804.834 \u00b1 31.763    B\/op readBuffered:\u00b7gc.alloc.rate.norm     44993.634 \u00b1 31.439    B\/op<\/code><\/pre>\n<p>\u0421\u043e\u0431\u0440\u0430\u0432 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u0432\u044b\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0442\u043e\u0433\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e:<\/p>\n<pre><code class=\"java\">private static boolean requiresBuffer(InputStream is) {   return is instanceof BufferedInputStream       || is instanceof ByteArrayInputStream       || is instanceof ChannelInputStream       || is instanceof ZipFile.ZipFileInflaterInputStream; }<\/code><\/pre>\n<p>\u0418 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e:<\/p>\n<pre><code class=\"java\">\/\/ \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u0438 \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 private class ZipFileInflaterInputStream extends InflaterInputStream {   \/\/... }<\/code><\/pre>\n<p>\u0417\u043b\u043e\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f, \u0432\u0435\u0434\u044c \u043a\u043e\u0434\u043e\u0432\u0430\u044f \u0431\u0430\u0437\u0430 &#171;\u0421\u043f\u0440\u0438\u043d\u0433\u0430&#187; \u0432\u0441\u0451 \u0435\u0449\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 &#171;\u0432\u043e\u0441\u044c\u043c\u0451\u0440\u043a\u0443&#187;, \u043d\u043e \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043d\u0430\u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u0435\u043d\u044c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0435\u0449\u0451 \u043e\u0434\u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0430:<\/p>\n<pre><code>error: package sun.nio.ch is not visible import sun.nio.ch.ChannelInputStream;               ^ (package sun.nio.ch is declared in module java.base, which does not export it to the unnamed module)<\/code><\/pre>\n<p>\u0412 &#171;\u0434\u0435\u0432\u044f\u0442\u043a\u0435&#187; \u043f\u0430\u043a\u0435\u0442 sun.nio.ch \u043d\u0435 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c java.base, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u0436\u0438\u043b\u0438 \u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0438\u0440\u0435, \u0442\u043e InputStream \u0431\u044b\u043b \u0431\u044b \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"java\">public interface InputStream extends Closeable {    default boolean isBuffered() {     return false;   } }<\/code><\/pre>\n<p>\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">public static InputStream buffered(InputStream is) {   return is.isBuffered() ? is : new BufferedInputStream(is); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0435\u0440\u043d\u0451\u043c\u0441\u044f \u043a org.springframework.asm.ClassReader, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0436\u0430\u0442\u044c \u0435\u0449\u0451 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c.<\/p>\n<pre><code class=\"java\">static byte[] readStream(InputStream is, boolean close) throws IOException {   try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {     byte[] data = new byte[INPUT_STREAM_DATA_CHUNK_SIZE];     int bytesRead;     while ((bytesRead = is.read(data, 0, data.length)) != -1) {       outputStream.write(data, 0, bytesRead);     }     outputStream.flush();     return outputStream.toByteArray();   } finally {     if (close) {       is.close();     }   } }<\/code><\/pre>\n<p>\u0411\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0431\u0443\u0444\u0435\u0440\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bv\/z_\/mc\/bvz_mcqhyk16s6t_jdd1-chbtx0.png\" width=\"1172\" height=\"635\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f 421 \u0431\u0430\u0439\u0442\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043e 4 \u043a\u0411 &#8212; \u0432 10 \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e. \u0422\u0430\u043a\u0436\u0435 \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u0435\u0442\u043e\u0434 <code>InputStream.read(byte[], int, int)<\/code>\u0441\u043c\u043e\u0433 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432 \u0431\u0443\u0444\u0435\u0440 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u0432\u044b\u0437\u043e\u0432 <code>OutputStream.toByteArray()<\/code>\u0441\u0442\u0430\u043b \u0431\u044b \u043d\u0435\u043d\u0443\u0436\u0435\u043d, \u0432\u0435\u0434\u044c \u043c\u0430\u0441\u0441\u0438\u0432 data \u0443\u0436\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430 \u0438\u0437\u0431\u0435\u0436\u0430\u0432 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>\u0417\u0430\u0439\u0434\u0451\u043c \u0432 \u043b\u043e\u0431 \u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <code>InputStream.available()<\/code>:<\/p>\n<pre><code class=\"java\">int expectedLength = inputStream.available();  try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {   byte[] data = new byte[expectedLength];   int bytesRead;   while ((bytesRead = inputStream.read(data, 0, available)) != -1) {     os.write(data, 0, bytesRead);   }   os.flush();   return expectedLength == os.size() ? data : os.toByteArray(); }<\/code><\/pre>\n<p> \u0418 \u0442\u0443\u0442 \u0436\u0435 \u043e\u0431\u043b\u043e\u043c:<\/p>\n<pre><code class=\"java\">\/**  * ... A single read or skip of this many bytes will not block, but  * may read or skip fewer bytes.  * ...  * It is never correct to use the return value of this method  * to allocate a buffer intended to hold all data in this stream.  **\/ public int available() throws IOException { }<\/code><\/pre>\n<p>\u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>expectedLength == os.size()<\/code>, \u0442. \u043a.  \u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d \u0441\u043b\u0443\u0447\u0430\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c <code>InputStream.available()<\/code>\u0432\u0435\u0440\u043d\u0451\u0442 3 \u043a\u0411, \u043d\u043e \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e 2, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043c\u044b \u043f\u043e\u0439\u0434\u0451\u043c \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 \u043f\u043e \u0446\u0438\u043a\u043b\u0443. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 <code>expectedLength == os.size()<\/code>\u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e, \u043d\u043e data \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e 1 \u043a\u0411 \u0438\u0437 3, \u0447\u0442\u043e \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u0441\u044f \u043f\u0430\u0434\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<\/p>\n<p>\u0427\u0442\u043e \u0436, \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043a\u043e\u0434 \u0447\u0442\u043e\u0431\u044b \u044f\u0432\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0442\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"java\">int expectedLength = inputStream.available(); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {   byte[] data = new byte[expectedLength];   int bytesRead;   int readCount = 0;   while ((bytesRead = inputStream.read(data, 0, bufferSize)) != -1) {     os.write(data, 0, bytesRead);     readCount++;   }   os.flush();   return readCount == 1 ? data : os.toByteArray(); }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043d\u043e \u043e\u043d \u0432\u0441\u0451 \u0435\u0449\u0451 \u0442\u0430\u0438\u0442 \u0441\u043a\u0440\u044b\u0442\u0443\u044e \u0443\u0433\u0440\u043e\u0437\u0443:<\/p>\n<pre><code class=\"java\">var file = new File(\"\/home\/s.tsypanov\/.bashrc\"); var fileInputStream = new FileInputStream(file); var available = fileInputStream.available();             \/\/ 4362 \u0431\u0430\u0439\u0442\u0430 var arrayLength = fileInputStream.readAllBytes().length; \/\/ 4362 \u0431\u0430\u0439\u0442\u0430  var file = new File(\"\/proc\/self\/stat\"); var fileInputStream = new FileInputStream(file); var available = fileInputStream.available();             \/\/ 0   \u0431\u0430\u0439\u0442 var arrayLength = fileInputStream.readAllBytes().length; \/\/ 324 \u0431\u0430\u0439\u0442\u0430<\/code><\/pre>\n<p>\u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u041b\u0438\u043d\u0443\u043a\u0441 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0442\u0432\u043e\u0440\u044f\u044e\u0442\u0441\u044f \u043f\u0443\u0441\u0442\u044b\u043c\u0438, \u043d\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0434\u0430\u043d\u043d\u044b\u0435. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 classpath \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d \u0432 <code>\/proc<\/code>, \u0442\u043e \u0432\u044b \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u0430\u0445\u0443\u0435\u043c\u0441\u044f:<\/p>\n<pre><code class=\"java\">int expectedLength = inputStream.available();  int bufferSize = expectedLength &lt; 256   ? INPUT_STREAM_DATA_CHUNK_SIZE   : expectedLength;  \/\/ ... byte[] data = new byte[bufferSize];<\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u043d\u043e\u0441\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u0443!<\/p>\n<pre><code>original  startUp                             760.1 \u00b1    13.5   ms\/op startUp:\u00b7gc.alloc.rate.norm    50429174.6 \u00b1 18132.0    B\/op  no BufferedInputStream  startUp                             718.7 \u00b1    20.5   ms\/op startUp:\u00b7gc.alloc.rate.norm    49776228.7 \u00b1 19919.8    B\/op  \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u043d\u044b\u0439 ClassReader.readStream()  startUp                             614.8 \u00b1     7.0   ms\/op  &lt;--- ! startUp:\u00b7gc.alloc.rate.norm    49003403.9 \u00b1 21786.8    B\/op<\/code><\/pre>\n<p>\u041e\u043a\u0440\u044b\u043b\u0451\u043d\u043d\u044b\u0439 \u0443\u0441\u043f\u0435\u0445\u043e\u043c \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u041f\u0420 <a href=\"https:\/\/github.com\/spring-projects\/spring-framework\/pull\/26665\" rel=\"noopener noreferrer nofollow\">Improve performance of ASM&#8217;s ClassReader.readStream()<\/a> , \u0437\u0430\u0431\u044b\u0432, \u0447\u0442\u043e <code>ClassReader<\/code> \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a ASM, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435 \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u041f\u0420 \u0442\u0443\u0434\u0430, \u0447\u0442\u043e \u044f \u0438 <a href=\"https:\/\/gitlab.ow2.org\/asm\/asm\/-\/merge_requests\/314\" rel=\"noopener noreferrer nofollow\">\u0441\u0434\u0435\u043b\u0430\u043b<\/a>. \u041e\u0434\u043d\u0430\u043a\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c ASM-\u0430 \u0438\u0434\u0435\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043c\u0435\u043b\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043b\u0435\u0436\u0430\u0442 \u0431\u0435\u0437 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u041d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e ASM &#8212; \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043c\u043d\u043e\u0433\u0438\u0435 \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u0443\u0442\u0451\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432 \u0432 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 (\u043a\u0440\u043e\u043c\u0435 \u0421\u043f\u0440\u0438\u043d\u0433\u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0432 JDK).<\/p>\n<p>\u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043f\u043e\u0445\u043e\u0436\u0438\u0439 \u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438 \u0432 \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<h4>\u0412\u044b\u0432\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/h4>\n<ul>\n<li>\n<p>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430 &#8212; \u044d\u0442\u043e &#171;\u0441\u0442\u0440\u0430\u043d\u0430, \u0433\u0434\u0435 \u043c\u043d\u043e\u0433\u043e-\u043c\u043d\u043e\u0433\u043e \u0434\u0438\u043a\u0438\u0445 \u043e\u0431\u0435\u0437\u044c\u044f\u043d&#187; \u0438 \u0432\u0441\u0451 \u0432 \u043d\u0435\u0439 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u0432\u043e\u0439 \u0441\u0442\u0440\u0430\u0445 \u0438 \u0440\u0438\u0441\u043a<\/p>\n<\/li>\n<li>\n<p>\u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0440\u0435\u0446\u0435\u043f\u0442\u043e\u0432 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0434\u043e\u0433\u043c\u043e\u0439, \u0434\u0430\u0436\u0435 \u0442\u0430\u043a\u0430\u044f \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0432\u0435\u0449\u044c, \u043a\u0430\u043a \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u0432\u0440\u0435\u0434\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>\u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0439\u0442\u0435 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0432\u0430\u0448\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u0435\u0451 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f. \u0414\u0430, \u044d\u0442\u043e \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043e\u043d\u0438 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u044b <\/p>\n<\/li>\n<\/ul>\n<h3>\u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b<\/h3>\n<ul>\n<li>\n<p>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u0442\u043e\u043b\u043a\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u0444\u0440\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0441\u0442\u043e\u043b\u044c \u0436\u0435 \u0432\u0430\u0436\u043d\u043e, \u043a\u0430\u043a \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/p>\n<\/li>\n<li>\n<p>\u0431\u0435\u0437 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0440\u043e\u0434\u044b \u0446\u0438\u0444\u0440 \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u043e\u043d\u0438 \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043d\u043e\u0433\u043e \u043c\u043e\u0436\u0435\u0442 \u0443\u0445\u0443\u0434\u0448\u0438\u0442\u044c \u0446\u0435\u043b\u043e\u0435<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0434 \u043a\u043e\u043f\u0438\u0440\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0432\u043d\u0438\u043a\u0430\u0439\u0442\u0435 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u2013 \u044d\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e \u0430\u0440\u0445\u0435\u043e\u043b\u043e\u0433\u0438\u044e<\/p>\n<\/li>\n<li>\n<p>\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u0443\u0447\u0430\u0439\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435 \u0434\u043e\u0432\u0435\u0440\u044f\u0439\u0442\u0435 \u0441\u043b\u0435\u043f\u0430 \u043a\u0440\u0430\u0441\u043e\u0442\u0430\u043c\/\u0443\u0436\u0430\u0441\u0430\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0417.\u042b. \u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u0434\u043b\u044f IO \u043b\u0435\u0436\u0430\u0442 <a href=\"https:\/\/github.com\/stsypanov\/ovn\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/div>\n<p> \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\/518744\/\"> https:\/\/habr.com\/ru\/post\/518744\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e, \u044d\u0442\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043c\u043e\u0435\u0433\u043e \u0434\u043e\u043a\u043b\u0430\u0434\u0430 \u043d\u0430 <a href=\"https:\/\/jpoint.ru\/2021\/talks\/1ui7ta2itmzurm0vaqmyjw\/\" rel=\"noopener noreferrer nofollow\">JPoint-2021<\/a>. \u041a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0443\u043f\u043e\u0440 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0438 \u0438\u0437 \u0436\u0438\u0437\u043d\u0438 \u0438 \u043d\u0430 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u044b\u0435 \u0432\u0435\u0449\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u043a\u0430\u0436\u0434\u044b\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c. \u041f\u043e\u0434 \u043a\u0430\u0442 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e\u0442\u0441\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u0438\u043c \u0441\u043e\u0447\u0443\u0432\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043d\u044e\u0430\u043d\u0441?<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u0438\u0442\u044c <a href=\"https:\/\/slovar.cc\/enc\/bolshoy\/2105945.html\" rel=\"noopener noreferrer nofollow\">\u0411\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u044d\u043d\u0446\u0438\u043a\u043b\u043e\u043f\u0435\u0434\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0430\u0440\u044e<\/a>, \u0442\u043e \u043d\u044e\u0430\u043d\u0441 (\u0444\u0440\u0430\u043d\u0446. nuance) &#8212; \u044d\u0442\u043e \u043e\u0442\u0442\u0435\u043d\u043e\u043a, \u0442\u043e\u043d\u043a\u043e\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435, \u0435\u0434\u0432\u0430 \u0437\u0430\u043c\u0435\u0442\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 (\u0432 \u0446\u0432\u0435\u0442\u0435, \u043c\u044b\u0441\u043b\u0438, \u0437\u0432\u0443\u043a\u0435 \u0438 \u0442. \u043f.). \u041e\u0447\u0435\u043d\u044c \u043f\u043e\u0448\u043b\u043e\u0435 (\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0435\u043c \u043e\u0447\u0435\u043d\u044c \u0432\u0435\u0440\u043d\u043e\u0435) \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043e \u0432 \u0432\u0438\u0434\u0435 \u0430\u043d\u0435\u043a\u0434\u043e\u0442\u0430 <a href=\"https:\/\/www.anekdot.ru\/id\/101180\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h3>\u041e\u0442\u043a\u0443\u0434\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u043d\u044e\u0430\u043d\u0441\u044b?<\/h3>\n<p>\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e: \u043f\u043e\u0447\u0435\u043c\u0443 \u043e\u0442\u043d\u044e\u0434\u044c \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0433\u043b\u044f\u0434\u044f \u043d\u0430 \u043a\u043e\u0434 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u043e \u0442\u043e\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c? \u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0442\u0432\u0435\u0442: \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e Java \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u0430. \u0420\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u044b\u0439 \u043e\u0442\u0432\u0435\u0442: \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u043e\u0434\u0435\u043b\u044b\u0432\u0430\u044f \u043f\u0443\u0442\u044c \u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0434\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0432\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0442\u0435\u0440\u043f\u0435\u0442\u044c \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0435\u0451 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e. \u0412 \u043e\u0431\u0449\u0438\u0445 \u0447\u0435\u0440\u0442\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<ul>\n<li>\n<p>\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434<\/p>\n<\/li>\n<li>\n<p>\u0431\u0430\u0439\u0442-\u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u043e\u0439<\/p>\n<\/li>\n<li>\n<p>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 (\u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439) \u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043e\u0434 \u043d\u0430 \u043b\u0435\u0442\u0443 \u043f\u043e \u043c\u0435\u0440\u0435 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u044e\u0430\u043d\u0441\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u044d\u0442\u0430\u043f\u043e\u0432, \u0433\u0434\u0435-\u0442\u043e \u043e\u043d\u0438 \u0431\u043e\u043b\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b, \u0433\u0434\u0435-\u0442\u043e \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b \u0432\u043e\u0432\u0441\u0435.<\/p>\n<h3>\u041a\u0430\u043a\u0438\u0435 \u0431\u044b\u0432\u0430\u044e\u0442 \u043d\u044e\u0430\u043d\u0441\u044b?<\/h3>\n<ul>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438\/\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043d\u044e\u0430\u043d\u0441\u044b \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u0441 \u043a\u043e\u043d\u0446\u0430.<\/p>\n<h3>\u041d\u044e\u0430\u043d\u0441\u044b \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u0447\u0442\u043e \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0434\u0430\u0451\u0442 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f <em>\u043b\u043e\u0436\u043d\u044b\u0439<\/em> \u0438, \u043f\u043e \u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e, <strong>\u043e\u043f\u0430\u0441\u043d\u044b\u0439<\/strong> \u0432\u044b\u0432\u043e\u0434: \u0435\u0441\u043b\u0438 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0442\u043e \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043d\u0435\u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e <em>\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439<\/em> \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u0438 \u0442\u0435\u0445 \u0438\u043b\u0438 \u0438\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043a\u043e\u0434\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a\u043e\u0439 \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"java\">byte[] bunn;   for (int i = 0; i &lt; bunn.length; i++) { \/\/ \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \"\u043f\u043e \u0441\u0442\u0430\u0440\u043e\u043c\u0443 \u0441\u0442\u0438\u043b\u044e\"   use(bunn[i]); }  for (byte bunny : bunn) {               \/\/ \u043f\u043e\u0434\u0441\u043b\u0430\u0441\u0442\u0438\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441   use(bunny); }<\/code><\/pre>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0434\u0435\u043b\u0430\u044e\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e <code>for-each<\/code>\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u0441\u0443\u0442\u0438 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0430\u0445\u0430\u0440\u043e\u043c \u0434\u043b\u044f <code>for-i<\/code>. \u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441 \ud83d\ude42<\/p>\n<pre><code class=\"java\">@State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class LoopyBenchmarks {   @Param({ \"32\", \"1024\", \"32768\" })   int size;    byte[] bunn;    @Setup   public void prepare() {     bunn = new byte[size];   }    @Benchmark   public void goodOldLoop(Blackhole fox) {     for (int y = 0; y &lt; bunn.length; y++) {       fox.consume(bunn[y]);     }   }    @Benchmark   public void sweetLoop(Blackhole fox) {     for (byte bunny : bunn) {       fox.consume(bunny);     }   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043c\u043e\u043f\u0435\u0434 \u043d\u0435 \u043c\u043e\u0439, \u043d\u0438\u0436\u0435 \u044f \u0434\u0430\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0430, \u0430 \u043f\u043e\u043a\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u043e\u0433\u043e\u043d:<\/p>\n<pre><code>Benchmark              (size)     Score        Error  Units  goodOldLoop               32     46.630        0.097  ns\/op goodOldLoop             1024   1199.338        0.705  ns\/op goodOldLoop            32768  37813.600       56.081  ns\/op  sweetLoop                 32     19.304        0.010  ns\/op sweetLoop               1024    475.141        1.227  ns\/op sweetLoop              32768  14295.800       36.071  ns\/op<\/code><\/pre>\n<p>\u0412\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u0435\u0442, \u043f\u0440\u0430\u0432\u0434\u0430?<\/p>\n<blockquote>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430 \u0434\u0430\u043b\u043e \u043f\u043e\u0447\u0442\u0438 \u0434\u0432\u0443\u043a\u0440\u0430\u0442\u043d\u044b\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 \u043d\u0430 \u043f\u0443\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435! \u041d\u0443\u0436\u043d\u043e \u0441\u0440\u043e\u0447\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430!<\/p>\n<\/blockquote>\n<p>\u0422\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438 \u044d\u043d\u0442\u0443\u0437\u0438\u0430\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 (\u0442\u0430\u043a \u043a\u043e\u0433\u0434\u0430-\u0442\u043e \u0434\u0443\u043c\u0430\u043b \u0438 \u044f). \u0411\u043e\u043b\u0435\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u043d\u0435 \u0442\u043e\u0440\u043e\u043f\u044f\u0442\u0441\u044f \u0441 \u0432\u044b\u0432\u043e\u0434\u0430\u043c\u0438, \u0432\u0435\u0434\u044c \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0443 \u0440\u043e\u0437\u043d\u044c. \u0411\u044b\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0432 \u043d\u0430\u0448\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043a\u0440\u0430\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043e\u0434\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430? \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e \u0434\u043b\u044f \u043d\u0430\u0441, \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 JMH \u043e\u043f\u0438\u0441\u0430\u043b\u0438 <a href=\"https:\/\/hg.openjdk.java.net\/code-tools\/jmh\/file\/tip\/jmh-samples\/src\/main\/java\/org\/openjdk\/jmh\/samples\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0436\u0438\u0437\u043d\u0438<\/a>, \u0441\u0440\u0435\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0445\u043e\u0434\u0438\u043c <a href=\"https:\/\/hg.openjdk.java.net\/code-tools\/jmh\/file\/523c524305e2\/jmh-samples\/src\/main\/java\/org\/openjdk\/jmh\/samples\/JMHSample_34_SafeLooping.java#l144\" rel=\"noopener noreferrer nofollow\">\u043d\u0443\u0436\u043d\u044b\u0439<\/a>:<\/p>\n<pre><code class=\"java\">@Fork(3) @State(Scope.Thread) @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class JMHSample_34_SafeLooping {   @Param({\"1\", \"10\", \"100\", \"1000\"})   int size;    int[] xs;    @Setup   public void setup() {     xs = new int[size];     for (int c = 0; c &lt; size; c++) {       xs[c] = c;     }   }    @Benchmark   public void measureRight_1(Blackhole bh) {     for (int x : xs) {       bh.consume(work(x));     }   }      static final int BASE = 42;    static int work(int x) {     return BASE + x;   } }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043a\u0430\u043a \u0432 \u043b\u0443\u0447\u0448\u0438\u0445 \u0434\u043e\u043c\u0430\u0445 \u041f\u043e\u0442\u0441\u0434\u0430\u043c\u0430 \u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u0432\u0435\u0440\u043d\u044b, \u043d\u043e \u0438\u0437 \u043d\u0438\u0445 \u0441\u0434\u0435\u043b\u0430\u043d \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434. \u0410 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0441\u0434\u0435\u043b\u0430\u043b \u041d\u0438\u0446\u0430\u043d \u0412\u0430\u043a\u0430\u0440\u0442 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"http:\/\/psy-lob-saw.blogspot.com\/2014\/08\/the-volatile-read-suprise.html\" rel=\"noopener noreferrer nofollow\">The volatile read surprise<\/a>:<\/p>\n<pre><code class=\"java\">\/\/ \u0441\u0442\u0430\u0440\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 org.openjdk.jmh.infra.Blackhole (\u0434\u043e JMH 1.3)  public volatile byte b1, b2; public volatile BlackholeL2 nullBait = null;  \/**  * Consume object. This call provides a side effect  * preventing JIT to eliminate dependent computations.  *  * @param b object to consume.  *\/ public final void consume(byte b) {   if (b == b1 &amp; b == b2) {     \/\/ SHOULD NEVER HAPPEN     nullBait.b1 = b; \/\/ implicit null pointer exception   } }<\/code><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 <code>Blackhole::consume<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0434\u0432\u0430 \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u044d\u0444\u0444\u0435\u043a\u0442 \u0447\u0451\u0440\u043d\u043e\u0433\u043e \u044f\u0449\u0438\u043a\u0430, \u043d\u0435 \u0434\u0430\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 <code>b<\/code>, \u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u043e\u043b\u044e <code>bunn<\/code> \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0441\u0442\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u043d\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0438 \u043f\u0440\u043e\u0445\u043e\u0434 \u043f\u043e \u043d\u0435\u0439 \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438:<\/p>\n<pre><code class=\"java\">@Benchmark public void goodOldLoopReturns(Blackhole fox) {   byte[] sunn = bunn; \/\/ make a local copy of the field   for (int y = 0; y &lt; sunn.length; y++) {     fox.consume(sunn[y]);   } }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>goodOldLoopReturns<\/code>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435 \u0431\u044b\u0441\u0442\u0440\u043e, \u043a\u0430\u043a \u0438 <code>for-each<\/code><\/p>\n<pre><code>Benchmark              (size)     Score        Error  Units  goodOldLoopReturns        32     19.306        0.045  ns\/op goodOldLoopReturns      1024    476.493        1.190  ns\/op goodOldLoopReturns     32768  14292.286       16.046  ns\/op  sweetLoop                 32     19.304        0.010  ns\/op sweetLoop               1024    475.141        1.227  ns\/op sweetLoop              32768  14295.800       36.071  ns\/op<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, <code>for-each<\/code> \u0432\u044b\u0438\u0433\u0440\u044b\u0432\u0430\u043b \u043b\u0438\u0448\u044c \u0438\u0437-\u0437\u0430 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e\u043b\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e:<\/p>\n<pre><code class=\"java\">@Benchmark public void sweetLoop(Blackhole fox) {   for (byte bunny : bunn) {     fox.consume(bunny);   } }<\/code><\/pre>\n<p>\u043f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432<\/p>\n<pre><code class=\"java\">@Benchmark public void sweetLoop(Blackhole fox) {   byte[] var2 = this.bunn;                  \/\/ &lt;---   int var3 = var2.length;    for(int var4 = 0; var4 &lt; var3; ++var4) {     byte bunny = var2[var4];     fox.consume(bunny);   } }<\/code><\/pre>\n<p>\u041c\u043e\u0440\u0430\u043b\u044c \u0441\u0435\u0439 \u0431\u0430\u0441\u043d\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0438 \u043a \u0432\u044b\u0432\u043e\u0434\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430:<\/p>\n<blockquote>\n<p>REMEMBER: <strong>The numbers below are just data.<\/strong> To gain reusable insights, you need to follow up on why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial experiments, perform baseline and negative tests that provide experimental control, make sure the benchmarking environment is safe on JVM\/OS\/HW level, ask for reviews from the domain experts. <strong>Do not assume the numbers tell you what you want them to tell.<\/strong><\/p>\n<\/blockquote>\n<p>\u041d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0440\u043e\u0434\u043d\u044b\u0445 \u043e\u0441\u0438\u043d \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e <strong>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0442\u043e\u043b\u043a\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u044c \u0436\u0435 \u0432\u0430\u0436\u043d\u043e, \u043a\u0430\u043a \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430<\/strong>. \u0426\u0438\u0444\u0440\u044b \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u043d\u0438\u0447\u0442\u043e, \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 &#8212; \u0432\u0441\u0451.<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435 \u043a Blackhole <\/summary>\n<div class=\"spoiler__content\">\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 1.3 \u0438 \u0434\u043e 1.28 \u043a\u043b\u0430\u0441\u0441 Blackhole \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u0434\u043d\u043e\u0435 \u0432\u043e\u0437\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430:<\/p>\n<pre><code class=\"java\">public volatile byte b1;  \/\/ \u0442\u043e\u043b\u044c\u043a\u043e b1 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u043a\u0430\u043a volatile public byte b2;  public final void consume(byte b) {   byte b1 = this.b1;   byte b2 = this.b2;   if (b == b1 &amp; b == b2) {     nullBait.b1 = b;   } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u0442\u0430\u043b\u0430 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0441\u043a\u0440\u043e\u043c\u043d\u0435\u0435:<\/p>\n<pre><code>                    (size)      Score      Error  Units goodOldLoop             32     70.307 \u00b1    0.752  ns\/op goodOldLoop           1024   2239.560 \u00b1   21.611  ns\/op goodOldLoop          32768  70890.224 \u00b1 3475.436  ns\/op  sweetLoop               32    103.163 \u00b1   39.564  ns\/op sweetLoop             1024   1812.092 \u00b1    4.234  ns\/op sweetLoop            32768  57918.608 \u00b1  249.921  ns\/op  goodOldLoopReturns      32     59.757 \u00b1    0.748  ns\/op goodOldLoopReturns    1024   1843.383 \u00b1   37.246  ns\/op goodOldLoopReturns   32768  57704.539 \u00b1  217.073  ns\/op<\/code><\/pre>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u044d\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u043f\u0440\u0435\u0434\u0435\u043b (\u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0435 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u0431\u044b\u0442\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e). \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 1.29 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0451\u0440\u043d\u043e\u0433\u043e \u044f\u0449\u0438\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043d\u044b\u0445 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"java\">private static final boolean COMPILER_BLACKHOLE = Boolean.getBoolean(\"compilerBlackholesEnabled\");  public final void consume(byte b) {   if (COMPILER_BLACKHOLE) {     consumeCompiler(b);   } else {     consumeFull(b); \/\/ \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e   } }  \/\/ Compiler blackholes block: let compilers figure out how to deal with it. private static void consumeCompiler(byte v) {}  \/\/ Full blackholes block: confuse compilers to get blackholing effects. \/\/ See implementation comments at the top to understand what this code is doing. private void consumeFull(byte b) {     byte b1 = this.b1; \/\/ volatile read     byte b2 = this.b2;     if ((b ^ b1) == (b ^ b2)) {         nullBait.b1 = b; \/\/ implicit null pointer exception     } }<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041a\u0440\u0438\u0442\u0438\u043a \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0441\u043a\u0430\u0436\u0435\u0442: &#171;\u041d\u043e \u044d\u0442\u043e \u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440!&#187;. \u041d\u0430 \u0434\u0435\u043b\u0435, \u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0443\u0436 \u0438 \u0432\u044b\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439:<\/p>\n<pre><code class=\"java\">byte[] bunn;  @Benchmark public void goodOldLoop(Blackhole fox) {   for (int y = 0; y &lt; bunn.length; y++) {     fox.consume(bunn[y]);   } }  \/\/-------------------  E[] elements;  @Override public void forEach(Consumer&lt;E&gt; consumer) {   for (int i = 0; i &lt; elements.length; i++) {     consumer.accept(elements[i]);   } }<\/code><\/pre>\n<p>\u0414\u0430-\u0434\u0430, \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 \u043a\u0430\u043a \u0434\u0432\u0435 \u043a\u0430\u043f\u043b\u0438 \u0432\u043e\u0434\u044b \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>Iterable.forEach(Consumer)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0441\u0442\u044c \u0443 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0412\u044b\u0448\u0435 \u043c\u044b \u0443\u0431\u0435\u0434\u0438\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u0432 99 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0437 100 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u043d\u043e \u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043e\u0442\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0432\u0441\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043b\u0438\u0431\u043e <code>for-each<\/code>, \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u044f\u0432\u043d\u0443\u044e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 \u0441\u0442\u0435\u043a\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>j.u.ArrayList<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">@Override public void forEach(Consumer&lt;? super E&gt; action) {   Objects.requireNonNull(action);   final int expectedModCount = modCount;   final Object[] es = elementData;   final int size = this.size;   for (int i = 0; modCount ==<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\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-321720","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/321720","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=321720"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/321720\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}