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  
  premature optimization, etc  (Read 2077 times)
0 Members and 1 Guest are viewing this topic.
Offline EnderGT

Jr. Member
**

Posts: 98



« on: 2008-12-05 11: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 Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #1 on: 2008-12-05 11: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

Jr. Member
**

Posts: 98



« Reply #2 on: 2008-12-05 11: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! Go get 'em!
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #3 on: 2008-12-05 12: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

Jr. Member
**

Posts: 98



« Reply #4 on: 2008-12-05 12: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 Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #5 on: 2008-12-05 12: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

Jr. Member
**

Posts: 98



« Reply #6 on: 2008-12-05 12: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 Kernel
*****

Posts: 1866
Medals: 5


falling into the abyss of reality


« Reply #7 on: 2008-12-05 15: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)
Offline EnderGT

Jr. Member
**

Posts: 98



« Reply #8 on: 2008-12-05 15: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 Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #9 on: 2008-12-05 16: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]
  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.13 seconds with 22 queries.