Skip to content

Commit

Permalink
[enhancement] Optimize memory copy overhead to enhance performance. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ewan0x79 authored Jul 1, 2024
1 parent be9ec22 commit c895909
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions api/src/main/java/ai/djl/ndarray/NDManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ default NDArray create(boolean[] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(float[][] data) {
FloatBuffer buffer = FloatBuffer.allocate(data.length * data[0].length);
FloatBuffer buffer = allocateDirect(data.length * data[0].length * 4).asFloatBuffer();
for (float[] d : data) {
buffer.put(d);
}
Expand All @@ -414,7 +414,7 @@ default NDArray create(float[][] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(int[][] data) {
IntBuffer buffer = IntBuffer.allocate(data.length * data[0].length);
IntBuffer buffer = allocateDirect(data.length * data[0].length * 4).asIntBuffer();
for (int[] d : data) {
buffer.put(d);
}
Expand All @@ -429,7 +429,7 @@ default NDArray create(int[][] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(double[][] data) {
DoubleBuffer buffer = DoubleBuffer.allocate(data.length * data[0].length);
DoubleBuffer buffer = allocateDirect(data.length * data[0].length * 8).asDoubleBuffer();
for (double[] d : data) {
buffer.put(d);
}
Expand All @@ -444,7 +444,7 @@ default NDArray create(double[][] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(long[][] data) {
LongBuffer buffer = LongBuffer.allocate(data.length * data[0].length);
LongBuffer buffer = allocateDirect(data.length * data[0].length * 8).asLongBuffer();
for (long[] d : data) {
buffer.put(d);
}
Expand All @@ -459,7 +459,7 @@ default NDArray create(long[][] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(byte[][] data) {
ByteBuffer buffer = ByteBuffer.allocate(data.length * data[0].length);
ByteBuffer buffer = allocateDirect(data.length * data[0].length);
for (byte[] d : data) {
buffer.put(d);
}
Expand All @@ -474,7 +474,7 @@ default NDArray create(byte[][] data) {
* @return a new instance of {@link NDArray}
*/
default NDArray create(boolean[][] data) {
ByteBuffer buffer = ByteBuffer.allocate(data.length * data[0].length);
ByteBuffer buffer = allocateDirect(data.length * data[0].length);
for (boolean[] d : data) {
for (boolean b : d) {
buffer.put((byte) (b ? 1 : 0));
Expand Down

0 comments on commit c895909

Please sign in to comment.