Apache Lucene open-source search software
Save allocating some zero length byte arrays (#13608)
Armin Braun4c1d50d8e8cd
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Lucene90BlockTreeTermsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Lucene90BlockTreeTermsReader.javaindex 3871945e0a4..c2ff50215a3 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Lucene90BlockTreeTermsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Lucene90BlockTreeTermsReader.java@@ -270,9 +270,8 @@ public final class Lucene90BlockTreeTermsReader extends FieldsProducer {throw new CorruptIndexException("invalid bytes length: " + numBytes, in);}- BytesRef bytes = new BytesRef();+ BytesRef bytes = new BytesRef(numBytes);bytes.length = numBytes;- bytes.bytes = new byte[numBytes];in.readBytes(bytes.bytes, 0, numBytes);return bytes;diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java b/lucene/core/src/java/org/apache/lucene/util/BytesRef.javaindex 351259e87b5..4298668468f 100644--- a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRef.java@@ -68,7 +68,7 @@ public final class BytesRef implements Comparable<BytesRef>, Cloneable {* both be zero.*/public BytesRef(int capacity) {- this.bytes = new byte[capacity];+ this.bytes = capacity == 0 ? EMPTY_BYTES : new byte[capacity];}/**@@ -77,7 +77,7 @@ public final class BytesRef implements Comparable<BytesRef>, Cloneable {* @param text This must be well-formed unicode text, with no unpaired surrogates.*/public BytesRef(CharSequence text) {- this(new byte[UnicodeUtil.maxUTF8Length(text.length())]);+ this(UnicodeUtil.maxUTF8Length(text.length()));length = UnicodeUtil.UTF16toUTF8(text, 0, text.length(), bytes);}