hg: lambda/lambda/jdk: Implement groupBy for both ordered and unordered sources

Brian Goetz brian.goetz at oracle.com
Tue Oct 23 13:54:55 PDT 2012


Good catch.  The null check here is cheap but the formatting is not, and 
given that this is on a hot code path, probably should be adjusted to 
use a static error message or inline the requireNonNull logic.

Unfortunately simply overloading Objects.requireNonNull with a version 
taking a Factory<String> would, while cheaper than formatting 
preemptively, not be as cheap as you think it is.  That's because its 
likely that the lambda would capture something, and still create an 
object to wrap the captured values.  Cheaper but not zero.  (The same 
issue comes up in the Logger API.)  We're working on some VM magic to 
reduce the cost here but that may take some time to fully coalesce.  The 
varargs version has the same issues (worse if there are primitives, 
since that would be multiple boxings plus an array boxing.)

On 10/23/2012 4:48 PM, Arne Siegel wrote:
> This excerpt from GroupByOpcries for a new overloaded requireNonNull in
> class Objects, as formatting an instance of an unknown type may be quite
> expensive:
>
>          public void accept(T t) {
>              K key = Objects.requireNonNull(mapper.map(t),
> String.format("The element %s cannot be mapped to a null key", t));
>
> Two simple solutions come to mind: adding a Factory<String> parameter,
> or adding a Object... parameter and moving the format into requireNonNull.
>
> Arne Siegel
>
> On 22 Oct 2012 at 21:05, brian.goetz at oracle.com wrote:
>
>> Changeset: 532efd9be743
>> Author:    briangoetz
>> Date:      2012-10-22 17:04 -0400
>> URL:       http://hg.openjdk.java.net/lambda/lambda/jdk/rev/532efd9be743
>>
>> Implement groupBy for both ordered and unordered sources
>> Contributed-By: paul.sandoz at oracle.com
>>
>> + src/share/classes/java/util/streams/ops/FlagDeclaringOp.java
>> ! src/share/classes/java/util/streams/ops/GroupByOp.java
>> ! test-ng/tests/org/openjdk/tests/java/util/streams/ops/GroupByOpTest.java
>> ! test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java
>>
>>
>


More information about the lambda-dev mailing list