Skip to content

Commit

Permalink
Fix a bug in object reuse in CompositeByteBuffer (#48)
Browse files Browse the repository at this point in the history
The buffer size should be less than or equal to the new size for object reuse to utilize the existing buffer in CompositeByteBuffer. The bug was that we previously did not check for equality, leading to spurious memory allocation in the common case where we attempt to reuse arrays with the same number of elements.
  • Loading branch information
majisourav authored May 13, 2020
1 parent 84432ee commit 4f0f61f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ByteBuffer allocate(int index, int size) {
ByteBuffer byteBuffer;

// Check if we can reuse the old record's byteBuffers, else allocate a new one.
if (byteBuffers.size() > index && byteBuffers.get(index).capacity() > size) {
if (byteBuffers.size() > index && byteBuffers.get(index).capacity() >= size) {
byteBuffer = byteBuffers.get(index);
byteBuffer.clear();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,16 @@ public void testFastGenericDeserializerPrimitFloatList() throws Exception {
List<Float> list = (List<Float>)genericRecord.get(1);
Assert.assertEquals(list.size(), 3);

// new record array length shorter than reuse record.
// new record array length same as before for reusing byte buffers
arrayList.clear();
arrayList.add((float)10);
arrayList.add((float)20);
arrayList.add((float)30);
record1.put("inventory", arrayList);
serializedBytes = serialize(record1, oldRecordSchema);
genericRecord = deserializer.deserialize(deserRecord, getDecoder(serializedBytes));
list = (List<Float>)genericRecord.get(1);
Assert.assertEquals(list.size(), 1);
Assert.assertEquals(list.size(), 3);

}
}

0 comments on commit 4f0f61f

Please sign in to comment.