Hi, lets enter here some Optimization Tips for 4k as a quick overview:
2 must-sees first:
http://www.java4k.com/index.php?action=view&page=getstarted (Java4k Homepage links)
http://www.indiespot.net/app/java-four-kay (Compile&Shrink)
Here some findings:
Code that looks “Optimized” and short often does not result in a smaller jar/pack200.
one example:
using
Version1:
Color c1=new Color(200,100,0);
looks worse than
Version2:
Color c1=new Color(0xc86400);
but when defining many colors
the first version is actually better when the color-parameters repeat,
Color c1=new Color(200,100,0);
Color c2=new Color(100,200,0); //good
(but: Color c2=new Color(102,203,0); //not good)
I think the reason is, that while the first version writes a shorter bytecode, the
second version is easier for the compressor to find a pattern in. So contracting information
into less bytes already in the sourcecode can render the efficiency of the packer useless.
another example:
to store a bytearray inside a string, where each value was between 1 and 10
I tried to compress it into UTF characters
“\u24fe\u21ee” -> storing 4 entries per character, eg 2 entries per byte
where my earlier version used a primitive use of simple character for each entry
“ABCABAAC” -> storing 1 entry per byte
In the end the second version compressed much better.
I think because the packer uses only “full” bytes to build its dictionary.
A quick way to test changes is to use an Hex Editor for example, to see the size of the classfile,
and compare it on the byte-level if any fancy code-constructions are not resulting in the same output as the “primitive” version.
a > b is not shorter than a >= b … so dont worry using >= <= etc. in comparissons/loops when needed
you can use a decompiler to see your own results, after obfuscation.
This really helps in some cases to see what was obfuscated away, and does not need improvement in
the source.
a last resort can always be to to “switch” positions of codelines, where the order does not matter.
The packer might find a better pattern in the new version.
This could be automated.
(only usefull at the very final packing up of the game, small changes can render the result useless)
more: