Skip to content

Commit

Permalink
Improve arraybuffer performance (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan authored Jul 13, 2024
1 parent c77f1e9 commit 9becf12
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;
Expand All @@ -8,19 +9,37 @@
@Emulate(valueStr = "java.nio.ByteBufferImpl", updateCode = true)
public abstract class ByteBufferImplEmu extends TByteBufferImpl implements HasArrayBufferView {

public ByteBufferImplEmu(int capacity, boolean direct) {
super(capacity, direct);
}
@Emulate
Int8ArrayWrapper backupArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public ByteBufferImplEmu(int start, int capacity, byte[] array, int position, int limit, boolean direct, boolean readOnly) {
super(start, capacity, array, position, limit, direct, readOnly);
}

@Override
@Emulate
public Int8ArrayWrapper getTypedArray() {
public ArrayBufferViewWrapper getArrayBufferView() {
Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView();
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = int8Array.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
Object array = array();
return TypedArrays.getArrayBufferView((JSObject)array);
Int8ArrayWrapper int8Array = TypedArrays.getArrayBufferView((JSObject)array);
return int8Array;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Float32ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.TFloatBufferOverArray", updateCode = true)
public abstract class FloatBufferOverArrayEmu extends TFloatBufferOverArray implements HasArrayBufferView {

@Emulate
Float32ArrayWrapper backupArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public FloatBufferOverArrayEmu(int start, int capacity, float[] array, int position, int limit, boolean readOnly) {
super(start, capacity, array, position, limit, readOnly);
}

@Override
@Emulate
public ArrayBufferViewWrapper getArrayBufferView() {
Float32ArrayWrapper originalBuffer = (Float32ArrayWrapper)getOriginalArrayBufferView();
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = originalBuffer.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
return TypedArrays.getTypedArray(array);
}

@Override
@Emulate
public int getElementSize() {
return 4;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Float32ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.FloatBufferOverByteBuffer", updateCode = true)
public abstract class FloatBufferOverByteBufferEmu extends TFloatBufferOverByteBuffer implements HasArrayBufferView {

@Emulate
Float32ArrayWrapper backupArray;
@Emulate
Float32ArrayWrapper floatArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public FloatBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) {
super(start, capacity, byteBuffer, position, limit, readOnly);
}

@Override
@Emulate
public Int8ArrayWrapper getTypedArray() {
public ArrayBufferViewWrapper getArrayBufferView() {
// Int8Array
Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView();
if(floatArray == null) {
floatArray = TypedArrays.createFloat32Array(int8Array.getBuffer());
}

int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = floatArray.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
HasArrayBufferView buff = (HasArrayBufferView)byteByffer;
return buff.getTypedArray();
return buff.getOriginalArrayBufferView();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;

public interface HasArrayBufferView {
Int8ArrayWrapper getTypedArray();
ArrayBufferViewWrapper getArrayBufferView();
ArrayBufferViewWrapper getOriginalArrayBufferView();
int getElementSize();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int32ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.TIntBufferOverArray", updateCode = true)
public abstract class IntBufferOverArrayEmu extends TIntBufferOverArray implements HasArrayBufferView {

@Emulate
Int32ArrayWrapper backupArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public IntBufferOverArrayEmu(int start, int capacity, int[] array, int position, int limit, boolean readOnly) {
super(start, capacity, array, position, limit, readOnly);
}

@Override
@Emulate
public ArrayBufferViewWrapper getArrayBufferView() {
Int32ArrayWrapper originalBuffer = (Int32ArrayWrapper)getOriginalArrayBufferView();
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = originalBuffer.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
return TypedArrays.getTypedArray(array);
}

@Override
@Emulate
public int getElementSize() {
return 4;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int32ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.IntBufferOverByteBuffer", updateCode = true)
public abstract class IntBufferOverByteBufferEmu extends TIntBufferOverByteBuffer implements HasArrayBufferView {

@Emulate
Int32ArrayWrapper backupArray;
@Emulate
Int32ArrayWrapper intArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;


public IntBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) {
super(start, capacity, byteBuffer, position, limit, readOnly);
}

@Override
@Emulate
public Int8ArrayWrapper getTypedArray() {
public ArrayBufferViewWrapper getArrayBufferView() {
// Int8Array
Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView();
if(intArray == null) {
intArray = TypedArrays.createInt32Array(int8Array.getBuffer());
}
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = intArray.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
HasArrayBufferView buff = (HasArrayBufferView)byteByffer;
return buff.getTypedArray();
return buff.getOriginalArrayBufferView();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int16ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.TShortBufferOverArray", updateCode = true)
public abstract class ShortBufferOverArrayEmu extends TShortBufferOverArray implements HasArrayBufferView {

@Emulate
Int16ArrayWrapper backupArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public ShortBufferOverArrayEmu(int start, int capacity, short[] array, int position, int limit, boolean readOnly) {
super(start, capacity, array, position, limit, readOnly);
}

@Override
@Emulate
public ArrayBufferViewWrapper getArrayBufferView() {
Int16ArrayWrapper originalBuffer = (Int16ArrayWrapper)getOriginalArrayBufferView();
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = originalBuffer.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
return TypedArrays.getTypedArray(array);
}

@Override
@Emulate
public int getElementSize() {
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
package org.teavm.classlib.java.nio;

import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int16ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays;
import com.github.xpenatan.gdx.backends.teavm.gen.Emulate;

@Emulate(valueStr = "java.nio.ShortBufferOverByteBuffer", updateCode = true)
public abstract class ShortBufferOverByteBufferEmu extends TShortBufferOverByteBuffer implements HasArrayBufferView {

@Emulate
Int16ArrayWrapper backupArray;
@Emulate
Int16ArrayWrapper shortArray;
@Emulate
int positionCache;
@Emulate
int remainingCache;

public ShortBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) {
super(start, capacity, byteBuffer, position, limit, readOnly);
}

@Override
@Emulate
public Int8ArrayWrapper getTypedArray() {
public ArrayBufferViewWrapper getArrayBufferView() {
// Int8Array
Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView();
if(shortArray == null) {
shortArray = TypedArrays.createInt16Array(int8Array.getBuffer());
}
int position1 = position();
int remaining1 = remaining();
if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) {
positionCache = position1;
remainingCache = remaining1;
backupArray = shortArray.subarray(position1, remaining1);
}
return backupArray;
}

@Override
@Emulate
public ArrayBufferViewWrapper getOriginalArrayBufferView() {
HasArrayBufferView buff = (HasArrayBufferView)byteByffer;
return buff.getTypedArray();
return buff.getOriginalArrayBufferView();
}

@Override
Expand Down
Loading

0 comments on commit 9becf12

Please sign in to comment.