Skip to content

Commit

Permalink
Initialize ByteBuffer for PrimitiveFloatList List<Float> interface us…
Browse files Browse the repository at this point in the history
…age (#42)

* Initialize ByteBuffer for PrimitiveFloatList List<Float> interface usage

Co-authored-by: Sourav Maji <[email protected]>
  • Loading branch information
majisourav and Sourav Maji authored Apr 7, 2020
1 parent 1eb7931 commit 65ca605
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class CompositeByteBuffer {
private int byteBufferCount;
private List<ByteBuffer> byteBuffers;

public CompositeByteBuffer() {
byteBuffers = new ArrayList<>(2);
public CompositeByteBuffer(boolean createEmpty) {
byteBuffers = createEmpty ? Collections.emptyList() : new ArrayList<>(2);
}

public ByteBuffer allocate(int index, int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ public PrimitiveFloatList(int capacity) {
if (capacity != 0) {
elements = new float[capacity];
}
}

public PrimitiveFloatList() {
byteBuffer = new CompositeByteBuffer();
// create empty ByteBuffer if capacity != 0 ( List<Float> interface usage case)
byteBuffer = new CompositeByteBuffer(capacity != 0);
}

public PrimitiveFloatList(Collection<Float> c) {
if (c != null) {
elements = new float[c.size()];
addAll(c);
}
byteBuffer = new CompositeByteBuffer(c != null);
}

/**
Expand Down Expand Up @@ -129,7 +128,7 @@ private static Object newPrimitiveFloatArray(Object old) {
return oldFloatList;
} else {
// Just a place holder, will set up the elements later.
return new PrimitiveFloatList();
return new PrimitiveFloatList(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ public byte[] serialize(Object object, GenericDatumWriter writer) throws Excepti
return output.toByteArray();
}

@Test
public void testPrimitiveFloatListAddPrimitive() {
int array_size = 250, iteration = 100;
float w = 0;
long startTime = System.currentTimeMillis();

for (int i = 0; i < iteration; i++) {
PrimitiveFloatList list = new PrimitiveFloatList(array_size);

for (int l = 0; l < array_size; l++) {
list.addPrimitive((float) l);
}
for (Float f : list) {
w += f;
}
}
long endTime = System.currentTimeMillis();
System.out.println(String.format("time taken to addPrimitive to float list: %d", endTime - startTime));
}

@Test
public void testFastFloatArraySerDes() {
int array_size = 2500, iteration = 100_000;
Expand Down

0 comments on commit 65ca605

Please sign in to comment.