Three questions

manu3d

Active Member
Contributor
Architecture
1) In TerasologyEngine.mainLoop() there's the following code block. Note the comment at the beginning talking about "once a second". Then note the line "Thread.sleep(100)". My guess is that either the comment should be talking about 1/10th of a second or the Thread.sleep() parameter should be 1000 (ms). Right?

// Only process rendering and updating once a second
if (!display.isActive() && isHibernationAllowed()) {
time.setPaused(true);
Iterator<Float> updateCycles = time.tick();
while (updateCycles.hasNext()) {
updateCycles.next();​
}
try {
Thread.sleep(100);​
} catch (InterruptedException e) {
logger.warn("Display inactivity sleep interrupted", e);​
}
display.processMessages();
time.setPaused(false);
continue;​
}

2) In engine/subsystems/LWJGLInput.preUpdate() I stumbled upon these two lines:

NUIManager nuiManager = CoreRegistry.get(NUIManager.class);
GameEngine engine = CoreRegistry.get(GameEngine.class);

Now, as the LWJGLInput class is an engine's subystem, this preUpdate() method gets called every frame, from the main loop. While I doubt it would be a significant optimization, would I be right in saying that perhaps these variables should be private members updated every frame rather than method-local variables recreated every frame?

3) Again in TerasologyEngine.mainLoop() there's the code:

if (currentState == null) {
shutdown();
break;​
}

The question is: what sets currentState to null? cleanup() does but is called after the main loop is interrupted. switchState() cannot, as calling init() on a null object would trigger a NPE? So, I'm wondering if the shutdown() call in this block is ever reachable.
 

Florian

Active Member
Contributor
Architecture
About 1.) The comment is older. So maybe it's a good idea to check which value is better before changing either the comment or the constant

About 2.) I think it is a good change

About 3.) Yeah looks like dead code to me too
 

manu3d

Active Member
Contributor
Architecture
Thanks Florian.

Regarding question 1, what's "better" in this context? If I'm interpreting this correctly the 100ms value would reduce the maximum delay from the time the user bring Terasology to the foreground and the time Terasology starts responding. In this case I'd think 100ms is better?

Regarding question 2, looking around in the rendering code I'm noticing quite a few instances of variables being created and initialized within loops, including methods that are called indirectly by the main loop. I'm wondering if this is due to something I don't know about compiler efficiency, that is, perhaps compilers are smart enough to move the instantiation of method-local variable outside of methods and loops?

Regarding question 3, I'll take it out, we might get a couple more frames per hour. =D
 

Florian

Active Member
Contributor
Architecture
Regarding point 1: Yes I also assume that it influences the time til it responses. I suggest you start terasology and try it out to check if it is indeed so. If yes I would recommend to replace the 100 by a constant and explain in the javadoc of the constant why it is 100 and not higher (noticable delay) or lower (resource consumtion ?). Maybe you also want to check why the value is not smaller. Maybe 20 ms might be even a better value, as I can't image that it really helps reducing CPU usage due to the number of background threads.

Regarding 2: The map could be modified at the next iteration, so there is nothing a compiler can optimize. (btw. the most optimization in java is done at runtime. It's really amazing what the JVM optimizes away). The calling of the code comes propably just from lazyness of the developer who wrote the code and just needed the value there.

Regarding 3: I think it's even less than 1 frame per year, but it saves another developer time when he does not have to wonder about it :)

Btw. a good way to find out the reason for lines it to use "git blame" or the IDE equivalent called annotations. In Intelij you can activate them by right clicking on the left part of the code editor. They tell you which line got added by which commit. If a commit is small and explains the reason for the change (good commits do that) then you will know why a certain code line got added or modified. You can also tell that way which code line is older and which might be outdated.
 

manu3d

Active Member
Contributor
Architecture
Regarding point 1: will do.

Regarding point 2: ok, if I see that kind of situation I'll try and correct it as I go along, on the sidelines of more important changes.

Regarding point 3: very good point. =)

I activated IntelliJ annotations just a few days ago for the first time but I didn't realize I could get the commit's text. I'll give it a more in-depth run next time. Thank you.
 

Cervator

Org Co-Founder & Project Lead
Contributor
Design
Logistics
SpecOps
Unrelated note: You two need to get some avatars! ;)
 
Top