Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Partial inlining  (Read 899 times)
0 Members and 2 Guests are viewing this topic.
Offline leknor

Full Member
**

Posts: 218


ROCK!!!


« on: 2003-04-24 11:25:55 »

I'm just thinking out load here but if someone has an authoritive answer that'd rock too.

It's not too uncomon to see code for lazy initilization that at the start of a common public method you test a boolean and possibly initilize the rest of an object. eg:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
class Foo {
 boolean initilized = false;
 publc void doSomething() {
  if (!initilized) doInit();
  // Other work
}

 private void doInit() {
  // Some stuff
 initilized = true;
 }
}

threading issues and correctness aside, the above code is a little error prone because as I add new methods I have to remeber to include that whole if statement and method call.

Slightly more robust is the code below:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
class Foo {
 boolean initilized = false;
 publc void doSomething() {
  doInit();
  // Other work
}

 private void doInit() {
  if (initilized) return;
  // Some stuff
 initilized = true;
 }
}

In this case the "if (initilized)" is in the doInit() method and I don't have to worry about when it is called. But now every time I call doSomething() I incur the overhead of calling doInit() even if it immeaditly returns.

Is it possible for HotSpot to optimize the "if (initilized)" to just before the method call? Is it worth the time and effort?
Offline Exocet

JGO n00b
*

Posts: 12



« Reply #1 on: 2003-04-28 09:02:40 »

If this is just a normal object, is there any reason why you wouldn't want to initialize it in the constructor?  
Offline Abuse

JGO Kernel
*****

Posts: 1866
Medals: 5


falling into the abyss of reality


« Reply #2 on: 2003-04-28 09:40:47 »

Quote
If this is just a normal object, is there any reason why you wouldn't want to initialize it in the constructor?  


its not that kind of init.
Games published by our own members! Go get 'em!
Offline rreyelts

Sr. Member
**

Posts: 300


There is nothing Nu under the sun


« Reply #3 on: 2003-04-28 11:30:00 »

Is it possible for HotSpot to optimize the "if (initilized)" to just before the method call? Is it worth the time and effort?

What you're describing is runtime constant analysis (very similar to monomorphic analysis), where HotSpot determines that the initialized flag is basically a constant false at runtime and rewrites the method in terms of a false flag. Then, given that the method becomes empty with a false flag, it is a trivial optimization to "inline" that into the calling method.

I wouldn't be surprised if HotSpot server did this (because it does optimizations of that nature), but I wouldn't be surprised if it didn't (because it likely won't waste the cpu cycles trying to optimize a method that is such a trivial portion of the execution time of your program). You might be able to force it to do this by turning the compilation parameters all the way up. If your init method is small enough, HotSpot may just decide to inline the whole entire thing.

In any case, I wouldn't be worried about it. I'd bet dimes to dollars that you've got much more important things to be concerned about compared to the cost of a non-virtual method call. If HotSpot saves you those nanoseconds, then good for it, otherwise, no big loss.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.091 seconds with 19 queries.