Skip to content

Commit

Permalink
Save allocating some zero length byte arrays (apache#13608)
Browse files Browse the repository at this point in the history
Something I found in a heap dump. For large numbers of `FieldReader`
where the minimum term is an empty string, we allocate MBs worth of
empty `byte[]` in ES. Worth adding the conditional here I think.
  • Loading branch information
original-brownbear authored Jul 24, 2024
1 parent acbd714 commit 4c1d50d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ private static BytesRef readBytesRef(IndexInput in) throws IOException {
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;
Expand Down
4 changes: 2 additions & 2 deletions lucene/core/src/java/org/apache/lucene/util/BytesRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public BytesRef(byte[] bytes) {
* both be zero.
*/
public BytesRef(int capacity) {
this.bytes = new byte[capacity];
this.bytes = capacity == 0 ? EMPTY_BYTES : new byte[capacity];
}

/**
Expand All @@ -77,7 +77,7 @@ public BytesRef(int capacity) {
* @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);
}

Expand Down

0 comments on commit 4c1d50d

Please sign in to comment.