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 (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  
  Optimization Questions  (Read 2035 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2009-01-04 21:47:02 »

As my game is already a bit too big and I've still got a couple things to add, I thought I'd ask a few optimization questions (seeing as this is my first 4k attempt). I know how to program in Assembly, so I'm keeping some of that in mind (like what seems like it will take more jumps, labels, etc.), but I don't know how to view the Java Assembly-ish byte code I've seen people post sometimes. How do I do that?

1) Which of the following take up the least space?
1  
2  
myGuyX += 1000;
myGuyY += 1000;

or
1  
2  
3  
int change = 1000;
myGuyX += change;
myGuyY += change;

or
1  
2  
myGuyX += 100*10;
myGuyY += 100*10;

or
1  
2  
3  
int change = 100*10;
myGuyX += change;
myGuyY += change;

Or if none of those are the best way, what is? I just noticed that in my program I have several numbers typed multiple times, especially 1024. Will storing it be better or worse for the class size? What about adding in an operation so that both operands are less than 256?

2) Using java.shape.contains() versus a simple bounding box check (i.e. x1 <= x2 + width2 && x1 + width1 >= x2 && y1 <= y2 + height2 && y1 + height1 >= y2); which is smaller? I notice in the class file any method I call ends up having its string stored in the class file, as well as a string of the path of the class that has that function. That's often like 20 letters for one function. In general, if I can do a bunch of operations (same goes for Math.min and Math.max) does it make more sense to do that instead of calling the function?

3) Does multiple if statements versus if/else statements save any space? (i.e. if (a <= 0) {} if (a > 0 && a <= 10) {} instead of if (a <= 0) {} else if (a <= 10))

4) What's the smallest way of doing loops? for vs while vs do/while. I'm doing typical array access, so I'm using for loops. I was originally using do/while because someone had mentioned that was cheaper, but it was confusing and I had a lot of looper variables sticking around.

5) Right now I'm using all Java's own drawing methods, namely fillOval, fillRect, etc. This means I don't need to import or use ImageIO, but it also means I'm drawing a lot of ovals. Here is the code for drawing the character:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
//Draw the character.
if (i == 0)
{
   g.setColor(Color.WHITE);
}
else
{
   g.setColor(Color.GRAY);
}
               
g.fillOval((int)fieldXs[i]-5,(int)fieldYs[i]-6,11,13);
g.fillOval((int)fieldXs[i]-3,(int)fieldYs[i]-12,6,6);
if (walkStep || i != 0)
{
   g.fillOval((int)fieldXs[i]-8,(int)fieldYs[i]-6,3,15);
   g.fillOval((int)fieldXs[i]+5,(int)fieldYs[i]-6,3,12);
   g.fillOval((int)fieldXs[i]-4,(int)fieldYs[i]+2,3,13);
   g.fillOval((int)fieldXs[i]+1,(int)fieldYs[i]+2,3,16);
}
else
{
   g.fillOval((int)fieldXs[i]-8,(int)fieldYs[i]-6,3,12);
   g.fillOval((int)fieldXs[i]+5,(int)fieldYs[i]-6,3,15);
   g.fillOval((int)fieldXs[i]-4,(int)fieldYs[i]+2,3,16);
   g.fillOval((int)fieldXs[i]+1,(int)fieldYs[i]+2,3,13);
}

As you can see, I've got a lot of coordinate stuff in there and arbitrary numbers, as well as calling 10 different methods and two sets of if/else statements. Drawing an image would be one single g.drawImage and would have less if statements (potentially). But, I would need a try/catch and ImageIO.read, as well as a small and well compressed image. Can those get small enough to make it worth it? I'm also devoting 8 lines of code (6 draws and 2 color changes) to draw the ghosts.

6) What's the cost of a few if statements versus another for loop? Currently I'm doing all drawing and logic in the same loops, and that involves a lot of if (i == 0) {} for things that should only happen once. This is saving me on lines of code, but I'm wondering it's actually costing me more bytes.

7) How much space does typecasting take up?

8 ) Is there a reason to use floats versus doubles? If it's cheaper to use floats, will it still be cheaper even though I have to typecast sometimes?

9) How costly is array access? Not, very, right? It wouldn't make sense to assign a temporary variable to hold the data if I do array access say 4 times, would it?

