You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2024/07/24 19:58:32 UTC

(lucene) branch branch_9x updated: Save allocating some zero length byte arrays (#13608)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 7b47c8a73cb Save allocating some zero length byte arrays (#13608)
7b47c8a73cb is described below

commit 7b47c8a73cba90b22923f9c578e2d37c43306f63
Author: Armin Braun <me...@obrown.io>
AuthorDate: Wed Jul 24 21:53:30 2024 +0200

    Save allocating some zero length byte arrays (#13608)
    
    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.
---
 .../codecs/lucene90/blocktree/Lucene90BlockTreeTermsReader.java       | 3 +--
 lucene/core/src/java/org/apache/lucene/util/BytesRef.java             | 4 ++--
 2 files changed, 3 insertions(+), 4 deletions(-)

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.java
index 74b8591bfd1..64c06524868 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
@@ -269,9 +269,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.java
index 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);
   }