Skip to content

Commit 20a98fa

Browse files
Stop allocating BytesRefBuilder for each StreamOutput
We can use a thread-local here, no need to allocate a fresh instance for each output when they mostly go unused anyway.
1 parent 6532af8 commit 20a98fa

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,12 @@ public void writeOptionalText(@Nullable Text text) throws IOException {
370370
}
371371
}
372372

373-
private final BytesRefBuilder spare = new BytesRefBuilder();
373+
private static final ThreadLocal<BytesRefBuilder> spareBytesRefBuilder = ThreadLocal.withInitial(BytesRefBuilder::new);
374374

375375
public void writeText(Text text) throws IOException {
376376
if (text.hasBytes() == false) {
377377
final String string = text.string();
378+
var spare = spareBytesRefBuilder.get();
378379
spare.copyChars(string);
379380
writeInt(spare.length());
380381
write(spare.bytes(), 0, spare.length());

0 commit comments

Comments
 (0)