{"id":5089,"date":"2014-03-30T18:44:23","date_gmt":"2014-03-30T18:44:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/final-methods-are-inlined-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:44:23","modified_gmt":"2014-03-30T18:44:23","slug":"final-methods-are-inlined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/final-methods-are-inlined-collection-of-common-programming-errors\/","title":{"rendered":"final methods are inlined?-Collection of common programming errors"},"content":{"rendered":"<p>As Jon said the inlining is done (when needed) by the JIT compiler not at bytecode generation level. Note also that sometimes inlining can result in a performance degradation because it could create a situation where the same code is present multiple times in the cpu l1 cache, removing space for other code. L1 cache misses can impact the performance more than a jump to a cached function.<\/p>\n<p>Constants (aka final static var) are inlined instead.<\/p>\n<p>See this to check<\/p>\n<pre><code>public class InlineTest {\n    final static int add(int x, int y) {\n        return x + y;\n    } \n}\n\n\npublic class Main {\n\n        static final int DIVISOR = 7;\n\n        static void main(String[] args){\n            final int a = new Integer(args[0]);\n            final int b = new Integer(args[1]);\n\n            if (InlineTest.add(a, b) % DIVISOR == 0)\n                System.exit(InlineTest.add(a, b));\n\n            System.out.print(\"The sum is \" + InlineTest.add(a, b));\n\n        }\n}\n<\/code><\/pre>\n<p>This is translated in:<\/p>\n<pre><code> 0 new #2 \n 3 dup\n 4 aload_0\n 5 iconst_0\n 6 aaload\n 7 invokespecial #3 \n10 invokevirtual #4 \n13 istore_1\n14 new #2 \n17 dup\n18 aload_0\n19 iconst_1\n20 aaload\n21 invokespecial #3 \n24 invokevirtual #4 \n27 istore_2\n28 iload_1\n29 iload_2\n30 invokestatic #5 \n33 bipush 7\n35 irem\n36 ifne 47 (+11)\n39 iload_1\n40 iload_2\n41 invokestatic #5 \n44 invokestatic #7 \n47 getstatic #8 \n50 new #9 \n53 dup\n54 invokespecial #10 \n57 ldc #11 \n59 invokevirtual #12 \n62 iload_1\n63 iload_2\n64 invokestatic #5 \n67 invokevirtual #13 \n70 invokevirtual #14 \n73 invokevirtual #15 \n76 return\n<\/code><\/pre>\n<p>You can see that static function InlineTest.add has been called multiple times with <code>invokestatic<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Jon said the inlining is done (when needed) by the JIT compiler not at bytecode generation level. Note also that sometimes inlining can result in a performance degradation because it could create a situation where the same code is present multiple times in the cpu l1 cache, removing space for other code. L1 cache [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5089","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5089"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5089\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}