Predicates?

Dan Smith daniel.smith at oracle.com
Fri Apr 19 11:09:17 PDT 2013


On Apr 18, 2013, at 1:16 PM, Brian Goetz <brian.goetz at oracle.com> wrote:

>> Predicate<String> isNull = Objects::isNull;
>> 
>> On performance: I'm sure Guava is optimized so that its methods
>> always return a singleton.  In principle, the semantics of lambdas
>> and method references are abstract enough that you shouldn't have to
>> worry about that.  In practice, I wouldn't expect clever
>> optimizations like that (e.g., turning every "x -> true" into the
>> same object) in the near future.
> 
> TO be clear: with our implementation, every invocation of the *statement*
> 
>  Predicate<String> p = () -> true;
> 
> always does return the same object.  What Dan is talking about is something different; if two places in the code have "equivalent" lambdas, whether they would be "interned".

And I guess the one-object-per-expression optimization is where most of the value of singletons/interning lies.  It's a big deal if you create 1 million different "alwaysTrue" predicates where you could have just 1.  Not so concerning if you have just 10 or 100 floating around (that is, the number of times in which the expression "x->true" appears in your code).

—Dan


More information about the lambda-dev mailing list