konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.64k stars 265 forks source link

More information about Optimization Levels #224

Open WhiteTrashLord opened 8 years ago

WhiteTrashLord commented 8 years ago

Could you please give more information about the Optimization Levels? Currently I can only work with the version 0.4.3 which doesn't have this new settings.

In my case TeaVM would be a perfect if it would produce more efficient code.

Even very small code generates quite big Javascript files because there is a lot of code like this:

"java.util.Comparator",E,[],0,0,[],[],GC,"java.lang.String$1",E,[TB],0,0,[],["a",function(){DD(this);}],W,"java.lang.annotation.Annotation",E,[],0,0,[],[],J,"java.lang.AbstractStringBuilder",E,[DB,U],0,J_$clinit,['KC','DC','XD'],["a",function(){KC(this);},"c",function(a){DC(this,a);}],XB,"java.lang.Appendable",E,[],0,0,[],[],PC,"java.lang.StringBuilder",J,[XB],0,0,[],["a",function(){YE(this);}],FB,"java.io.InputStream",E,[AB],0,0,[],["a",function(){CE(this);}],IC,"java.lang.ConsoleInputStream",FB,[],0,0,[],

A high optimation level will reduce this kind of code? Or you won't do anything about it because you don't think that this is important?

konsoletyper commented 8 years ago

Could you please give more information about the Optimization Levels?

They are not about code size, but about debug experience/speed tradeoff. But this may depend on different circumstances. For example, with FULL optimization level WebAssembly backend produces slightly less code, which is not true for JS backend. The general rule is:

Here the the full table for now (this can be changed):

Currently I can only work with the version 0.4.3 which doesn't have this new settings.

You can build TeaVM from sources and work with the most recent version.

git clone https://github.com/konsoletyper/teavm.git
cd teavm
mvn clean install -DskipTests

A high optimation level will reduce this kind of code?

No, you can't make TeaVM to strip this code. It's even impossible to remove this code completely, since without it TeaVM won't work at all.

Or you won't do anything about it because you don't think that this is important?

I don't know whether it's really important for you. You are speculating about "TeaVM generating too large files". You have to cease speculating and ask several questions.

  1. What is the size of .js file generated by TeaVM?
  2. What is the size of $rt_metadata fragment?
  3. What is the "huge size"? Do you have some limitations in your business requirements? Is that the file size or load time?
  4. Did you try gziping JS file? Is the compressed file still too huge?
  5. Did you compare with alternative compilers (such as GWT, JSweet, Dragome) on the same codebase?
WhiteTrashLord commented 8 years ago

Thank you for your answer. I tried it with this very simple code:

 public static void main() {
       Window.alert(Window.current());
}

I was finally able to compile it by changing some of the TeaVM code. Using separate mode it generates a 17 kB Javascript file and the 30 kB runtime.js.

TeaVM is the best compiler among the compilers I have tested. So I think I will use it anyway. But it would be even better if it would produce even smaller Javascript output.

konsoletyper commented 8 years ago

Why optimize TeaVM for "Hello World" writers? It's possible to optimize TeaVM a little in this direction, but the efforts won't worth spending.