{"id":739,"date":"2018-01-26T11:13:31","date_gmt":"2018-01-26T09:13:31","guid":{"rendered":"http:\/\/www.loicmathieu.fr\/wordpress\/?p=739"},"modified":"2018-01-26T11:13:31","modified_gmt":"2018-01-26T09:13:31","slug":"les-optimisations-de-performances-de-java-9","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/les-optimisations-de-performances-de-java-9\/","title":{"rendered":"Java 9 performance optimizations"},"content":{"rendered":"<p>In a previous article on Java 9, I listed all the main new features for the developers : <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\"><a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\">http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs<\/a><\/a>.<\/p>\n<p>Here, I will list all the main performance optimizations of Java 9.<\/p>\n<p>I will again go through the main JEP  :<\/p>\n<h2>JEP 143: Improve Contended Locking<\/h2>\n<p>Optimization of Java monitors (lock optimization) when contended (when multiple threads access the same monitor). These are changes inside the JVM that allow siginficant (x2) performance boost on some JVM operations on lock primitives on objects (synchronize, wait, &#8230;).<\/p>\n<p>More information in the following presentation by Monica Beckwith :<\/p>\n<p><a href=\"https:\/\/www.slideshare.net\/MonicaBeckwith\/jfokus-java-9-contended-locking-performance\"><a href=\"https:\/\/www.slideshare.net\/MonicaBeckwith\/jfokus-java-9-contended-locking-performance\">https:\/\/www.slideshare.net\/MonicaBeckwith\/jfokus-java-9-contended-locking-performance<\/a><\/a><\/p>\n<h2>JEP 193: Variable Handles<\/h2>\n<p>A new API in Java, targeting advanced users, allowing to <strong>replace the usage of Unsafe<\/strong> for atomically accessing in a performant way variables.<\/p>\n<blockquote>A variable handle is a typed reference to a variable, which supports read and write access to the variable under a variety of access modes. Supported variable kinds include instance fields, static fields and array elements.<\/blockquote>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/193\"><a href=\"http:\/\/openjdk.java.net\/jeps\/193\">http:\/\/openjdk.java.net\/jeps\/193<\/a><\/a><\/p>\n<h2>JEP 197: Segmented Code Cache<\/h2>\n<p>The code cache (a part of the Metaspace) has been separated by code types (method, non-method, profiled, non-profiled) for performance optimization and allowing future optimizations.<\/p>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/197\"><a href=\"http:\/\/openjdk.java.net\/jeps\/197\">http:\/\/openjdk.java.net\/jeps\/197<\/a><\/a><\/p>\n<h2>JEP 254: Compact Strings<\/h2>\n<p>That&#8217;s one of the main JEP around performance!<\/p>\n<p>The java String implementation was reworked to allow a more compact version.<\/p>\n<p>Until now, Strings was stored in UTF-16, each character took 2 bytes as stored in an array of char.<\/p>\n<p>With Compact String, at the String construction time, if the String is ISO-8859-1 (or Latin-1) compatible, each character will then be stored in only one byte, the String storage evolves from an array of char to an array of bytes. The potential gain is huge : almost 50% shrinking of the size needed to store Strings. Strings are usually the main source of memory usage and generally they are ISO-8859-1 compatible.<\/p>\n<p>If the String is not ISO-8859-1 compatible, then characters are still stored in a bytes array, but you will need two bytes for each one. A flag was added to the String class to know the encoding of this one.<\/p>\n<p>This change has been made without any changes on the public API of Java : by using Java 9, you directly use Compact Strings! StringBuilder\/Buffer was also updated to take advantage of it.<\/p>\n<p>During preliminary tests for the implementation of this JEP, the following gains have been seen (see <a href=\"http:\/\/cr.openjdk.java.net\/~shade\/density\/state-of-string-density-v1.tx\"><a href=\"http:\/\/cr.openjdk.java.net\/~shade\/density\/state-of-string-density-v1.tx\">http:\/\/cr.openjdk.java.net\/~shade\/density\/state-of-string-density-v1.tx<\/a><\/a>t) :<\/p>\n<ul><li>Gain of 5% to 15% of the size of the heap (25% of objects are String)<\/li>\n\n<li>Performance gain of 20% for Latin-1 compared to UTF-16 (30% less garbage) : microbenchmark of String instanciation\/concatenation<\/li>\n<\/ul>\n<p>Moreover, this change open the door to some more optimizations in the code of Java and the JVM (like String encoding\/decoding).<\/p>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/254\"><a href=\"http:\/\/openjdk.java.net\/jeps\/254\">http:\/\/openjdk.java.net\/jeps\/254<\/a><\/a><\/p>\n<h2>JEP 274: Enhanced Method Handles<\/h2>\n<p>Multiple addons to the MethodHandle API that allows to create new compiler optimizations.<\/p>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/274\"><a href=\"http:\/\/openjdk.java.net\/jeps\/274\">http:\/\/openjdk.java.net\/jeps\/274<\/a><\/a><\/p>\n<h2>JEP 280: Indify String Concatenation<\/h2>\n<p>That&#8217;s one of the main JEP around performance!<\/p>\n<p>After the String compaction, introducing the String optimized concatenation!<\/p>\n<p>First, some explanations.<\/p>\n<p>When we concatenate strings in Java, we create a lot of ephemeral objects, that will then be collected by the Garbage Collector (GC): <\/p>\n<p><code>String s = &quot;toto&quot; + &quot;tata&quot; + &quot;titi&quot; + &quot;tutu&quot;<\/code><\/p>\n<p>This leads to the creation of 6 strings as strings are immutable in Java : &#8220;toto&#8221;, &#8220;tata&#8221;, &#8220;titi&#8221;,&#8221;tototata&#8221;, &#8220;tototatatiti&#8221;, &#8220;tototatatititutu&#8221; &#8230; and in the end we will only keep <code>s<\/code> and the other objects will be cleaned by the GC.<\/p>\n<p>Since java 6, the Java compiler (javac) try to optimize this by replacing, at compile time, these concatenations by the use of StringBuilder. It replaces the previous code by<\/p>\n<p><code>String s = new StringBuilder(&quot;toto&quot;).append(&quot;tata&quot;).append(&quot;titi&quot;).append(&quot;tutu&quot;).toString();<\/code><\/p>\n<p>This optimization, although effective, has some limits, among others it depends of the capacity of the compiler to detect String concatenation. Moreover, the Java Just In Time Compiler (JIT) also optimize these String concatenations in a multiple of levels.<\/p>\n<p>The JEP 280 replaces all this by the usage of the InvokeDynamic bytecode for the String concatenation and implement this at runtime by a concatenation strategy optimized directly inside the JVM (an intrinsic). Multiple string concatenation strategies has been developed, including one based on MethodHandles (with inline StringBuilder), this is the default one.<\/p>\n<p>Tests made while developing this functionality shown that (see <a href=\"http:\/\/cr.openjdk.java.net\/~shade\/8085796\/notes.txt\"><a href=\"http:\/\/cr.openjdk.java.net\/~shade\/8085796\/notes.txt\">http:\/\/cr.openjdk.java.net\/~shade\/8085796\/notes.txt<\/a><\/a>):<\/p>\n<ul><li>Performances when the code is compiled by C2 (the last level of the JIT) are the same<\/li>\n\n<li>Performances when the code is only compiled by C1 (le first level of the JIT) are better<\/li>\n\n<li>Performances when C1\/C2 didn&#8217;t succeed of optimizing the string concatenation are way beter !<\/li>\n<\/ul>\n<p>In conclusion : no performance regression, better starting performances (when the code is not compiled or only by C1), better performance when javac or C1\/C2 didn&#8217;t succeed in optimizing the code!<\/p>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/280\"><a href=\"http:\/\/openjdk.java.net\/jeps\/280\">http:\/\/openjdk.java.net\/jeps\/280<\/a><\/a><\/p>\n<h2>JEP 285: Spin-Wait Hints<\/h2>\n<p>A new method in the Thread class allow to notify the JVM that we are in a spin-wait loop. The JVM is then free to do or not do something about it (this is a native method). A spin-wait loop is a loop in which the Thread that execute it cannot make anything because it is waiting on an external event (busy wait).<\/p>\n<p>Here are an exemple from the JavaDoc :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass EventHandler {\n  volatile boolean eventNotificationNotReceived;\n\n  void waitForEventAndHandleIt() {\n    while ( eventNotificationNotReceived ) {\n      java.lang.Thread.onSpinWait();\n    }\n  readAndProcessEvent();\n}\n\n  void readAndProcessEvent() {\n    \/\/ Read event from some source and process it\n    . . .\n  }\n}\n<\/pre>\n<p>More information in the description of the JEP :&nbsp;<a href=\"http:\/\/openjdk.java.net\/jeps\/285\"><a href=\"http:\/\/openjdk.java.net\/jeps\/285\">http:\/\/openjdk.java.net\/jeps\/285<\/a><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>In a previous article on Java 9, I listed all the main new features for the developers : <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\">http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs<\/a>. Here, I will list all the main performance optimizations of Java 9. I will again go through the main JEP : JEP 143: Improve Contended Locking Optimization of Java monitors (lock optimization) when contended (when multiple threads access the same monitor). These are changes inside the JVM that allow siginficant (x2) performance boost on some JVM operations on lock primitives on&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/les-optimisations-de-performances-de-java-9\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[11,158,159],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-java9","tag-performance"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":722,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-10-quoi-de-neuf\/","url_meta":{"origin":739,"position":0},"title":"Java 10 : what&#8217;s new ?","author":"admin","date":"Monday March 26th, 2018","format":false,"excerpt":"Now that java 10 is out, it's time to look at all the new functionalities of this version. Like my previous article on Java 9, I will focus on the changes that will impact developers that uses Java leaving aside the changes that are internal\/very small\/on rarely used API. The\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1112,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-15-quoi-de-neuf\/","url_meta":{"origin":739,"position":1},"title":"Java 15 : what&#8217;s new ?","author":"admin","date":"Thursday July  2nd, 2020","format":false,"excerpt":"Now that Java 15 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk throught all it\u2019s functionalities that brings to us, developers, this new version. This article is part of a series on what\u2019s new on the last versions of Java, for those who\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":652,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\/","url_meta":{"origin":739,"position":2},"title":"What&#8217;s new in java 9 for developers","author":"admin","date":"Monday March 27th, 2017","format":false,"excerpt":"Sorry, this entry is only available in Fran\u00e7ais.","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"jshell the Java REPL","src":"https:\/\/i0.wp.com\/loicmathieu.fr\/wordpress\/wp-content\/uploads\/jshell.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":947,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-13-quoi-de-neuf\/","url_meta":{"origin":739,"position":3},"title":"Java 13 : what&#8217;s new ?","author":"admin","date":"Tuesday August 13th, 2019","format":false,"excerpt":"Now that Java 13 is features complete (Release Candidate at the day of writing), it\u2019s time to walk throught all it\u2019s functionalities that brings to us, developers, this new version. This article is part of a series on what\u2019s new on the last versions of Java, for those who wants\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1877,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-24-quoi-de-neuf\/","url_meta":{"origin":739,"position":4},"title":"Java 24 : what&#8217;s new?","author":"admin","date":"Friday January 10th, 2025","format":false,"excerpt":"Now that Java 24 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk through all the functionalities that bring to us, developers, this new version. This article is part of a series on what\u2019s new on the last versions of Java, for those who\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":865,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-12-quoi-de-neuf\/","url_meta":{"origin":739,"position":5},"title":"Java 12 : what&#8217;s new","author":"admin","date":"Wednesday January 23rd, 2019","format":false,"excerpt":"Now that Java 12 is features complete (Rampdown Phase 2 at the day of writing), it's time to walk throught all it's fonctionalities that brings to us, developers, this new version. This article is part of a serie on what's new on the last versions of Java, for those who\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":0,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}