On Sun, Nov 16, 2008 at 12:01 PM, Shawn Pearce <[EMAIL PROTECTED]> wrote:
> My initial guess is that the array serialization during RPC is packing each
> byte into its own element in a JSON array.  So your 400kB byte array turns
> into a 409,600 element integer array, which is probably using at least 16
> bytes per entry, resulting in a fairly large (several megabyte) footprint in
> Firefox.  Doesn't explain why FF grabs several GB for this, but I think you
> are using a lot more memory than you expect.

That's a good guess, but you have to remember one thing: Javascript's
only number type is equivalent to Java's double.  It's been a while
since I've looked at the RPC internals, so things may have changed,
but it has been true that a byte[] would be converted into the
equivalent of a double[], and then serialized.

I'm curious about what you're doing with a byte[] on the client, Axel.

Besides filing an issue to see if the GWT developers want to support
your use case, I'd suggest the following: try sending a double[]
instead of a byte[] by packing 6 bytes into each double.  Code like
the following might work.

public static double[] bytesToDoubles(byte[] bytes) {
  double[] ret = new double[(bytes.length + 5) / 6];

  for (int i = 0, n = r.length; i < n; ++i) {
    ret[i] = bytesToDouble(bytes, i * 6);
  }

  return ret;
}

private static double bytesToDouble(byte[] bytes, int offset) {
  if (offset + 5 < bytes.length) {
    double highTwo = 4294967296.0 * (bytes[0] << 8 + bytes[1]);
    int lowFour = bytes[2] << 24 + bytes[3] << 16 + bytes[4] << 8 + bytes[5];

    return highTwo + lowFour;
  }

  double highTwo = 4294967296.0 * (safeGet(bytes, 0) << 8 + safeGet(bytes, 1));
  int lowFour = safeGet(bytes, 2) << 24 + safeGet(bytes, 3) << 16 +
safeGet(bytes, 4) << 8 + safeGet(bytes, 5);

  return highTwo + lowFour;
}

private static byte safeGet(byte[] bytes, int index) {
  return (index >= bytes.length ? 0 : bytes[index]);
}

I'm in a rush, so I'll leave the unpacking code as an "exercise for
the reader".  Note also that, since Java's String is a counted data
type, it might be safe to pack bytes into a String without base64
encoding them first, which would save on bytes, too.

Ian

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to