Results 1 to 15 of 15
Thread: Compilator translation
- 07-29-2012, 10:54 PM #1
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:
my question is:Java Code:do { /* do something */ sweep++; } while (sweep < infos.getTermsList().size() && others conditions)
if infos aren't modified during do-while instructions, at compile time the instruction
are converted in to a local variable or it will be called every time?Java Code:infos.getTermsList().size()
- 07-29-2012, 10:59 PM #2
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.
- 07-30-2012, 10:47 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: Compilator translation
It is called every time.
- 07-30-2012, 11:08 AM #4
Re: Compilator translation
You're right, it's a question that I want to solve once forever ;)
- 07-30-2012, 11:57 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 07-30-2012, 06:55 PM #6
- 07-30-2012, 07:18 PM #7
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.
- 07-30-2012, 11:20 PM #8
Re: Compilator translation
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 :( :(
- 07-31-2012, 12:55 AM #9
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.
- 07-31-2012, 02:00 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Compilator translation
Sorry if I'm missing something, but why not just say
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.Java Code:int size = infos.getTermsList().size();
- 07-31-2012, 08:46 AM #11
- 07-31-2012, 09:57 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Compilator translation
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.
- 07-31-2012, 11:06 AM #13
Re: Compilator translation
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 :(
- 07-31-2012, 12:22 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 07-31-2012, 12:40 PM #15
Similar Threads
-
Java Translation
By hello_world in forum New To JavaReplies: 6Last Post: 02-09-2012, 12:14 PM -
Language translation
By archanaanbu in forum Java AppletsReplies: 13Last Post: 02-19-2011, 06:56 PM -
Language translation
By archanaanbu in forum Java AppletsReplies: 0Last Post: 02-09-2011, 05:21 AM -
WORD Translation HELP PLZ :(
By sammypants in forum New To JavaReplies: 2Last Post: 11-24-2009, 01:46 PM -
X / Y Translation Problem
By SkinnyK in forum New To JavaReplies: 4Last Post: 01-16-2009, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks