Transparency
Reinier Zwitserloot
reinier at zwitserloot.com
Wed Jul 14 07:17:17 PDT 2010
Indeed, having: new ArrayList<int>(); be as fast as an int array requires
reification; once ArrayList creates a backing array of type Object[], the
performance game is lost.
Nothing short of reification can address this issue. To be specific,
specialization won't solve it any more than primitives-in-generics does.
However, this:
List<int> list = Lists.of(int.class);
list.add(10);
or:
List<int> list = new IntList();
list.add(10);
*CAN* be made fast. Specialization, interestingly enough, cannot do this*,
but my primitives in generics proposal can. Which partly makes me wonder why
we're talking about the far more complicated specialization concept in the
first place. I know scala uses this, but they either introduced their
collections API with specialization from day one, or they created a
backwards incompatible retrofit later.
*) list.add(10), is, as far as the compiler knows, a call to add(T) on a
List<int>. If this is to be fast under specialization, it would therefore
have to generate a call to an add method with signature (I)Z and not with
signature (Ljava/lang/Object;)Z like it would today (and autobox to make the
int fit as an object). However, if it is to generate such a call, then *ANY*
list, no matter how it was obtained, must have an add (I)Z method, but lists
written today do not because that's not part of the List.java interface. We
can't add methods to interfaces or we'd break backwards compatibility, and
breaking backwards compatibility is not acceptable. Therefore, in list
implementations that lack add(I)Z, the JVM or the classloader has to
generate these. There's precedence of course: Brian Goetz' proposal for
defender methods does something quite similar. Still, this is far from
trivial, and I'd like to see a proposal on how one could update List.java so
that the JVM / ClassLoader considers the specialization methods as
defenderish, but the object-based one itself not. Primitives-in-generics
keeps things simple by letting the call to (Ljava/lang/Object;)Z stand, and
letting the JVM hotspot compiler eliminate the inefficiency instead.
--Reinier Zwitserloot
On Tue, Jul 13, 2010 at 11:08 PM, Nathan Bryant <nathan.bryant at linkshare.com
> wrote:
> John, it's just array creation
>
> class Foo<T> {
> T[] arr = new T[SIZE]; // Can't write this without reification
> }
>
> class Foo<T> {
> Object[] arr = new Object[SIZE];
>
> void set(int i, T t) { arr[i] = t; } // Either causes autoboxing
> unbeknownst to the sorry fool who wrote new Foo<int>, or becomes illegal
> }
>
>
> John Nilsson wrote:
>
> > What exactly is it that requires reification to work?
>
> BR,
> John
>
>
More information about the lambda-dev
mailing list