Skip to main content
9 events
when toggle format what by license comment
Jun 16, 2020 at 10:01 history edited CommunityBot
Commonmark migration
Nov 23, 2018 at 14:15 history edited h.j.k. CC BY-SA 4.0
updated to https link
Nov 22, 2018 at 17:17 comment added entonio We get that the ways method references and lambdas are created and used are internally different. What I don't get is why, instead of burdening the language with method references, the compiler doesn't just optimise-as-references those lambdas that are equivalent to references from the end-user's perspective. If it would have been asking too much too soon, well, then the optimisation could have been left for later on. I can't see any use for method references except as optimised alternatives to lambdas, and we're repeatedly told that kind of micro-optimisation isn't for us to care about.
Nov 4, 2016 at 15:34 comment added Walfrat @Ajax What about this one ? blog.soat.fr/2015/12/benchmark-java-lambda-vs-classe-anonyme.
Jul 21, 2016 at 11:20 comment added Ajax Last spammy comment... The dynamic class generation is pretty heavyweight. Still probably better than loading an anonymous class in the classloader (since the heaviest parts are only done once), but you would be really surprised just how much work goes into creating a lambda instance. It would be interesting to see real benchmarks of lambdas versus method references versus anonymous classes. Note that two lambdas or method references which reference the same things, but at different places create different classes at runtime (even within the same method, right after each other).
Jul 21, 2016 at 11:17 comment added Ajax Presumably a private method also won't need virtual dispatch, whereas anything more public will need to close over the instance (via a generated static method) so that it can invokevirtual on the actual instance to make sure it notices overrides. TL;DR: Method references close over less arguments, so they leak less, and require less dynamic code generation.
Jul 21, 2016 at 11:15 comment added Ajax ...In addition to closing over variables, lambdas in particular also create a static method that is injected into the declaring class so the created lambda has something to call to map into the function(s) you want to invoke. A method reference with an instance qualifier will take that instance as a parameter to this method; I haven't tested if a this::method reference in a private class skips this closure, but I have tested that static methods do, in fact, skip the intermediate method (and just create an object to conform to the invocation target at the call site).
Jul 21, 2016 at 11:10 comment added Ajax This is the most relevant answer, as it actually touches on the internal machinery needed to create a lambda. As someone who actually debugged the lambda creation process (in order to figure out how to generate stable ids for a given lambda / method reference so they can be removed from maps), I found it quite surprising just how much work is done to create an instance of a lambda. Oracle / OpenJDK use ASM to dynamically generate classes that, if necessary, close over any variable referenced in your lambda (or the instance qualifier of a method reference)...
May 29, 2015 at 18:47 history answered h.j.k. CC BY-SA 3.0