System.arraycopy limits and Function.prototype.apply

419 views
Skip to first unread message

cko

unread,
Jan 2, 2012, 6:05:33 AM1/2/12
to Google Web Toolkit
Hi all,

I have this silly example (which is actually quite common in regular
Java):

private static int testArraycopy(int size)
{
double[] src = new double[size];
double[] dest = new double[size + 1];

System.arraycopy(src,0,dest,0,src.length);

return size;
}

After compiling this to Javascript I get various errors if the array
sizes are large:

Chrome 16; size=99999:
RangeError: Maximum call stack size exceeded

Firefox 8; size=491519:
RangeError: arguments array passed to Function.prototype.apply is too
large

So the problem is invoking a varargs function (in our case
Array.splice) with the content of the src array as individual function
arguments.

Why is System.arraycopy() implemented like this? Is this a known
limitation? Are there recommended workarounds?
(another place that fails the String(char[]) constructor that does a
similar thing)

Kind regards,
Csaba


Thomas Broyer

unread,
Jan 3, 2012, 4:06:46 AM1/3/12
to google-we...@googlegroups.com


On Monday, January 2, 2012 12:05:33 PM UTC+1, cko wrote:

So the problem is invoking a varargs function (in our case
Array.splice) with the content of the src array as individual function
arguments.

Why is System.arraycopy() implemented like this?

How would you have done it? Array.splice/Array.slice surely is the fastest emulation of System.arraycopy.
 
Is this a known limitation?

Not to my knowledge; probably no-one ever tried copying that many elements.
 
Are there recommended workarounds?

Something like that maybe?

while (length > MAX_ELEMENTS_PER_COPY) {
  System.arraycopy(src, srcPos, dest, destPost, Math.max(length, MAX_ELEMENTS_PER_COPY));
  srcPos += MAX_ELEMENTS_PER_COPY;
  destPost += MAX_ELEMENTS_PER_COPY;
  length -= MAX_ELEMENTS_PER_COPY;
}

cko

unread,
Jan 3, 2012, 9:08:25 AM1/3/12
to Google Web Toolkit
Thank you. Well, I am porting a desktop solution where having byte
arrays with 100 thousand elements are not considered large (or
uncommon).
But now I know I should look out for these.
As workaround I am currently using ArrayList (which, contrary to the
desktop version, does not use System.arraycopy()), but will consider
also your suggestion.
Kind regards,

Csaba
Reply all
Reply to author
Forward
0 new messages