Tuesday, October 17, 2017

Re: Controlling Inling in GWT compiler



On Wed, Oct 18, 2017 at 1:14 PM, Colin Alworth <niloc132@gmail.com> wrote:
For the first one, chaining methods has the unfortunate detail that each method has a return value, which the next object is called on. To my knowledge, there is no optimization in the compiler which currently can undo the presence of the return value, and recognize that the method returns "this" (or a param, some other easily aliased value) as a special case.

Ok. I think I have been spoilt by working on the JVM for so long. I just assumed that GWT and/or Closure could do escape analysis and inline non-escaping objects into the current scope. I assume this is not the case then? It would explain the resulting code.

Lets assume that "str" was only one char, since we are compiling after all, 40 chars for the "idiomatic" js version, 37 chars for staticified version, 50 for the "inlined" version.

Hmm ... I guess I was too down in the trenches to think high level enough. I like this idea. In my context the following is the lay of the land

Current Java Input: 
new InputBuilder().type( "checkbox" ).className( "foo" ).child( "Hello" ).build()

Naive Javascript output (71 chars)
React.createElement('input',{type:'checkbox',className:'foo'},'Hello')

Current GWT output: (lots)
function up(a){var b,c;c=a.b;b=a.a;return $wnd.React.createElement.apply(null,[hw,c].concat(b))}
function vp(a,b){a.a.concat(b);return a}
function wp(a){a.b['className']='foo';return a}
function xp(a){a.b['type']='checkbox';return a}
a=up(vp(wp(xp(new yp)),'Hello'))

Non-reusable GWT output: (82 chars)
function wp(a){a.b['className']='foo';return a}
a=up(vp(wp(xp(new yp)),'Hello'))

Hypothetical Less Naive ES3 Javascript output (45 chars)
ab(dc,{'type':xs,'className':'foo'},'Hello')

Hypothetical Less Naive ES6 Javascript output (36 chars)
ab(dc,{[eg]:xs,[gg]:'foo'},'Hello')

So even if you strip the potentially reusable code out of the current GWT output the non reusable chunk is still 82 characters which is 9 characters more than the most naive javascript implementation. However a more optimized javascript implementation that aliases the methods and constants comes in at 36 chars. However I think that uses some ES6 features ([gg] as key). 

I still suspect (having not experimented with it) that the "return this" aliasing will still confuse matters, at least without deliberate code in closure to handle it. At least with strings, it isn't smart enough:

Right.
 
I played with a similar optimization in GWT a few years ago, but ended up not finishing it. It mostly dealt with trying to deal with compile-time strings and avoiding runtime regular expressions on them, but I abandoned it. If there is enough interest in this, I think I at least have the working knowledge to give it a shot... but lets be sure that we get something out of it.

To be perfectly honest I have no idea on the state of either the GWT or closure optimizer so could not rightly say but I suspect that without an escape analysis this may be a very difficult optimization to get right and probably a lot of work ;) I remember spending weeks  (months?) trying to get some similar optimizations working in a past life and I mostly failed ! But if you want to try don't let me stop you.

create({a:'asdf',b:1,c:false});
create(c(b(a({},'asdf'),1),false);
Literal is 31 bytes, chained is 35, so two bytes per setter (plus the cost of the setter method, which can then be reused). Might not be worth more than a few hours of time, but then again, the work could lead to other unexpected benefits...


I think the optimization would offer a fairly significant improvement but I also think it is a massive amount of work. Maybe time would be better spent elsewhere.

Anyhoo interesting idea - thanks for your reply. 

--
Cheers,

Peter Donald

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment