{"id":271,"date":"2011-06-02T10:18:01","date_gmt":"2011-06-02T10:18:01","guid":{"rendered":"https:\/\/kari.world.ikari.fi\/2011\/06\/02\/premature-optimization-chapter-class-is-the-root-of-all-evil\/"},"modified":"2011-06-02T10:18:01","modified_gmt":"2011-06-02T10:18:01","slug":"premature-optimization-chapter-class-is-the-root-of-all-evil","status":"publish","type":"post","link":"https:\/\/kari.world.ikari.fi\/?p=271","title":{"rendered":"Premature Optimization: Chapter &#8221;Class is the root of all evil&#8221;"},"content":{"rendered":"<p>It&#8217;s interesting question of how different constructs perform in code. And  in java world and very often encountered construct is discussion about what should be used as param (or return value) in API methods. From API maintainability, extensibility, clearness etc. point of view, answer is 101% clear. Using <b>interfaces<\/b> make APIs more generic, by leaving implementation specific detail away, which allows very easily to change implementation if nececssary.<\/p>\n<p>Premature optimization group, on the other hand, talks for using concrete classes in APIs since they are faster. Up to some extend that is true, but in reality in 99% of cases that performance difference is irrelevant when balanced against benefits what interfaces bring into table. Also doing microbenchmark to analyze effect is also practically futile (even if one in reference is showing some difference in special <code>volatile<\/code> access case), since JVM is doing some internal optimization for them. I remember reading from some notes in web (can&#8217;t recall &amp; find reference right now) that that there is some look up <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/hotspot-dev\/2009-February\/001373.html\">cache<\/a>, which makes performance in both cases equivalent, as long as amount of different concrete classes is same. Surely, effects of such caching are difficult to see in any microbenchmark.<\/p>\n<p>Digging deeper in research finds out that there really is some <a href=\"http:\/\/www.javaspecialists.eu\/archive\/Issue158.html\">smart optimization<\/a> in JVM.<\/p>\n<p><b>References:<\/b><br \/>\n<a href=\"http:\/\/bobah.net\/d4d\/source-code\/misc\/invokevirtual-vs-invokeinterface-performance-benchmark\">invokevirtual vs invokeinterface performance benchmark<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1504633\/what-is-the-point-of-invokeinterface\">What is the point of invokeinterface?<\/a><br \/>\n<a href=\"http:\/\/wikis.sun.com\/display\/HotSpotInternals\/MicroBenchmarks\">HotSpotInternals: MicroBenchmarks<\/a><br \/>\n<a href=\"http:\/\/wikis.sun.com\/display\/HotSpotInternals\/InterfaceCalls\">HotSpotInternals: InterfaceCalls<\/a><br \/>\n<a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/hotspot-dev\/2009-February\/001373.html\">Hotspot maillist: inline cache<\/a><br \/>\n<a href=\"http:\/\/en.wikipedia.org\/wiki\/Inline_caching\">Wikipedia: Inline Caching<\/a><br \/>\n<a href=\"http:\/\/www.javaspecialists.eu\/archive\/Issue158.html\">The Java Specialists&#8217; Newsletter: Polymorphism Performance Mysteries Explained<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/4423968\/how-are-java-interfaces-implemented-internally-vtables\">Stackoverflow: How are java interfaces implemented internally? (vtables?)<\/a><br \/>\n<a href=\"http:\/\/wikis.sun.com\/display\/HotSpotInternals\/VirtualCalls\">HotSpotInternals: VirtualCalls<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s interesting question of how different constructs perform in code. And in java world and very often encountered construct is discussion about what should be used as param (or return value) in API methods. From API maintainability, extensibility, clearness etc. point of view, answer is 101% clear. Using interfaces make APIs more generic, by leaving&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=271"}],"version-history":[{"count":0,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}