10) Is it cheaper to say array.length or the length number if I know what it is? Like int[] arr = new int[500]; Is for (int i = 0; i < arr.length; i++) cheaper or is for (int i = 0; i < 500; i++) cheaper?

That's all I can think of for now. Thanks a lot for your answers!

See my work:
OTC Software
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #1 - Posted 2009-01-05 01:59:41 »

That's loads of questions! I'll try answering, but keep in mind it's all AFAIK:

1) Which of the following take up the least space?
Almost certainly this one:
1  
2  
myGuyX += 1000;
myGuyY += 1000;

It's a very good idea to try to write repetitive code, as that helps the zipping.

2) Using java.shape.contains() versus a simple bounding box check (i.e. x1 <= x2 + width2 && x1 + width1 >= x2 && y1 <= y2 + height2 && y1 + height1 >= y2); which is smaller?
That depends on how often you use it and what the check is. If it's just a single line of code, doing it manually is a LOT smaller as method calls add a whole bunch of garbage the constant pool.
The flip side is that once you've used a function call, using it again later on is much cheaper, possibly cheaper than the manual intersect check.

3) Does multiple if statements versus if/else statements save any space? (i.e. if (a <= 0) {} if (a > 0 && a <= 10) {} instead of if (a <= 0) {} else if (a <= 10))
Shouldn't matter much at all. You could save a few bytes by fiddling around with if statement logic, but it's highly situational.

4) What's the smallest way of doing loops? for vs while vs do/while. I'm doing typical array access, so I'm using for loops. I was originally using do/while because someone had mentioned that was cheaper, but it was confusing and I had a lot of looper variables sticking around.
.. no idea, sorry. Cheesy

5) Right now I'm using all Java's own drawing methods, namely fillOval, fillRect, etc. [...] But, I would need a try/catch and ImageIO.read, as well as a small and well compressed image. Can those get small enough to make it worth it?
This totally depends on how fancy graphics you want. If you want bitmap quality graphics, you're either going to have to do pixel level synthesis, or load bitmap data somehow.
The upside of generated graphics is that it's very cheap to add variations of the graphics, like having ghosts get bigger with each level.
I usually go for pixel generated graphics.

6) What's the cost of a few if statements versus another for loop?
That depends on how similar the loops would be if separated. Try it (and zip the results), and see if it's worth it.
I usually have a single loop and try to optimize away special cases.

7) How much space does typecasting take up?
Depends on what type it is. Most primitive conversions is a single byte per cast.
Casting objects requires the class name to be in the constant pool (of course), and a couple of bytes.

8 ) Is there a reason to use floats versus doubles? If it's cheaper to use floats, will it still be cheaper even though I have to typecast sometimes?
long version: doubles and longs take up two slots in the stack, meaning the compiler has to use different instructions for certain actions (dup and stack reordering). These instructions aren't bigger than the original ones, but if you're mixing the two types, zipping might get less efficient.
short version: try it! My guess is that whatever causes the least typecasts will win.

9) How costly is array access?
More than a local variable lookup, but repeated code usually gets zipped away, so you usually don't gain much from storing an array entry.

10) Is it cheaper to say array.length or the length number if I know what it is?
It's cheaper to say the number.



Some links:
Jasmin java assembler
Jasper java disassembler (or just use "javap - c myclass.class", but that output is pretty horrible)

Play Minecraft!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #2 - Posted 2009-01-05 02:25:20 »

>But, I would need a try/catch[...]

throws Exception Wink

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #3 - Posted 2009-01-05 02:48:34 »

Hehe, posted just after the ones above - there's probably quite abit of duplication  Wink

As my game is already a bit too big and I've still got a couple things to add, I thought I'd ask a few optimization questions (seeing as this is my first 4k attempt). I know how to program in Assembly, so I'm keeping some of that in mind (like what seems like it will take more jumps, labels, etc.), but I don't know how to view the Java Assembly-ish byte code I've seen people post sometimes. How do I do that?

1) Which of the following take up the least space?
1  
2  
myGuyX += 1000;
myGuyY += 1000;

or
1  
2  
3  
int change = 1000;
myGuyX += change;
myGuyY += change;

