Hello.

I have an direct buffer
ByteBuffer mScreenBuffer = ByteBuffer.allocateDirect(width*height*4);

I can read him trought
byte[] bytebuf = new byte[width*height*4];
mScreenBuffer.get(bytefuf);
and it will be lightingly fast

Well, now i need array of ints so lets make some changes:

int[] intbuf = new int[width*height];
mScreenBuffer.asIntBuffer().get(intbuf);

oops, here we have a 3-second delay while data will be copied.

Holy shit.
Well, lets see what happens:
(lets see in android\dalvik\libcore\nio\src\main\java\java\nio)

1) ByteBuffer.allocateDirect generate DirectByteBuffer
2) asIntBuffer() generate IntToByteBufferAdapter
3) IntToByteBufferAdapter extends IntBuffer implements DirectBuffer.
DirectBuffer interface doesnt contain anything useful, lets see to
IntBuffer class
and what we see?

IntByffer:
------------------------------------------------------------------------------------------------------
  public abstract int get(); <--- so it will use DirectByteBuffer's
get() method since they share only one get() method

  public IntBuffer get(int[] dest) {
        return get(dest, 0, dest.length);
    }

  public IntBuffer get(int[] dest, int off, int len) {
       int length = dest.length;
       if (off < 0 || len < 0 || (long)len + (long)off > length) {
           throw new IndexOutOfBoundsException();
       }
       if (len > remaining()) {
           throw new BufferUnderflowException();
       }
       for (int i = off; i < off + len; i++) {
           dest[i] = get();
       }
       return this;
   }


It mean my 400x400 int array will produce 160k of DirectByteBuffer's
get() calls

DirectByteBuffer.java
-----------------------------------------------------
   public final byte get() {
        if (position == limit) {
            throw new BufferUnderflowException();
        }
        return getBaseAddress().getByte(offset + position++);
    }

Seems like there we use a jni call
So it mean 160k of jni calls for one array copy.

Am i somewhere wrong?





-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" 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/android-developers?hl=en

Reply via email to