Function and variable inlining on Sun javac

Discussion in 'Java' started by Ronald Fischer, Jul 20, 2004.

  1. The Java language specification explicitly allows function and variable inlining
    for certain cases, such as final functions and final variables.

    Relating to this, I would like to know how Sun's Java Compiler deals with this.
    Does it actually do inlining? What about calls to (non-final, non-static)
    functions in different packages? May these be inlined too?

    I tried to search Sun's Java Docs, but did not find anything useful.

    Ronald
     
    Ronald Fischer, Jul 20, 2004
    #1
    1. Advertising

  2. Ronald Fischer wrote:
    > The Java language specification explicitly allows function and variable inlining
    > for certain cases, such as final functions and final variables.
    >
    > Relating to this, I would like to know how Sun's Java Compiler deals with this.
    > Does it actually do inlining? What about calls to (non-final, non-static)
    > functions in different packages? May these be inlined too?


    Chances are, some Sun guy who sometimes frequents this group and who
    knows "a little bit" about the compiler :) will give you a much better
    answer. My knowledge is from some old discussions.

    IIRC a lot of optimizations have in fact been been removed from the
    compiler and left to the JIT compiler in the runtime. The compiler's
    optimizations did make it harder for the JIT compiler, so they had to go.

    You will see that constants like "static final bool DEBUG = false" will
    be inlined, but that's all I have seen recently.

    /Thomas
     
    Thomas Weidenfeller, Jul 20, 2004
    #2
    1. Advertising

  3. Ronald Fischer wrote:
    > The Java language specification explicitly allows function and variable inlining
    > for certain cases, such as final functions and final variables.
    >
    > Relating to this, I would like to know how Sun's Java Compiler deals with this.
    > Does it actually do inlining?


    It inlines primitive and string constants, since it actually *has* to do
    that. Switch wouldn't work if it didn't inline such constants.

    Some time ago, javac also attempted to inline some final methods, but it
    actually generated illegal bytecode many times, so the feature was
    discarded (see below.)

    >What about calls to (non-final, non-static)
    > functions in different packages? May these be inlined too?


    No. It is not possible in general, because of the language spec
    constraints on binary compatibility and the nature of dynamic linking:

    QUOTE

    13.4.20 Method and Constructor Body

    Changes to the body of a method or constructor do not break
    compatibility with pre-existing binaries.

    We note that a compiler cannot expand a method inline at compile time.

    The keyword final on a method does not mean that the method can be
    safely inlined; it means only that the method cannot be overridden. It
    is still possible that a new version of that method will be provided at
    link time. Furthermore, the structure of the original program must be
    preserved for purposes of reflection.

    In general we suggest that implementations use late-bound (run-time)
    code generation and optimization.

    UNQUOTE

    Note that inlining of final methods is only legal at runtime:

    QUOTE

    8.4.3.3 final Methods

    At run time, a machine-code generator or optimizer can "inline" the body
    of a final method .... Such inlining cannot be done at compile time
    unless it can be guaranteed that Test and Point will always be
    recompiled together, so that whenever Point-and specifically its move
    method-changes, the code for Test.main will also be updated.

    UNQUOTE
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Jul 20, 2004
    #3
  4. Ronald Fischer

    Roedy Green Guest

    On Tue, 20 Jul 2004 15:45:44 +0200, Thomas Weidenfeller
    <> wrote or quoted :

    >> Relating to this, I would like to know how Sun's Java Compiler deals with this.
    >> Does it actually do inlining? What about calls to (non-final, non-static)
    >> functions in different packages? May these be inlined too?


    The inlining is done by Hotspot. What is really remarkable is that a
    new class being loaded may invalidate previous inlining. Hotspot has
    to revert to interpretation in mid-flight and then gradually rehotspot
    without the inlining for the newly overridden method.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 20, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xarax
    Replies:
    1
    Views:
    1,481
    Andy Flowers
    Jul 2, 2003
  2. Stefan
    Replies:
    0
    Views:
    5,028
    Stefan
    Nov 5, 2003
  3. Andrew Thompson

    com.sun.tools.javac.Main ..API docs?

    Andrew Thompson, Jan 4, 2004, in forum: Java
    Replies:
    13
    Views:
    4,723
    Andrew Thompson
    Jan 6, 2004
  4. Jesper Sahner
    Replies:
    7
    Views:
    7,910
    Jesper Sahner
    Nov 21, 2004
  5. Replies:
    0
    Views:
    2,902
Loading...

Share This Page