Skip to content

Commit 70e7fa4

Browse files
authored
Avoid double allocation when compressing histograms (elastic#137003)
1 parent 5ef646c commit 70e7fa4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/CompressedHistogramData.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ static void write(OutputStream output, int scale, BucketIterator negativeBuckets
117117
}
118118
output.write((byte) scaleWithFlags);
119119
if (hasNegativeBuckets) {
120-
ByteArrayOutputStream temp = new ByteArrayOutputStream();
120+
AccessibleByteArrayOutputStream temp = new AccessibleByteArrayOutputStream();
121121
BucketsDecoder.serializeBuckets(temp, negativeBuckets);
122-
byte[] data = temp.toByteArray();
123-
writeVLong(data.length, output);
124-
output.write(data);
122+
writeVLong(temp.size(), output);
123+
output.write(temp.getBufferDirect(), 0, temp.size());
125124
}
126125
BucketsDecoder.serializeBuckets(output, positiveBuckets);
127126
}
@@ -252,6 +251,14 @@ private static void serializeBuckets(OutputStream out, BucketIterator buckets) t
252251
}
253252
}
254253

254+
private static class AccessibleByteArrayOutputStream extends ByteArrayOutputStream {
255+
256+
byte[] getBufferDirect() {
257+
return buf;
258+
}
259+
260+
}
261+
255262
private static class AccessibleByteArrayStreamInput extends ByteArrayInputStream {
256263

257264
AccessibleByteArrayStreamInput(byte[] buf, int offset, int length) {

0 commit comments

Comments
 (0)