or
1  
2  
myGuyX += 100*10;
myGuyY += 100*10;

or
1  
2  
3  
int change = 100*10;
myGuyX += change;
myGuyY += change;

Or if none of those are the best way, what is? I just noticed that in my program I have several numbers typed multiple times, especially 1024. Will storing it be better or worse for the class size? What about adding in an operation so that both operands are less than 256?

Javac evaluates composition of constants during compilation, therefore 10*100 will be evaluated to 1000.
This means code examples a & c will generate identical bytecode, as will examples b & d.
In addition, because your intermediary 'int change' is assigned to only once, and given a constant value, most good optimisers will entirely eliminate it.
If you changed it's declaration to 'final int change = ....' javac too would make this same optimisation. (this is good programming practice in any case)
Therefore, all 4 examples should be considered as generating identical bytecode when using the right compilation tools.

Quote
2) Using java.shape.contains() versus a simple bounding box check (i.e. x1 <= x2 + width2 && x1 + width1 >= x2 && y1 <= y2 + height2 && y1 + height1 >= y2); which is smaller? I notice in the class file any method I call ends up having its string stored in the class file, as well as a string of the path of the class that has that function. That's often like 20 letters for one function. In general, if I can do a bunch of operations (same goes for Math.min and Math.max) does it make more sense to do that instead of calling the function?

yep, every reference to an external class member requires a number of constant pool entries.
It becomes a trade-off based upon how much work the method does for you, and how many invocations are made.

Quote
3) Does multiple if statements versus if/else statements save any space? (i.e. if (a <= 0) {} if (a > 0 && a <= 10) {} instead of if (a <= 0) {} else if (a <= 10))

Generally no.

Quote
4) What's the smallest way of doing loops? for vs while vs do/while. I'm doing typical array access, so I'm using for loops. I was originally using do/while because someone had mentioned that was cheaper, but it was confusing and I had a lot of looper variables sticking around.

Just make sure the logic done in the loop counter is equivalent, and it should result in the same bytecode.

Having said that while & for loops contain an implicit unconditional jump at the end of the loop. do/while loops do not have this (because the loop condition is already at the end of the loop), and therefore will be smaller.
Ofcourse, a do/while won't always fit with the logic performed by the loop. (for & while loop 0 or more times, do/while loops 1 or more times)

e.g.

while sudo-code

1     X > Y ? GOTO 5
2     // statement in loop
3     // statement in loop
4     GOTO 1
5     // code beyond loop

do/while sudo-code

1 // statement in loop
2 // statement in loop
3 // X <= Y ? GOTO 1
4 // code beyond loop

Quote
5) Right now I'm using all Java's own drawing methods, namely fillOval, fillRect, etc. This means I don't need to import or use ImageIO, but it also means I'm drawing a lot of ovals. Here is the code for drawing the character:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
//Draw the character.
if (i == 0)
{
   g.setColor(Color.WHITE);
}
else
{
   g.setColor(Color.GRAY);
}
               
g.fillOval((int)fieldXs[i]-5,(int)fieldYs[i]-6,11,13);
g.fillOval((int)fieldXs[i]-3,(int)fieldYs[i]-12,6,6);
if (walkStep || i != 0)
{
   g.fillOval((int)fieldXs[i]-8,(int)fieldYs[i]-6,3,15);
   g.fillOval((int)fieldXs[i]+5,(int)fieldYs[i]-6,3,12);
   g.fillOval((int)fieldXs[i]-4,(int)fieldYs[i]+2,3,13);
   g.fillOval((int)fieldXs[i]+1,(int)fieldYs[i]+2,3,16);
}
else
{
   g.fillOval((int)fieldXs[i]-8,(int)fieldYs[i]-6,3,12);
   g.fillOval((int)fieldXs[i]+5,(int)fieldYs[i]-6,3,15);
   g.fillOval((int)fieldXs[i]-4,(int)fieldYs[i]+2,3,16);
   g.fillOval((int)fieldXs[i]+1,(int)fieldYs[i]+2,3,13);
}

As you can see, I've got a lot of coordinate stuff in there and arbitrary numbers, as well as calling 10 different methods and two sets of if/else statements. Drawing an image would be one single g.drawImage and would have less if statements (potentially). But, I would need a try/catch and ImageIO.read, as well as a small and well compressed image. Can those get small enough to make it worth it? I'm also devoting 8 lines of code (6 draws and 2 color changes) to draw the ghosts.

