Re: ClassCastException on Array content cast
Thomas Pornin wrote:
According to Bill McCleary <mccleary.b@folderol.cs.uhtx.edu>:
How cheap is the creation and discarding of the zero-length array here?
As a general rule, there is no problem of performance until duly
measured,
Yep.
> and performance issues begin with algorithmics (canonical
> example is the use of a O(n2) sorting algorithm instead of O(n log n)).
One may assume that allocation and GC-ing of an object has a cost which
rises with the object size;
Really?
I would expect both allocation and GC cost to be independent of object
size.
And only moving around as part of memory defragmentation to depend
on object size.
thus, a zero-length array does not induce
algorithmic performance issue.
I am not disagreeing, but I really can't see the importance.
Hence, until further notice, it is cheap. One may prefer the use of a
static template (a zero-length array allocated once and for all) for
esthetic reasons, though.
It is an option in the very unlikely case that allocation and GC of
empty array indeed is a performance problem.
However:
especially if, after using it for type inference, the compiler sees
that it's never used and optimizes it away. (If not javac, the JIT.)
I find it rather improbable, because ArrayList.toArray() will use the
provided array, fill it and return it, if it has the proper size -- and
a zero-length array is big enough if the list is empty. The JIT compiler
would require some pretty heavy context information to optimize away the
allocation.
Agree.
Arne