Lambda overhead

269 views
Skip to first unread message

Paul Stockley

unread,
Apr 15, 2016, 9:11:33 AM4/15/16
to GWT Contributors
Am I right in saying that java 8 lambda's under the covers is implemented as an anonymous inner class? Is this also true for SAM interfaces annotated with JsFunction annotated interfaces?

Roberto Lublinerman

unread,
Apr 15, 2016, 10:57:00 AM4/15/16
to google-web-tool...@googlegroups.com
Yes, right now all lambdas are implemented as anonymous inner classes, even the ones implementing JsFunction interfaces.

On Fri, Apr 15, 2016 at 6:11 AM, Paul Stockley <pstoc...@gmail.com> wrote:
Am I right in saying that java 8 lambda's under the covers is implemented as an anonymous inner class? Is this also true for SAM interfaces annotated with JsFunction annotated interfaces?

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/369a5094-7e64-4351-b1e5-16ff5a3a2468%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Stockley

unread,
Apr 15, 2016, 11:25:41 AM4/15/16
to GWT Contributors
Are there plans to optimize this in the GWT2.x version in the future? 


On Friday, April 15, 2016 at 10:57:00 AM UTC-4, Roberto Lublinerman wrote:
Yes, right now all lambdas are implemented as anonymous inner classes, even the ones implementing JsFunction interfaces.
On Fri, Apr 15, 2016 at 6:11 AM, Paul Stockley <pstoc...@gmail.com> wrote:
Am I right in saying that java 8 lambda's under the covers is implemented as an anonymous inner class? Is this also true for SAM interfaces annotated with JsFunction annotated interfaces?

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-contributors+unsubscribe@googlegroups.com.

Roberto Lublinerman

unread,
Apr 15, 2016, 12:03:12 PM4/15/16
to google-web-tool...@googlegroups.com
It should not be hard to make JsFunction lambdas more terse, but there are no plans for GWT 2.x.

On Fri, Apr 15, 2016 at 8:25 AM, Paul Stockley <pstoc...@gmail.com> wrote:
Are there plans to optimize this in the GWT2.x version in the future? 

On Friday, April 15, 2016 at 10:57:00 AM UTC-4, Roberto Lublinerman wrote:
Yes, right now all lambdas are implemented as anonymous inner classes, even the ones implementing JsFunction interfaces.
On Fri, Apr 15, 2016 at 6:11 AM, Paul Stockley <pstoc...@gmail.com> wrote:
Am I right in saying that java 8 lambda's under the covers is implemented as an anonymous inner class? Is this also true for SAM interfaces annotated with JsFunction annotated interfaces?

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/9fce9c7d-b956-4fcf-b16a-8d25ffb69e57%40googlegroups.com.

Paul Stockley

unread,
Apr 17, 2016, 11:40:48 PM4/17/16
to GWT Contributors
Given that it will be realistically a couple of years before most large projects could migrate to J2CL, it would be really nice to have a more optimal code generation for lambda's, especially for JsFunction. When 2.8 gets released, I think people will really start taking advantage of existing JS libraries that really heavily use functions. 

Roberto Lublinerman

unread,
Apr 18, 2016, 12:04:24 PM4/18/16
to google-web-tool...@googlegroups.com
We accept patches :) 

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.

Ray Cromwell

unread,
Apr 18, 2016, 12:34:26 PM4/18/16
to google-web-toolkit-contributors
Seems to me this'll be tricky to pull off. The GWT compiler has no
notion of captured scope. If you want to create a class type that
represents a lambda, but which doesn't actually get output as a class,
you'd have to change many many parts of the compiler.

I think perhaps the best thing you could do would be a kind of
'peephole' optimization pass. After all Java optimization passes have
run, and after JS has been generated, go through using pattern
matching to find "new generatedlambda(captured variables)" and replace
it with "makeLambda(classtype, castMap, classLiteral, function(...) {
body of single abstract method })". Then hope the JS dead code pruning
removes the unused inner class.

I think if you try to model this in the Java AST it would impact too much stuff.
> https://groups.google.com/d/msgid/google-web-toolkit-contributors/CAC7T7g%3DkH9YQjA_P02ibv-Fg4u-gRW6s4Ojm6S2KgTK0KDOYmQ%40mail.gmail.com.

Roberto Lublinerman

unread,
Apr 18, 2016, 12:49:42 PM4/18/16
to google-web-tool...@googlegroups.com
The scheme I had in mind does not modify much the Java AST representation but is more in the lowering to the JsAST, basically it boils down the the following:. 
1. You create the lambda body as a static method of the class where it appears. This static method has all captures as parameters (thiis is very similar as we are doing today).
2. Synthesize an anonymous inner class that implement the JsFunciton (also same as we are doing now). The compiler in the java phase sees this as an innerclass.
3. Emit the following code for new JsFuncitonimplementation(capture1, capture2) as function(par1, par2, parn) -> {Class.lambdaFunctionImplementation(capture1, capture2, par1, par2, parn) }. 
4. Do not emit any code for the anonymous inner class.

