From 65ca6053dc7fef58d3b9cac6d09f641a2d281ae5 Mon Sep 17 00:00:00 2001 From: majisourav <62680973+majisourav@users.noreply.github.com> Date: Tue, 7 Apr 2020 11:57:51 -0700 Subject: [PATCH] Initialize ByteBuffer for PrimitiveFloatList List interface usage (#42) * Initialize ByteBuffer for PrimitiveFloatList List interface usage Co-authored-by: Sourav Maji --- .../avro/fastserde/CompositeByteBuffer.java | 5 +++-- .../avro/fastserde/PrimitiveFloatList.java | 9 ++++----- .../FastDeserializerDefaultsTest.java | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/CompositeByteBuffer.java b/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/CompositeByteBuffer.java index b7448531f..a4562b91d 100644 --- a/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/CompositeByteBuffer.java +++ b/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/CompositeByteBuffer.java @@ -3,6 +3,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Collections; import java.util.List; @@ -10,8 +11,8 @@ public class CompositeByteBuffer { private int byteBufferCount; private List 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) { diff --git a/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/PrimitiveFloatList.java b/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/PrimitiveFloatList.java index fd9a8df02..d80548d40 100644 --- a/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/PrimitiveFloatList.java +++ b/avro-fastserde/src/main/java/com/linkedin/avro/fastserde/PrimitiveFloatList.java @@ -48,10 +48,8 @@ public PrimitiveFloatList(int capacity) { if (capacity != 0) { elements = new float[capacity]; } - } - - public PrimitiveFloatList() { - byteBuffer = new CompositeByteBuffer(); + // create empty ByteBuffer if capacity != 0 ( List interface usage case) + byteBuffer = new CompositeByteBuffer(capacity != 0); } public PrimitiveFloatList(Collection c) { @@ -59,6 +57,7 @@ public PrimitiveFloatList(Collection c) { elements = new float[c.size()]; addAll(c); } + byteBuffer = new CompositeByteBuffer(c != null); } /** @@ -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); } } diff --git a/avro-fastserde/src/test/java/com/linkedin/avro/fastserde/FastDeserializerDefaultsTest.java b/avro-fastserde/src/test/java/com/linkedin/avro/fastserde/FastDeserializerDefaultsTest.java index 3340d6141..c490c9d75 100644 --- a/avro-fastserde/src/test/java/com/linkedin/avro/fastserde/FastDeserializerDefaultsTest.java +++ b/avro-fastserde/src/test/java/com/linkedin/avro/fastserde/FastDeserializerDefaultsTest.java @@ -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;