Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
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  
  premature optimization, etc  (Read 2983 times)
0 Members and 1 Guest are viewing this topic.
Offline EnderGT

Junior Member





« Posted 2008-12-05 17:34:08 »

I know this is premature optimization, but the thought occurred to me...

The question is: is it more efficient to have 20 or so single-letter int variables (on the stack), or one int array? Does the array reference cost anything significant that would negate the benefit of removing the 20 or so variable names? In my game, the array(s) will be referenced quite heavily.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-12-05 17:44:00 »

It depends.

Local variables can be placed in registers, which are extremely fast.
int[] are placed in the heap, so in optimal conditions the int[] is in the cache, which is much slower than registers.

There is also the traceoff of maintainability.
Make it work.
Make it stable.
Make it fast.
Make it ugly, yet 2% faster.

Last but not least 'single-letter int variables' are as fast as 'three pages long int variables'.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline EnderGT

Junior Member





« Reply #2 - Posted 2008-12-05 17:47:50 »

It depends.

Local variables can be placed in registers, which are extremely fast.
int[] are placed in the heap, so in optimal conditions the int[] is in the cache, which is much slower than registers.

There is also the traceoff of maintainability.
Make it work.
Make it stable.
Make it fast.
Make it ugly, yet 2% faster.

Last but not least 'single-letter int variables' are as fast as 'three pages long int variables'.
In the case of the game I'm working on right now, speed is unimportant, but the final size is going to be critical. I have so much going on, I'm going to need to squeeze every bit.

I'm just curious if using one int[] would be less bytecode than ~20 individual int variables.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-12-05 18:11:29 »

:| I didn't realize this was the 4K forum... My bad!


In that case, even then the 1 character ints are as small are the fully named local variables.


A simple multiplication function shows what the generated bytecode is like.


When using an int[20], you'd have:
[aload_x + iconst_x] or
[aload_x + bipush + byte] or
[bipush + byte + aload + bipush + byte] to access an element

When using 20 ints, you'd have:
[iload_x] or
[bipush + byte + iload] to access a variable

My guess is that using ints is a bit smaller, until you need to to math on them, where you'd otherwise use a loop.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline EnderGT

Junior Member





« Reply #4 - Posted 2008-12-05 18:24:08 »

In that case, even then the 1 character ints are as small are the fully named local variables.
Bear with me, but the "common knowledge" is to use 1-character names rather than fully named variables, as it results in smaller, more compressible code. Is this untrue?

That being said, my question is really about using 20 int variables vs 1 int[20]. I've never dealt with the bytecode before (a deficiency I should rectify one of these days), so I'm not really sure what I'm seeing in your illustration.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2008-12-05 18:31:26 »

class names and field names are stored in bytecode, so making them shorter results in smaller bytecode

local variable names are not stored in bytecode, so you can make them as long as you want

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline EnderGT

Junior Member





« Reply #6 - Posted 2008-12-05 18:37:48 »

class names and field names are stored in bytecode, so making them shorter results in smaller bytecode

local variable names are not stored in bytecode, so you can make them as long as you want

And this just reinforces that I need to start learning about the bytecode.

Thanks for the info!
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #7 - Posted 2008-12-05 21:11:43 »

Though naming is irrelevant if you are passing your classes through an obfuscator & optimiser, as everyone in this competition should be!

Quote
[iload_x] or
[bipush + byte + iload] to access a variable

The general purpose "iload x" instruction (rather than the special case "iload_[0-3]" shortcuts ) does not take it's parameter (the index of the local variable) from the operand stack, it takes it as a unsigned byte parameter.

Therefore, (in the normal cases) getting a local variable onto the operand stack is either a 1 byte (e.g. iload_0 ) or 2 byte ( iload 0 ) instruction.

Accessing an array element is more complex.
At best it will be a 3 byte instruction (The array is stored in 1 of the first 4 local variables, and you are explicitly accessing an array element from 0 to 5. e.g. "aload_0; iconst_0; aaload" )
Typically however it will be much more than this.


Note, in the above I used the qualifier 'in the normal cases'.
If you have more than 256 live local variables, the compiler will begin using the 'wide' instruction.
This preceeds the iload instruction, making the iload instruction expect 2 bytes for the local variable index rather than just 1.
Therefore, accessing local variables beyond the 256th becomes a 4 byte instruction!

Typically the compiler will shuffle local variables around, so as to make best use of the smaller instructions.
(or is it proguard that does this? I can never remember exactly what optimisations javac performs)

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

Junior Member





« Reply #8 - Posted 2008-12-05 21:18:45 »

Though naming is irrelevant if you are passing your classes through an obfuscator & optimiser, as everyone in this competition should be!
It's not the actual names I was worried about, it was the "space" required for multiple variable names, versus the "space" required for a single variable name and then array accesses.

Thanks to you and Riven for the excellent answers. I won't bother with the array(s) unless I really feel it necessary.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2008-12-05 22:19:56 »

You have to keep in mind that there are no named local variables in bytecode. Local variables can even 'share' the same position on the stack.

Even if you have 300 local variables, but you only declare and discard 4 variables at a time, the compiler can keep all variables within iload_[0..3] (one after another - in time)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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 (18 views)
2014-07-30 21:08:39

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

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

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

E.R. Fleming (34 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 (44 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 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

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