Skip to content

Commit

Permalink
Do not allocate primitive float array unless needed (#129)
Browse files Browse the repository at this point in the history
* Do not allocate primitive float array unless needed

Co-authored-by: Sourav Maji <majisourav@gmail.com>
  • Loading branch information
majisourav99 and majisourav authored Jan 27, 2021
1 parent 2904ae9 commit 4265e82
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ public static Object readPrimitiveFloatArray(Object old, Decoder in) throws IOEx
} while (length > 0);

array.byteBuffer.setByteBufferCount(index);
setupElements(array, (int)totalLength);
array.size = (int) totalLength;
return array;
} else {
return new ByteBufferBackedPrimitiveFloatList(0);
}
}

/**
* The primitive float array `elements` will only be used when the interface user calls a mutating operation.
* eg add/remove else for read-only use case this will not be called.
* @param list
* @param totalSize
*/
private static void setupElements(ByteBufferBackedPrimitiveFloatList list, int totalSize) {
if (list.elements.length != 0) {
if (totalSize <= list.getCapacity()) {
Expand Down Expand Up @@ -271,6 +277,7 @@ private void cacheFromByteBuffer() {
}
synchronized (this) {
if (!isCached) {
setupElements(this, this.size);
byteBuffer.setArray(elements);
isCached = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CompositeByteBuffer {
private List<ByteBuffer> byteBuffers;

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

public ByteBuffer allocate(int index, int size) {
Expand Down

0 comments on commit 4265e82

Please sign in to comment.