This function will behave as if it was a native function passed from JS, and object methods will work in the same way. Of course the devil is in the details.


Ray Cromwell

unread,
Apr 18, 2016, 12:55:03 PM4/18/16
to google-web-toolkit-contributors
That seems similar to my proposal, only you're doing it in GenJsAST.
You'll still need to the makeLambda() trick to allow it to work as a
regular obejct as well, with hashCode()/equals()/getClass()
properties, as well as castMap installed. But if you use static method
delegation, the size won't be as ideal if the static method doesn't
inline.

You want something like x -> 42 + capture to compile to
makeLambda(function(x) { return 42 + capture }, castMap, classLit),
not function(x) { return Class.foo(capture, x); } do you not?


On Mon, Apr 18, 2016 at 9:49 AM, 'Roberto Lublinerman' via GWT
> https://groups.google.com/d/msgid/google-web-toolkit-contributors/CAC7T7gngwS13pEuSHWSMZAtcScTPwuH8tKRNGT7fQMBxa_S2jQ%40mail.gmail.com.

Roberto Lublinerman

unread,
Apr 18, 2016, 1:17:49 PM4/18/16
to google-web-tool...@googlegroups.com
The point is that for JsFunctions you DON'T actually need to do the makeLambda(). JsFunction was designed to be able to pass JS functions to JAVA so there is a code path for that and we can exploit it for JsFunction lambdas.

So x -> 42  + capture will be represented in the JAva AST as
class X {         
  {
         .....  new X$0(capture);
  }
  // Synthetic lambda method
  int lambda_f(capture, x) {
     return 42 + capture;
  }

// Lambda function implmenetation. Nothing needs to be emitted for this in JS
class X$0 implements JsFunctionInterface {
  capture;
  X$0(capture) {
   this.capture = capture;  
  }

  int m(x) {
     X.lambda_f(this.capture, x);
  }
}


In the JS ast we do

  new X$0(capture)  -->  function(x) { X.lambda_f(capture, x) }

So we don't need makeLambda or anything else; we can just treat JsFunciton implementation as if they were functions passed in from JavaScript where object methods will go through the regular trampoline into the "JSO" implementation.


  

Ray Cromwell

unread,
Apr 18, 2016, 1:22:30 PM4/18/16
to google-web-toolkit-contributors
I understand, but the trampolines cause bloat, and if you're
suggesting treating all non-JsFunction Java8 lambdas as JsFunctions as
far as code-gen is concerned, then you would not be able to make the
following code work:

foo(Callable x) { bar(x); }
foo(Runnable x) { bar(x); }

bar(Object x) { if (x instanceof Callable) { print("it's a callable");
} else if (x instanceof Runnable) { print("It's a runnable"); } }

You need a castMap for that.


On Mon, Apr 18, 2016 at 10:17 AM, 'Roberto Lublinerman' via GWT
> https://groups.google.com/d/msgid/google-web-toolkit-contributors/CAC7T7gm_uaYNVTAXA8Yzs4QWw8X5jjqPS_SNsfc-5duLThiORA%40mail.gmail.com.

Roberto Lublinerman

unread,
Apr 18, 2016, 1:48:24 PM4/18/16
to google-web-tool...@googlegroups.com
That is why I am saying that it will be easy to do for JsFunctions but due to Java semantics (regular) lambdas are just not plain functions and thus I don't think there is much to gain there. 

I don't think there is much to gain on the regular lambdas. There are 2 different ways we can handle them and reduce a bit of the generated code but I don't think there is a lot of potential ways.
1) do not generate anonymous inner classes, rather have the make lambda factory take all the required parameters, i.e. castmap.
2) create an anonymous innerclass based on the class/interface that is being extended/implemented when a lambda for that is seen. In that way if an interface is used with many lambdas there is only one supporting class instead of many.


Ray Cromwell

unread,
Apr 18, 2016, 1:50:00 PM4/18/16
to google-web-toolkit-contributors
I'm ok with restricting them to @JsFunction java8 lambdas. That's
likely to be the common path for web oriented code for event handling.


On Mon, Apr 18, 2016 at 10:47 AM, 'Roberto Lublinerman' via GWT
> https://groups.google.com/d/msgid/google-web-toolkit-contributors/CAC7T7gmnH3XjO71wNzphXNyByDWn16iSHHF8RbRqMAGDW-%3Dsyw%40mail.gmail.com.

Colin Alworth

unread,
Apr 22, 2016, 2:41:06 PM4/22/16
to google-web-toolkit-contributors
Meta: it would make my year if we could get one of these compiler discussions happening once a week on a public forum like this, both for better basic understanding about why some of these concepts are not trivial, and for better visibility into what else is going on in the process of continuing to improve and grow GWT.


Reply all
Reply to author
Forward
0 new messages