Skip to content

Apache Lucene open-source search software

Use growNoCopy when copying bytes in BytesRefBuilder (#13930)

Ignacio Vera05e06e51ec34
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);
}