Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Compilator translation

    I have a very simple question.
    I am developing a software that give particular focus on performance.
    If I have a code like:
    Java Code:
    do
    {
    	/* do something */
    	sweep++;
    }
    while (sweep < infos.getTermsList().size() && others conditions)
    my question is:
    if infos aren't modified during do-while instructions, at compile time the instruction
    Java Code:
    infos.getTermsList().size()
    are converted in to a local variable or it will be called every time?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Compilator translation

    To see for yourself, add a call to println in getTermsList() that prints a message every time it is called.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: Compilator translation

    It is called every time.

  4. #4
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    You're right, it's a question that I want to solve once forever ;)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Compilator translation

    Essentially the compiler has to compile it to call the size() everytime as it has no real way of knowing whether that value (or the termsList one) is changed at all during the execution of the loop. Since it can't make that assumption it creates code that calls it everytime.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    Quote Originally Posted by Tolls View Post
    Essentially the compiler has to compile it to call the size() everytime as it has no real way of knowing whether that value (or the termsList one) is changed at all during the execution of the loop. Since it can't make that assumption it creates code that calls it everytime.
    Theoretically with static optimization of code the compiler can know if termsList.
    It seems that Java Compiler doesn't make that optimization ;)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Compilator translation

    What is returned by getTermsList()?
    Can its size be changed while the loop is executing?
    How can the compiler know that?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    Quote Originally Posted by Norm View Post
    What is returned by getTermsList()?
    Can its size be changed while the loop is executing?
    How can the compiler know that?
    getTermsList() returns an ArrayList<String>
    It's size doesn't change during loop.
    With static analysis compiler can know if structures involved in loop changes during execution, but this work if compiler perform this type static analysis during pre-compilation.
    The question was: JVM do that?
    Unfortunately no :( :(

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Compilator translation

    What about another thread?
    Also, I think there can be different versions of the JVM from different sources.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Compilator translation

    Sorry if I'm missing something, but why not just say

    Java Code:
    int size = infos.getTermsList().size();
    before the loop starts and then use size in the while condition. This also better expresses the assumption that the size will not be changing.

  11. #11
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    Quote Originally Posted by Norm View Post
    What about another thread?
    Also, I think there can be different versions of the JVM from different sources.
    Static analysis can include milti-thread with shared data ;)

    Quote Originally Posted by pbrockway2 View Post
    Sorry if I'm missing something, but why not just say

    Java Code:
    int size = infos.getTermsList().size();
    before the loop starts and then use size in the while condition. This also better expresses the assumption that the size will not be changing.
    That was done ;)
    Thanks :)

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Compilator translation

    Quote Originally Posted by claudiocarcaci View Post
    Static analysis can include milti-thread with shared data ;)
    No it can't because it can only do that if it knows the class 'infos' is used in a multi-threaded environment or not.
    And that is not known at compile time, only at runtime.

    Now the JVM itself could do that based on actual execution, but even then it could only do that if it loaded the entire application and analysed it to understand what is going on...at runtime...I know I would not want to take that hit.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    Quote Originally Posted by Tolls View Post
    No it can't because it can only do that if it knows the class 'infos' is used in a multi-threaded environment or not.
    And that is not known at compile time, only at runtime.

    Now the JVM itself could do that based on actual execution, but even then it could only do that if it loaded the entire application and analysed it to understand what is going on...at runtime...I know I would not want to take that hit.
    But multithreaded apps with sharing of infos data are written in static code ;)
    http://www.lst.inf.ethz.ch/research/.../PLDI_2003.pdf ;)
    It's possible, but JVM doesn't do that :(

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Compilator translation

    "A compiler for an object-oriented programming language
    with multi-threading needs precise information about the
    sharing of objects."

    Which is exactly what I was saying.
    Neither the Java compiler nor the JVM has this information.
    To get this information you would have to completely change how Java works, starting with the class loader.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: Compilator translation

    Quote Originally Posted by Tolls View Post
    "A compiler for an object-oriented programming language
    with multi-threading needs precise information about the
    sharing of objects."

    Which is exactly what I was saying.
    Neither the Java compiler nor the JVM has this information.
    To get this information you would have to completely change how Java works, starting with the class loader.
    Misunderstang, regret :(
    :D

Similar Threads

  1. Java Translation
    By hello_world in forum New To Java
    Replies: 6
    Last Post: 02-09-2012, 01:14 PM
  2. Language translation
    By archanaanbu in forum Java Applets
    Replies: 13
    Last Post: 02-19-2011, 07:56 PM
  3. Language translation
    By archanaanbu in forum Java Applets
    Replies: 0
    Last Post: 02-09-2011, 06:21 AM
  4. WORD Translation HELP PLZ :(
    By sammypants in forum New To Java
    Replies: 2
    Last Post: 11-24-2009, 02:46 PM
  5. X / Y Translation Problem
    By SkinnyK in forum New To Java
    Replies: 4
    Last Post: 01-16-2009, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •