ASTERIXDB-1746: no excessive new byte[] allocation during a record build
- During a record write using RecordBuilder.write(), it allocates two new byte[]
using toByteArray() method. We can replace it to getByteArray() method and
this reduces a lot of new byte[] allocation.
Change-Id: Ibdbc6314abd72d2a93978da49ea406c3c38731d2
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1376
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ian Maxon <imaxon@apache.org>
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/RecordBuilder.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/RecordBuilder.java
index d247350..44181d5 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/RecordBuilder.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/RecordBuilder.java
@@ -20,7 +20,6 @@
package org.apache.asterix.builders;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.IOException;
@@ -59,7 +58,7 @@
private final IBinaryHashFunction utf8HashFunction;
private final IBinaryComparator utf8Comparator;
- private final ByteArrayOutputStream closedPartOutputStream;
+ private final ByteArrayAccessibleOutputStream closedPartOutputStream;
private int[] closedPartOffsets;
private int numberOfClosedFields;
private byte[] nullBitMap;
@@ -73,7 +72,7 @@
private RuntimeRecordTypeInfo recTypeInfo;
public RecordBuilder() {
- this.closedPartOutputStream = new ByteArrayOutputStream();
+ this.closedPartOutputStream = new ByteArrayAccessibleOutputStream();
this.numberOfClosedFields = 0;
this.openPartOutputStream = new ByteArrayAccessibleOutputStream();
@@ -287,14 +286,14 @@
for (int i = 0; i < numberOfSchemaFields; i++) {
out.writeInt(closedPartOffsets[i] + headerSize + (numberOfSchemaFields * 4));
}
- out.write(closedPartOutputStream.toByteArray());
+ out.write(closedPartOutputStream.getByteArray(), 0, closedPartOutputStream.getLength());
}
// write the open part
if (numberOfOpenFields > 0) {
out.writeInt(numberOfOpenFields);
out.write(openPartOffsetArray, 0, openPartOffsetArraySize);
- out.write(openPartOutputStream.toByteArray());
+ out.write(openPartOutputStream.getByteArray(), 0, openPartOutputStream.getLength());
}
} catch (IOException e) {
throw new HyracksDataException(e);
diff --git a/hyracks-fullstack/hyracks/hyracks-data/hyracks-data-std/src/main/java/org/apache/hyracks/data/std/util/ByteArrayAccessibleOutputStream.java b/hyracks-fullstack/hyracks/hyracks-data/hyracks-data-std/src/main/java/org/apache/hyracks/data/std/util/ByteArrayAccessibleOutputStream.java
index 370f936..c1558ba 100644
--- a/hyracks-fullstack/hyracks/hyracks-data/hyracks-data-std/src/main/java/org/apache/hyracks/data/std/util/ByteArrayAccessibleOutputStream.java
+++ b/hyracks-fullstack/hyracks/hyracks-data/hyracks-data-std/src/main/java/org/apache/hyracks/data/std/util/ByteArrayAccessibleOutputStream.java
@@ -103,4 +103,11 @@
}
buf = Arrays.copyOf(buf, newCapacity);
}
+
+ /**
+ * Return the current length of this stream (not capacity).
+ */
+ public int getLength() {
+ return count;
+ }
}