Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Partial inlining  (Read 1277 times)
0 Members and 1 Guest are viewing this topic.
Offline leknor

Junior Member




ROCK!!!


« Posted 2003-04-24 17: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

Senior Newbie





« Reply #1 - Posted 2003-04-28 15: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 Coder


Medals: 11


falling into the abyss of reality


« Reply #2 - Posted 2003-04-28 15: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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #3 - Posted 2003-04-28 17: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]
  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.

CogWheelz (16 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!