From 05e06e51ec3429d45d75c4c7d61e55c6c6dee2f2 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Mon, 21 Oct 2024 07:36:46 +0200 Subject: [PATCH] Use growNoCopy when copying bytes in BytesRefBuilder (#13930) --- lucene/CHANGES.txt | 2 ++ .../org/apache/lucene/util/BytesRefBuilder.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 5d9343bca2a..15a72c66a63 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -53,6 +53,8 @@ Optimizations * GITHUB#13800: MaxScoreBulkScorer now recomputes scorer partitions when the minimum competitive allows for a more favorable partitioning. (Adrien Grand) +* GITHUB#13930: Use growNoCopy when copying bytes in BytesRefBuilder. (Ignacio Vera) + Bug Fixes --------------------- * GITHUB#13832: Fixed an issue where the DefaultPassageFormatter.format method did not format passages as intended diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java index 21adf0c60da..27fc4337d25 100644 --- a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java +++ b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java @@ -100,8 +100,10 @@ public class BytesRefBuilder { * #clear()} and then {@link #append(byte[], int, int)}. */ public void copyBytes(byte[] b, int off, int len) { - clear(); - append(b, off, len); + assert ref.offset == 0; + ref.length = len; + growNoCopy(len); + System.arraycopy(b, off, ref.bytes, 0, len); } /** @@ -109,8 +111,7 @@ public class BytesRefBuilder { * #clear()} and then {@link #append(BytesRef)}. */ public void copyBytes(BytesRef ref) { - clear(); - append(ref); + copyBytes(ref.bytes, ref.offset, ref.length); } /** @@ -118,8 +119,7 @@ public class BytesRefBuilder { * #clear()} and then {@link #append(BytesRefBuilder)}. */ public void copyBytes(BytesRefBuilder builder) { - clear(); - append(builder); + copyBytes(builder.get()); } /** @@ -135,7 +135,7 @@ public class BytesRefBuilder { * text. */ public void copyChars(CharSequence text, int off, int len) { - grow(UnicodeUtil.maxUTF8Length(len)); + growNoCopy(UnicodeUtil.maxUTF8Length(len)); ref.length = UnicodeUtil.UTF16toUTF8(text, off, len, ref.bytes); } @@ -144,7 +144,7 @@ public class BytesRefBuilder { * text. */ public void copyChars(char[] text, int off, int len) { - grow(UnicodeUtil.maxUTF8Length(len)); + growNoCopy(UnicodeUtil.maxUTF8Length(len)); ref.length = UnicodeUtil.UTF16toUTF8(text, off, len, ref.bytes); }