How to design class hierarchy to make use of Bimorphic inlining

I have a question regarding the optimum number of implementations you can have of an interface.
I have 1 interface and 2 main implementations. All the methods except one very rarely invoked method in those two main sub-classes are frozen using final.
interface Foo{
  void hotMethod1();
  void rarelyUsedMethod();
class A implements Foo{
  final void hotMethod1(){.. .. ..}
  //Non final!!
  void rarelyUsedMethod(){.. .. ..}
class B implements Foo{
  final void hotMethod1(){.. .. ..}
  //Non final!!
  void rarelyUsedMethod(){.. .. ..}
}Then there are many (more than 20!) sub-classes of these 2 main sub-classes - dynamically generated bytecode.
class Aee26671 extends A{
  void rarelyUsedMethod(){.. .. ..}
class B33423 extends B{
  void rarelyUsedMethod(){.. .. ..}
class C12ds423 extends A{
  void rarelyUsedMethod(){.. .. ..}
and so on...All these second level sub-classes just override the rarelyUsedMethod().If in some caller method I do this:
x(Foo foo){
  foo.hotMethod1();
}Does the Hotspot compiler recognize that there are only 2 possible targets because A and B have their implementations marked as final?
I've heard of Bimorphic inlining. But does it look just at candidate Sub-classes - in this case there are more than 2 sub-classes.
Or, does it realize that the actual candidate implementations of the hot method are just 2 (A and B) because they are "final" and optimize it?
PS: Does Hotspot do Bi or Polymorphic inlining?

Hi Ashwin,
as far as I understand, the bi-morphic case is really a special optimization that occurs when there are only two possible classes (not necessarily methods) involved. You can easily test that by adapting my newsletter: [Polymorphism Performance Mysteries Explained|http://www.javaspecialists.eu/archive/Issue158.html]
Here is how I tested it:
import java.util.Random;
public class PolymorphismTest {
  private static final int UPTO = 100 * 1000 * 1000;
  public static void main(String[] args) throws Exception {
    int offset = Integer.parseInt(args[0]);
    Foo[] tests = generateTestData(offset);
    System.out.println("Warmup");
    test_all(tests);
    System.out.println("Actual run");
    printHeader(tests);
    test_all(tests);
  private static void test_all(Foo[] tests) {
    for (int j = 0; j < 10; j++) {
      run_tests(tests);
  public static void run_tests(Foo[] tests) {
    long time = System.currentTimeMillis();
    test(tests);
    time = System.currentTimeMillis() - time;
    System.out.print(time + "\t");
    System.out.flush();
    System.out.println();
  public static void test(Foo[] sources) {
    Foo t0 = makeRandomTest(sources);
    Foo t1 = makeRandomTest(sources);
    Foo t2 = makeRandomTest(sources);
    Foo t3 = makeRandomTest(sources);
    Foo t4 = makeRandomTest(sources);
    Foo t5 = makeRandomTest(sources);
    Foo t6 = makeRandomTest(sources);
    Foo t7 = makeRandomTest(sources);
    Foo t8 = makeRandomTest(sources);
    Foo t9 = makeRandomTest(sources);
    for (int i = 0; i < UPTO / 10; i++) {
      t0.hotMethod1();
      t1.hotMethod1();
      t2.hotMethod1();
      t3.hotMethod1();
      t4.hotMethod1();
      t5.hotMethod1();
      t6.hotMethod1();
      t7.hotMethod1();
      t8.hotMethod1();
      t9.hotMethod1();
  private static Foo makeRandomTest(Foo[] sources) {
    return sources[((int) (Math.random() * sources.length))];
  private static void printHeader(Foo[] tests) {
    System.out.print(tests[0].getClass().getSimpleName());
    System.out.print('\t');
    System.out.println();
  private static Foo[] generateTestData(int offset)
      throws Exception {
    switch (offset) {
      default:
        throw new IllegalArgumentException("offset:" + offset);
      case 0:
        return fillSources(A.class);
      case 1:
        return fillSources(A.class, B.class);
      case 2:
        return fillSources(A.class, B.class, Aee26671.class);
      case 3:
        return fillSources(A.class, B.class, Aee26671.class, B33423.class);
  private static Foo[] fillSources(
      Class<? extends Foo>... fooClasses)
      throws Exception {
    Foo[] sources = new Foo[1000];
    Random rand = new Random(0);
    for (int i = 0; i < sources.length; i++) {
      int offset = Math.abs(rand.nextInt() % fooClasses.length);
      sources[i] = fooClasses[offset].newInstance();
    return sources;
}On my machine, the results are as follows:
1 class: average=0, stdev=0
2 classes: average=103, stdev=4
3 classes: average=672, stdev=26
4 classes: average=666, stdev=0.4
As I thought, the bi-morphic optimization is only applied on a class instance basis, rather than a method basis.
More information in my newsletter: [Polymorphism Performance Mysteries Explained|http://www.javaspecialists.eu/archive/Issue158.html]
Regards
Heinz

Similar Messages

  • How to design an iPhone app that uses both Portrait and Landscape in Flash CS6

    So I have created an App that has a different menu when it is held portrait, but changes when held landscape. I can get the navigation to work, However I am unsure how to design landscape pages inside Flash?
    My stage is set to the standard 640x960 air for iOS, which gives me perfect portrait pages, But when I tilt my phone to landscape the screen obviously changes, and it looks weird. Im hoping someone can tell me how i can get this view inside Flash, while still having the portrait screens too?
    I want to have both Portrait and Landscape in the same app, and am currently so confused how I can design this inside flash?
    I hope this makes sense, and that someone could throw some help my way!

    you can change the display depending on stage orientation but it doesn't make sense to "..swap the stage orientation between screens".
    to change the display, use a StageOrientationEvent.ORIENTATION_CHANGE event and listener:
    stage.addEventListener("orientationChange", orientationChangeF);
    function orientationChangeFr(e:StageOrientationEvent):void{
    trace(e.afterOrientation.toString());

  • How to design  class(es) for implementing game characters?

    Good day! i have a player, monster1, monster2, vendor etc. in my game. what is the best way to design classes for my creatures? by the way, im using midp (J2ME). for now, i have a Creature superclass for my characters. Player, Monster, NPC are its subclasses. have i done the right thing?
    What about class design for my monsters? will i make Monster class a superclass for Monster1, Monster2, Monster3 etc?
    im just concerned of my code's efficiency and speed. i wonder if too much classes could slow down my application since i'm developing a mobile game..?
    regards.

    "In many cases multiple instances of a single class works just as well."
    This sounds a lot like the way I would do something in other languages. I would simply create an array of structs (or records) containing variables like these:
    type critter:record
      int x,y; // The critter's position, of course
      int vx,vy; // Used to keep track of velocity
      int ctype; // What kind of thing this is
      int subtype; // Additional flags or info that may affect behavior
      int yadayadayada,etcetera;
    end;
    var critters:array[0..somelargenumber] of critter;Of course, I suspect that what may work well in Pascal might not work as well in Java. For one thing, I don't think Java even has records per se. It may be necessary to use a class to duplicate that functionality. (Is this true? Or did I miss something?) The array then becomes an array of objects.
    Since I'm using classes anyway, I guess it makes sense that I should go all the way, creating interfaces for common functions and subclassing a basic Critter class for each different kind of critter (at least in cases where the behavior and/or rendering methods would be significantly different from the basic class methods).
    This is okay as long as I don't go overboard, right?

  • How to understand WS has to make use of SOAP

    Hi All
    via WLS 8.x, we can easily try a HelloWorld java class basis WS and test it
    like the following:
    String wsdlUrl = "http://localhost:7001/web-services/HelloWorld?WSDL";
    HelloWorld service = new HelloWorld_Impl( wsdlUrl );
    HelloWorldPort port = service.getHelloWorldPort();
    result = port.get( ... );
    What I don't understand is, how is the SOAP applied and how can it be kept
    track of during the request and response?
    Thanks lots
    John
    Toronto

    HI...
    If u want to keep track of soap message u can use handler class (GenericHandler ) which allows you to read and modify or attach attachement.In client code you need to specify this handler then it ll call request and reponse method of handler berfore n after calling webservice.
    Cheers,
    Rana...
    Inter net Inter Operate.......Webservices everywhere....

  • Xc7z015clg485 and xc7z030sbg485, how to design the schematic to make them pin to pin compatible

    Hi all,
    I have a schematic design case about pin to pin compatiblity with xc7z015clg485 and xc7z030sbg485. I compared xc7z015clg485pkg.txt and xc7z030sbg485pkg.txt. And found something different as below:
        1) 010 no HP bank, 030 have HP banks. In 010, H8    IO_0_34; In 030, H8    IO_0_VRN_34; In 010, R8    IO_25_34; In 030, R8    IO_25_VRP_34. So I assigned ddr3 pins to bank34 and bank35, and pulldown H8, pullup R8.
        2) In 010, V7 NC; In 030, V7 MGTAVTTRCAL_112; In 010, W3 NC; In 030, W3 MGTVCCAUX. 010 only have GTP, 030 only have GTX. So I followed the GTX rule to assign CML pins and power pins. That is to say, supplied V7 with 1.2V, and W3 with 1.8V. (to V7 and  W3 with preserved 0 ohm resistors)
        3) The PS pins of 010 are fully compatible with 030's ? (I assigned PL pins only with 030 pins definition)
        Is there something wrong with my above 3 schematic compatible designs ? Or others I had not considered.  Any question, could you please email to [email protected] or call me directly +86-134-0532-1640 ? Thank you for your help and attention!
     

    Yeah, I've read the official Toshiba statement on this. It is, to be frank, utter bulls**t.
    Almost all socket M processors have thermal characteristics within a few degrees of each other, a cooling system designed for one of them will cool all of them just as well. Certainly one that is designed to cool a hotter CPU should be able to cope with a cooler one - as I said the Thermal Design Power of a T2300 is 2 degrees +lower+ than that of the T7200 the laptop shipped with. It is also an older chip, so there is no real excuse for the bios not being programmed to recognise it and cool it appropriately.
    That statement should read like this:
    "We have made sure that our laptops only work with the specific processor they ship with. This is to preclude you increasing the performance of the laptop yourself. If you want a faster laptop we want to force you to buy a whole new one from us instead of just getting a better chip and fitting it using simple tools."
    In my case I'm actually putting a slower chip into the laptop but it won't work for the same reason: Toshiba have +deliberately+ crippled any upgrade/downgrade paths. Toshiba have tools to change the bios so it recognises the installed chip and runs the fan appropriately, but apparently they won't share them (see "real reason" above) - I'm hoping that somebody out there also has the tools and +is+ willing to share them.

  • How to find out which users are using SharePoint Designer to make changes in SharePoint 2010 site?

    Hi there,
    How to find out which users are using SharePoint Designer to make changes in SharePoint 2010 site?
    Thanks.

    You need to enable auditing on SharePoint server and it will let you know, if someone makes any critical changes for the same.
    Please walk through this informative KB to enable auditing on SharePoint :https://support.office.com/en-za/article/Configure-audit-settings-for-a-site-collection-f5a346d0-ee0f-4412-a5e6-d9b5abaa1012
    Here is one more resource :
    https://support.office.com/en-in/article/View-audit-log-reports-4293e8d5-4e7d-4201-b8ac-c8e63a100131
    Moreover, if you wish to audit such critical changes automatically, you may consider on this comprehensive application (http://www.sharepointauditing.com/) that helps to track every changes on SharePoint into
    real time and provides the captured data at granular level.

  • How can I develop a web application using EJB design pattern?

    I have searched over the web and found quite a lot of tutorials on how to use the EJB design pattern.
    I know that there will be a home interface, EJB object interface and a SessionBean.
    But the tutorials often only cover a single class, this made me unable to get a complete picture of how EJB design pattern can be implemented into a whole system.
    I am now required to devleop an online shopping web application using EJB and JSP page.
    I think I will need to create a lot of classes: Member, ShoppingCart, Product...etc.
    What I want to ask is that, do I need to create a home interface, EJB object interface and a SessionBean for each of these classes?
    I really need some ideas on how to develop this system using EJB + JSP pages.
    Many thanks to you all.

    For every EJB that you want to create, you will need to code a home and remote interface and a bean class.
    You could start getting your ideas here
    http://www.theserverside.com/books/wiley/masteringEJB/
    http://www.coreservlets.com

  • We are a creative design studio, we need to use apple mac pro server , so we can make more than a different user to use at the same time doing different activities, on different screens, is it possible?what is the max. no. of users that can work efficient

    we are a creative design studio, we need to use apple mac pro server , so we can make more than a different user to use at the same time doing different activities, on different screens, is it possible?what is the max. no. of users that can work efficient.
    Appreciate your support and if possible , how to do this?

    If you want to work with Mac OS X, you need one computer per simultaneous user.
    What you are describing, " Multiple simultaneous logins to a single computer" is not avialable on a regular Mac of any description, unless you decide to use Unix tools instead of Mac OS X.
    Server will happily store files for many, many users and provide them to multiple (up to hundreds) of computers at "near hard Drive" speeds over Gigabit Ethernet. It can make the File Sharing part easy.

  • How to make use of classlocator in NWDS.

    Hi Experts,
    I don't have Class / jar Locator.
    I am unable to install from SourceForge Website, it is showing some error.
    where can i get it ?
    And how to make use of classlocator in NWDS.
    Help me out in this regard.it's urgent
    Regards
    Bala

    Hi Balakrishna,
    You can download the  classlocator  from this URL:
    http://www.filewatcher.com/b/ftp/ftp.heanet.ie/mirrors/download.sourceforge.net/pub/sourceforge/c/cl/classlocator.0.0.html
    Check this thread for more help
    How to use classlocator plugin?
    Thanks n Regards
    Santosh
    Reward if helpful !!!

  • How to make use of gui components in eclipse 3.2

    i mean, when u r writing some application,
    we can do it by graphical representationi.e Design view, the tool/editor will generate code for the the application what we have done in design view.
    how can i make use of that design view in eclipse 3.2/eclipsex.x?
    can any one help regarding this?

    I guess you mean an Eclipse GUI Builder? There's Jigloo, and V4All. There may be others.

  • How to make use of asynchronous service in CAF development

    Hello SDNs,
    How can we make use of asynchronous service in CAF development;
    Actually i am new to CAF development; my business requirement suites for the service provided by SAP. But the service provided is asynchronous; is it not possible to use the asynchronous service in my CAF application? if it is possible, can any one please tell me the drawbacks of using it!
    You help would be highly appreciated.
    Thanks,
    Sireesha.B

    Hi,
    >> I have some portal service and how to use that in the web dynpro application.
    Yes. U can use Portal service in webdynpro. Check the link given by subathra.
    >> and also what is the main diff between Portal Component and web dynpro application
    Portal Components are components designed to run in portal server while webdynpro application can run in J2EE WebAS.
    >> do we required portal to run web dynpro application.
    No for general cases. Just WebAS is sufficient to run webdynpro appl. But if u use portal service in ur webdynpro appl, then make sure both (portal service and webdynpro appl) run on same J2EE engine. That is mandatory.
    Regards,
    Vijai

  • How to make use of the swf made by alchemy?

    Most of the examples are about compiling C/C++ to swc, then make use of it.
    As the alchemy introduction said, we can also compile into swf file. so i tried it. But i cann't get the CLibInit class. How to make it successful?
    The codes is below:
        private function initApp():void
            var  loader:Loader = new Loader();
            var context:LoaderContext = new  LoaderContext();
            context.applicationDomain =   new  ApplicationDomain(ApplicationDomain.currentDomain);
             loader.load(new URLRequest("abc.swf"),context);
             loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
         private function onComplete(e:Event):void
            var clibinit:Class =  this.loaderInfo.applicationDomain.getDefinition("module.abc.CLibInit")  as Class;
    The abc.swf is the file i compile in Alchemy. And i can get the CLibInit class by using swc file as below:
        import __AS3__.vec.*;
        import cmodule.abc.*;    
        var abc:Object;
        private static function init() : void
                var clibinit:* = new CLibInit();
                abc = clibinit.init();
                return;
            }// end function

    I have the same problem. Did you get it?

  • How to make use of Windows authentication from my Java application

    I have a Java application, Instead I design one more login page for my application, I want to make use of Windows Authentication.
    How should I use that windows authentication in my java application
    can any help me in suggesting a solution

    How will they be able to access your application if they aren't users of the system?

  • How to make use of StreamGobbler?

    Hi,
    I want to redirect the out and err statements to a file.
    I found a class called StreamGobbler at http://www.physionet.org/physiotools/puka/sourceCode/puka/StreamGobbler.java
    I dont know how to make use of it as I have almost no knowledge of Concurrency and threads in Java.
    Plz guide.

    I found a class called StreamGobbler at
    http://www.physionet.org/physiotools/puka/sourceCode/p
    uka/StreamGobbler.javaDo you see, at the very top of that page, the URL for the original article? It explains everything step by step in great detail.

  • I want to make gImage Class extends Image and use Graphics2D

    Sorry, my english short...
    I want to make gImage Class extends Image and use Graphics2D
    I trying to do...
    make class gImage..
    extends Image class. (Inheritance)
    but Image class is abstract class..
    And I want to use gImage class same as Image class.
    I can't use constructor... because by Abstract calss...
    How do i?
    Help me.. plz...
    And I want to use Graphics2D at Applet Game, But not use Java JDK Plug-in...
    No Way?

    You need to use BufferedImage with 2D or RenderedImage with JAI.
    BufferedImage effectivley extends an Image so you can
    draw it to screen in paintCOmponent.
    I use an Extended JPanel to draw an image in a Panel with this.
    MArk

Maybe you are looking for