If you can get away without using images, you should. As you've already mentioned, it avoids pulling in a load of api methods.
Though for future reference it's worth knowing you can just declare main as 'throws Throwable', rather than trying to handle any Exceptions.
Regarding your specific example, you should eliminate all of the repeated accesses to the array - accessing an array element requires significantly more bytecode than a local variable.
You should use the ternary operator for your setColor call -> "g.setColor(i==0?Color.WHITE:Color.GRAY);"
Also, if you are accessing more than 1 of the named Color instances, you should change to using the constructor "new Color(int)", as each reference to a named Color instance increases the constants pool size.

Quote
6) What's the cost of a few if statements versus another for loop? Currently I'm doing all drawing and logic in the same loops, and that involves a lot of if (i == 0) {} for things that should only happen once. This is saving me on lines of code, but I'm wondering it's actually costing me more bytes.

As a general rule, special-casing the 'i==0' will give you smaller bytecode.

Quote
7) How much space does typecasting take up?

Depends what you are typecasting between?
primitive types? tiny, 1 byte.
object types? more. Though I doubt you will be doing this often in a 4kb app.

Quote
8 ) Is there a reason to use floats versus doubles? If it's cheaper to use floats, will it still be cheaper even though I have to typecast sometimes?

Wide primitive constants (double & long) occupy twice the number of bytes in the constants pool, occupy two constants pool indexes, and can need larger instructions to access them. 3 good reasons to avoid them if possible.
Ofcourse, as with many aspects of 4kb it's a trade-off, because most of the Java API methods (java.awt.geom, java.lang.Math etc) operate on doubles.
So, if you use floats you reduce constants pool size, but end up having to add bytes for casting to & from any API methods you use.

Quote
9) How costly is array access? Not, very, right? It wouldn't make sense to assign a temporary variable to hold the data if I do array access say 4 times, would it?

The exact number of bytes can vary tremendously, but access to array elements is always larger than local variables.
If you read from an element more than once, I'd suggest you store it in a [final] local variable.
It's much easier for an optimiser to undo any overzealous common subexpression elimination you've put in, than it is for the optimiser to work it out itself.
Having said that, it gets abit more complicated if you are writing to the array elements as well as reading.

Quote
10) Is it cheaper to say array.length or the length number if I know what it is? Like int[] arr = new int[500]; Is for (int i = 0; i < arr.length; i++) cheaper or is for (int i = 0; i < 500; i++) cheaper?

If the length value can be evaluated to a constant, it will (for all but the most fantastical of situations!) be smaller to use the constant value.
If the length is a variable determined at runtime, the answer isn't so clear.
Quote
That's all I can think of for now. Thanks a lot for your answers!

The only thing left to say I think, is not to worry about micro-optimisations until your game is done.
If you try and keep your game below 4kb every time you add a new feature, you will waste hours.

Write your game with optimal size in-mind, but don't go back and optimise anything until the games fundamentals are done.

:edit:

Actually, after reading Markus's post, there is 1 more thing to say.
The unpredictability of zipping fcuks up alot of micro-optimisations, another reason to avoid doing them until the very end! Wink

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #4 - Posted 2009-01-05 03:23:17 »

Actually, after reading Markus's post, there is 1 more thing to say.
The unpredictability of zipping fcuks up alot of micro-optimisations, another reason to avoid doing them until the very end! Wink
Yeah, I've been finding this a lot. I've left it until the end to do the more crazy/unreadable optimisations and I'm finding that most of them either don't have any effect or have unpredictable effects after running moogie's compacting tool. In particular I've been finding that removing/reducing the number of float->int casts has had no effect, and neither has reusing local variables. Seems that the optimisers are pretty good at compacting functionally identical code down to the smallest common representation.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2009-01-05 04:23:05 »

Great helpful advice, thanks all.

My game is now pretty much done and is I think at least 5000 bytes without any optimizers or extra compression. So I'll experiment with all this and see if I can get it down lower.

See my work:
OTC Software
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 (15 views)
2014-07-29 18:09:19

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

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

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

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

pw (40 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 (29 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!