deduplicating lambda methods

Liam Miller-Cushon cushon at
Sat Mar 3 03:00:49 UTC 2018

On Fri, Mar 2, 2018 at 5:17 PM, Brian Goetz <brian.goetz at> wrote:

> I think AST comparison is likely to be easier and more effective. And it
> doesn't have to be perfect; if it gets fooled by occasional differences,
> that's OK, as long as it doesn't merge lambdas that are actually
> different.  And you don't have the generated code until later, when its
> likely harder to do the merging.  There's a whole pass in the compiler
> pipeline for lambda method desugaring (LambdaToMethod), so there's an
> obvious place to do this transformation.

That's where I started for the prototype. The appeal of bytecode is that
it's easier to implement value equality, and it supports deduplicating
different source that lowers to the same bytecode. However the AST
comparison is easy enough to support (just a little more verbose), and the
return on handling non-identical duplicates seems fairly small.

> Is there any trickiness with capturing lambdas?  I don't think so -- I
> think we can merge these too, although I suspect the return on that effort
> is lower.  I'll bet the most common case is lambdas like e -> e, x ->
> System.out.println(x), etc.

I don't think so either, and depending on where in lambda desugaring the
deduplication happens we might get this for free.

> Maybe you could pull together data on how often lambdas are duplicated
> within a source file, and of the duplicated lambdas, what percentage are
> stateless and non-serializable?

Will do, thanks.

On Fri, Mar 2, 2018 at 6:22 PM, John Rose <john.r.rose at> wrote:

> On Mar 2, 2018, at 6:14 PM, Brian Goetz <brian.goetz at> wrote:
> >
> > This seems more like a candidate for a jlink-time optimization.
> Yes, good, although the very first time it is used in any large body like
> java.base it will discover the same basic top five. Those might as well be
> done unconditionally.  In which case the first batch can be done without de
> ending on jlink.

I'll see if I can collect some data about what the top N lambdas look like.

More information about the amber-dev mailing list