Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  final variables and hotspot optimization  (Read 3459 times)
0 Members and 1 Guest are viewing this topic.
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Posted 2005-10-18 09:24:55 »

Hi, haven't verified it yet, but doas anyone know if such code would be fully optimized by hotspot.

Class Test {
static final boolean DEBUG = GlobalProperties.isDebug();

...
void test() {
if (DEBUG){  // --- > will that be optimized (thrown away) by hotspot is DEBUG = false at runtime ?
  // println...
}
}

the javac cannot optmize the code by deleting the if(DEBUG) from the bytecode as its value isn't known at compile time... but what about hotspot ?

Lilian

Offline Mark Thornton

Senior Member





« Reply #1 - Posted 2005-10-18 10:16:40 »

Yes that works (and has for years). I use it all the time. The java assert system is done that way too (just a little hidden under the covers).
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2005-10-18 12:03:52 »

I seem to recall the optimizing javac compiler option also cuts out dead code at compile time.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #3 - Posted 2005-10-18 12:42:11 »

But this is not dead code...as the value of the flag relies on a dynamic property (e.g. taken from command line).

It's only dead at runtime.

Lilian

Offline Mark Thornton

Senior Member





« Reply #4 - Posted 2005-10-18 13:20:17 »

Compile time dead code is always removed (and the optimising option no longer exists anyway), but that is not the query of this thread.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2005-10-18 14:19:05 »

Just covering all the bases Wink
Hotspot Server VM culls dead code at runtime.

Cas Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #6 - Posted 2005-10-18 15:04:24 »

I'm not so sure the client VM does this as well (I have to test), and the client VM is the important VM.

Offline Mark Thornton

Senior Member





« Reply #7 - Posted 2005-10-18 15:34:30 »

I seem to remember testing it on the client VM once and finding that it did work (i.e. there was no detectable slow down caused by the test).
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #8 - Posted 2005-10-18 17:41:50 »

Both the HotSpot client and server compilers perform this optimization of loading the values of static final fields when the bytecodes are compiled to machine code (assuming the class has been initialized, etc.)
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2005-10-18 20:03:07 »

This very test was performed on JGO a few years ago but my pot-addled brain tells me that the client VM did not perform this optimisation at runtime, at the time (which would have been a 1.4 VM).

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #10 - Posted 2005-10-18 21:28:50 »

I just did some testing on the client and it seems to perform this optimization, but I think there's a quite annoying limitation.
for example, when you have in your class a

final boolean DO_SOMETHING_MORE = false;

and elsewhere you do somewhere in a tight loop or something

if (DO_SOMETHING_MORE) {
  system.out.println("Doing something more");
}

this is correctly optimized away.

But, if you set this final boolean in the constructor like this:

public MyClass(boolean doSomethingMore) {
  this.DO_SOMETHING_MORE = doSomethingMore;
}

and you instantiate this class like this

MyClass myInstance = new MyClass(false);

this optimization is not done.

It seems like either this optimization is done only by javac, or *if* such optimizations are done in runtime (which doesn't seem so) it just fails to optimize this away if the final boolean is set in the constructor. Whatever the case, the optimization seems only to be performed in a static context.
(EDIT: when it's a static constant, not a constant in an instance)

In any way, this seems like a really annoying limitation for me because I wanted to be able to optimize a CPU emulator if possible by adding a flag in the constructor like this CpuEmulator(boolean strictEmulation) where the CPU will be emulated a little bit more loosely (for example no cycle counting and no addressing exceptions) if strict emulation is set to false.

Offline pepijnve

Junior Member




Java games rock!


« Reply #11 - Posted 2005-10-19 09:20:51 »

Isn't it to be expected that specific instantiations of a class are not taken into account when optimizing? Otherwise if you had two instances of MyClass: new MyClass(false) and new MyClass(true), those two instances would need to have different compiled code behind them...
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #12 - Posted 2005-10-19 09:25:29 »

It is to be expected if it is an ahead of time compilation optimization, but there was talk about it being a runtime optimization. But I suppose it's maybe a too difficult and expensive optimization to do in runtime (especially in the client VM).

Offline pepijnve

Junior Member




Java games rock!


« Reply #13 - Posted 2005-10-19 10:20:54 »

You would have to ask a jvm guru for more details, but I have a gut feeling that you can't have multiple compiled versions of a bit of bytecode at runtime. (This is just my gut feeling, so I might be completely wrong Smiley) If my assumption is correct, then that kind of explains why it can't be optimised (easily). You could compile the case where the field is false, but then once an instance gets created which sets the field to true, the compiled code would have to be invalidated.
Also, if it is possible to have multiple compiled versions per method/class then the number of possible compiled versions would grow exponentially with the number of final booleans...
If the field in question is static and final it might get optimized at runtime? Something like:
1  
2  
3  
4  
5  
private static final boolean STRICT_CPU_EMU;

static {
  STRICT_CPU_EMU = Boolean.valueOf(System.getProperty("strictcpuemulation")).booleanValue();
}

but that's probably not what you want to achieve.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #14 - Posted 2005-10-19 11:12:07 »

You're right, that's not what I want to achieve  Smiley

But maybe something like this could be used to do something like unload a class, set the system property to something else and then reload it? (dunno if this is possible)

Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #15 - Posted 2005-10-19 11:20:32 »

It's possible with a suitable ClassLoader
(you could even have multiple concurrent versions of the same class, )

There's an article at javalobby today on that topic with a list of classloaders (mainly for embedding plugins into applications).

Lilian

http://javalobby.org/java/forums/t18345.html

Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #16 - Posted 2005-10-19 11:56:29 »

As I understand it this optimisation is figured out by Hotspot Server. And don't forget it can dynamically decompile code when it notices something's changed.

Cas Smiley

Offline pepijnve

Junior Member




Java games rock!


« Reply #17 - Posted 2005-10-19 12:07:56 »

The key issue remains whether or not multiple compiled versions can exist in parallel. Otherwise the following scenario is possible
  • new CpuEmulator(false)
  • hotspot compiles bits of CpuEmulator, optimizing out the unused code
  • new CpuEmulator(true)
  • hotspot detects the change (which isn't a class level change) and decompiles CpuEmulator
At that point you have two options, either you're stuck with an unoptimized version of CpuEmulator that handles both cases or two specifically optimized versions exist in parallel...

Anyway I'm going to stop speculating now Smiley
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #18 - Posted 2005-10-19 17:18:48 »

The key issue remains whether or not multiple compiled versions can exist in parallel. Otherwise the following scenario is possible

Off the top of my head, that's a fundamental internal feature of the JVM that's been around for as long as hotspot has (if not longer).

malloc will be first against the wall when the revolution comes...
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #19 - Posted 2005-10-19 18:35:51 »

HotSpot currently only folds static final fields' values into generated machine code, as they are guaranteed by the language and libraries to be constant over the program's execution. HotSpot does not (at least currently) generate multiple specialized copies of machine code based on a program's execution (aside from supporting multiple on-stack-replacement copies with particular entry points), though the same method, when inlined in multiple contexts, may be optimized differently based on the local usage. Note though that specialization is not necessarily required in order to optimize some of the usages of non-static final fields, or in fact non-final fields, because of HotSpot's ability to dynamically deoptimize under arbitrary conditions. Some work is underway to take advantage of optimization opportunities in this area.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #20 - Posted 2005-10-19 19:23:56 »

Maybe the question is also "do you always want multiple copies".
What if my constructor would look like this:

public MyClass(int optimizationLevel) {...}

so an int instead of a boolean. What if there are 50 different values of the int which would cause some code to be omitted or executed differently. I dunno how great it would be to have 50 slightly different copies of the same class in memory... (especially in my particular case where these kind of classes tend to be quite big). I could imagine that with some applications, the JVM's footprint could go through the roof if the JVM gets too eager to create multiple versions of the same class.

For now i think I'll look into how I could get multiple copies through a custom ClassLoader

Offline PeterB

Junior Member





« Reply #21 - Posted 2005-10-23 20:52:32 »

Because the DEBUG variable is static, the code I've pasted below will completely strip all of the "// do something" code from the class file at compile time if DEBUG is set to false in the following way:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Class Test
 {
    static final boolean DEBUG = false;

    void test()
    {
        if (DEBUG)
        {
           // do something
       }
   }
}


However, going back to your example, unless isDebug() is a static method in the class GlobalProperties that has the line "return false;" or "return true;" the result of this call cannot be determined, so all of the code below will remain in the class file. (you could be reading the debug value from a properties file for instance)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Class Test
 {
    static final boolean DEBUG = GlobalProperties.isDebug();

    void test()
    {
        if (DEBUG)
        {
           // do something
       }
   }
}


Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (11 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!