Simplifying reified generics with partial specialization

Tomas Mikula tomas.mikula at gmail.com
Mon Jan 5 15:28:54 UTC 2015


On Mon, Jan 5, 2015 at 1:10 PM, Ron Pressler <ron at paralleluniverse.co> wrote:
> Hi. I've been following the discussion here, and I think there's a simple
> solution that will satisfy everyone: [...]
>
> The idea is simple: a generic instantiation over a value type would
> specialize fields (and array fields, obviously) and local variables, but
> not arguments and return values of non-private methods.

This does not let us enjoy a performance gain in higher-order
operations, such as summing a Stream<int> using Stream#reduce:

Stream<int> stream;
stream.reduce(0, (a, b) -> a + b);

The second argument to the reduce method has the type
BinaryOperator<int>, whose method

T apply(T a, T b)

is actualy

Integer apply(Integer a, Integer b);

as argument types and return types are not specialized as per your
proposal. Thus, all ints in the specialized Stream still have to be
boxed to be passed to the apply method, then unboxed inside the apply
method for the + operator, whose result is again boxed as a return
value of apply.

Regards,
Tomas


More information about the valhalla-dev mailing list