diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionaryBuilder.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionaryBuilder.java index fe8d7fd6225..9547b6f4cfb 100644 --- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionaryBuilder.java +++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionaryBuilder.java @@ -116,7 +116,7 @@ class TokenInfoDictionaryBuilder { // new word to add to fst ord++; lastValue = token; - scratch.grow(token.length()); + scratch.growNoCopy(token.length()); scratch.setLength(token.length()); for (int i = 0; i < token.length(); i++) { scratch.setIntAt(i, token.charAt(i)); diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/UserDictionary.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/UserDictionary.java index 42807eed278..beb439cde86 100644 --- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/UserDictionary.java +++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/UserDictionary.java @@ -138,7 +138,7 @@ public final class UserDictionary implements Dictionary { } // add mapping to FST String token = values[0]; - scratch.grow(token.length()); + scratch.growNoCopy(token.length()); scratch.setLength(token.length()); for (int i = 0; i < token.length(); i++) { scratch.setIntAt(i, (int) token.charAt(i)); diff --git a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/TokenInfoDictionaryBuilder.java b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/TokenInfoDictionaryBuilder.java index 8639f66be7e..79c56c24804 100644 --- a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/TokenInfoDictionaryBuilder.java +++ b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/TokenInfoDictionaryBuilder.java @@ -112,7 +112,7 @@ class TokenInfoDictionaryBuilder { // new word to add to fst ord++; lastValue = surfaceForm; - scratch.grow(surfaceForm.length()); + scratch.growNoCopy(surfaceForm.length()); scratch.setLength(surfaceForm.length()); for (int i = 0; i < surfaceForm.length(); i++) { scratch.setIntAt(i, surfaceForm.charAt(i)); diff --git a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/UserDictionary.java b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/UserDictionary.java index 4632edc8a21..6219be1e813 100644 --- a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/UserDictionary.java +++ b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/dict/UserDictionary.java @@ -121,7 +121,7 @@ public final class UserDictionary implements Dictionary { } // add mapping to FST - scratch.grow(token.length()); + scratch.growNoCopy(token.length()); scratch.setLength(token.length()); for (int i = 0; i < token.length(); i++) { scratch.setIntAt(i, token.charAt(i)); diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java index 08d582f45c0..916b696020d 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java @@ -330,7 +330,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer { } catch (ParseException pe) { throw new CorruptIndexException("failed to parse int length", in, pe); } - termByteArray.grow(len); + termByteArray.growNoCopy(len); termByteArray.setLength(len); in.readBytes(termByteArray.bytes(), 0, len); term.copyBytes(SimpleTextUtil.fromBytesRefString(termByteArray.get().utf8ToString())); @@ -571,7 +571,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer { } catch (ParseException pe) { throw new CorruptIndexException("failed to parse int length", in, pe); } - term.grow(len); + term.growNoCopy(len); term.setLength(len); in.readBytes(term.bytes(), 0, len); return term.get(); @@ -758,7 +758,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer { } catch (ParseException pe) { throw new CorruptIndexException("failed to parse int length", in, pe); } - term.grow(len); + term.growNoCopy(len); term.setLength(len); in.readBytes(term.bytes(), 0, len); return term.get(); diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java index abfbdd25a02..e8fbadfe7c5 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java @@ -600,7 +600,7 @@ class SimpleTextFieldsReader extends FieldsProducer { SimpleTextUtil.readLine(in, scratch); if (StringHelper.startsWith(scratch.get(), PAYLOAD)) { final int len = scratch.length() - PAYLOAD.length; - scratch2.grow(len); + scratch2.growNoCopy(len); System.arraycopy(scratch.bytes(), PAYLOAD.length, scratch2.bytes(), 0, len); scratch2.setLength(len); payload = scratch2.get(); @@ -727,7 +727,7 @@ class SimpleTextFieldsReader extends FieldsProducer { } lastDocsStart = in.getFilePointer(); final int len = scratch.length() - TERM.length; - lastTerm.grow(len); + lastTerm.growNoCopy(len); System.arraycopy(scratch.bytes(), TERM.length, lastTerm.bytes(), 0, len); lastTerm.setLength(len); docFreq = 0; diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java index a107096d4bb..27656ea08cc 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java @@ -150,7 +150,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader { readLine(); assert StringHelper.startsWith(scratch.get(), TERMTEXT); int termLength = scratch.length() - TERMTEXT.length; - term.grow(termLength); + term.growNoCopy(termLength); term.setLength(termLength); System.arraycopy(scratch.bytes(), TERMTEXT.length, term.bytes(), 0, termLength); diff --git a/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java index f6d672aa41b..45dcc504186 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java @@ -143,7 +143,7 @@ public abstract class TermVectorsWriter implements Closeable, Accountable { if (payload == null) { payload = new BytesRefBuilder(); } - payload.grow(payloadLength); + payload.growNoCopy(payloadLength); positions.readBytes(payload.bytes(), 0, payloadLength); payload.setLength(payloadLength); diff --git a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java index bbac4231dcd..1042ecbeb47 100644 --- a/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java +++ b/lucene/core/src/java/org/apache/lucene/index/FreqProxFields.java @@ -505,7 +505,7 @@ class FreqProxFields extends Fields { hasPayload = true; // has a payload payload.setLength(posReader.readVInt()); - payload.grow(payload.length()); + payload.growNoCopy(payload.length()); posReader.readBytes(payload.bytes(), 0, payload.length()); } else { hasPayload = false; diff --git a/lucene/core/src/java/org/apache/lucene/util/ByteBlockPool.java b/lucene/core/src/java/org/apache/lucene/util/ByteBlockPool.java index c7e0997cac0..986118acded 100644 --- a/lucene/core/src/java/org/apache/lucene/util/ByteBlockPool.java +++ b/lucene/core/src/java/org/apache/lucene/util/ByteBlockPool.java @@ -216,7 +216,7 @@ public final class ByteBlockPool implements Accountable { result.offset = pos; } else { // Uncommon case: The slice spans at least 2 blocks, so we must copy the bytes. - builder.grow(length); + builder.growNoCopy(length); result.bytes = builder.get().bytes; result.offset = 0; readBytes(offset, result.bytes, 0, length); diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRefArray.java b/lucene/core/src/java/org/apache/lucene/util/BytesRefArray.java index 6f69843586b..dd8473a408b 100644 --- a/lucene/core/src/java/org/apache/lucene/util/BytesRefArray.java +++ b/lucene/core/src/java/org/apache/lucene/util/BytesRefArray.java @@ -94,7 +94,7 @@ public final class BytesRefArray implements SortableBytesRefArray { Objects.checkIndex(index, lastElement); int offset = offsets[index]; int length = index == lastElement - 1 ? currentOffset - offset : offsets[index + 1] - offset; - spare.grow(length); + spare.growNoCopy(length); spare.setLength(length); pool.readBytes(offset, spare.bytes(), 0, spare.length()); return spare.get(); 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 97c1fac2032..21adf0c60da 100644 --- a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java +++ b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java @@ -60,6 +60,13 @@ public class BytesRefBuilder { ref.bytes = ArrayUtil.grow(ref.bytes, capacity); } + /** + * Used to grow the builder without copying bytes. see {@link ArrayUtil#growNoCopy(byte[], int)}. + */ + public void growNoCopy(int capacity) { + ref.bytes = ArrayUtil.growNoCopy(ref.bytes, capacity); + } + /** Append a single byte to this builder. */ public void append(byte b) { grow(ref.length + 1); diff --git a/lucene/core/src/java/org/apache/lucene/util/IntsRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/IntsRefBuilder.java index c1f7078da23..7ab208c3ba1 100644 --- a/lucene/core/src/java/org/apache/lucene/util/IntsRefBuilder.java +++ b/lucene/core/src/java/org/apache/lucene/util/IntsRefBuilder.java @@ -77,9 +77,14 @@ public class IntsRefBuilder { ref.ints = ArrayUtil.grow(ref.ints, newLength); } + /** Grow the reference array without copying the origin data to the new array. */ + public void growNoCopy(int newLength) { + ref.ints = ArrayUtil.growNoCopy(ref.ints, newLength); + } + /** Copies the given array into this instance. */ public void copyInts(int[] otherInts, int otherOffset, int otherLength) { - grow(otherLength); + growNoCopy(otherLength); System.arraycopy(otherInts, otherOffset, ref.ints, 0, otherLength); ref.length = otherLength; } @@ -94,7 +99,7 @@ public class IntsRefBuilder { * UTF-8 to UTF-32 and then copied into this builder. */ public void copyUTF8Bytes(BytesRef bytes) { - grow(bytes.length); + growNoCopy(bytes.length); ref.length = UnicodeUtil.UTF8toUTF32(bytes, ref.ints); } diff --git a/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java b/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java index 3800f00af8f..61db0db1c42 100644 --- a/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java +++ b/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java @@ -593,7 +593,7 @@ public class OfflineSorter { } short length = in.readShort(); - ref.grow(length); + ref.growNoCopy(length); ref.setLength(length); in.readBytes(ref.bytes(), 0, length); return ref.get(); diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/Util.java b/lucene/core/src/java/org/apache/lucene/util/fst/Util.java index 9fdc460d058..5800b22028d 100644 --- a/lucene/core/src/java/org/apache/lucene/util/fst/Util.java +++ b/lucene/core/src/java/org/apache/lucene/util/fst/Util.java @@ -738,7 +738,7 @@ public final class Util { public static IntsRef toUTF16(CharSequence s, IntsRefBuilder scratch) { final int charLimit = s.length(); scratch.setLength(charLimit); - scratch.grow(charLimit); + scratch.growNoCopy(charLimit); for (int idx = 0; idx < charLimit; idx++) { scratch.setIntAt(idx, s.charAt(idx)); } @@ -794,7 +794,7 @@ public final class Util { /** Just converts IntsRef to BytesRef; you must ensure the int values fit into a byte. */ public static BytesRef toBytesRef(IntsRef input, BytesRefBuilder scratch) { - scratch.grow(input.length); + scratch.growNoCopy(input.length); for (int i = 0; i < input.length; i++) { int value = input.ints[i + input.offset]; // NOTE: we allow -128 to 255 diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java index fc585441d61..b31330035bd 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java @@ -452,7 +452,7 @@ public class AnalyzingSuggester extends Lookup { payload = null; } - buffer = ArrayUtil.grow(buffer, requiredLength); + buffer = ArrayUtil.growNoCopy(buffer, requiredLength); output.reset(buffer); @@ -522,7 +522,7 @@ public class AnalyzingSuggester extends Lookup { } input.reset(bytes.bytes, bytes.offset, bytes.length); short analyzedLength = input.readShort(); - analyzed.grow(analyzedLength + 2); + analyzed.growNoCopy(analyzedLength + 2); input.readBytes(analyzed.bytes(), 0, analyzedLength); analyzed.setLength(analyzedLength); diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/CompletionFieldsConsumer.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/CompletionFieldsConsumer.java index c1db7eb62e1..8c6a0856f05 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/CompletionFieldsConsumer.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/CompletionFieldsConsumer.java @@ -223,7 +223,7 @@ final class CompletionFieldsConsumer extends FieldsConsumer { ByteArrayDataInput input = new ByteArrayDataInput(payload.bytes, payload.offset, payload.length); int len = input.readVInt(); - scratch.grow(len); + scratch.growNoCopy(len); scratch.setLength(len); input.readBytes(scratch.bytes(), 0, scratch.length()); long weight = input.readVInt() - 1; diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionBuilder.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionBuilder.java index 232ce8ed3ba..29949f845a0 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionBuilder.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionBuilder.java @@ -176,7 +176,7 @@ public class FSTCompletionBuilder { "Bucket outside of the allowed range [0, " + buckets + "): " + bucket); } - scratch.grow(utf8.length + 10); + scratch.growNoCopy(utf8.length + 10); scratch.clear(); scratch.append((byte) bucket); scratch.append(utf8); diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java index 44e13980054..d398fdcf318 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java @@ -180,7 +180,7 @@ public class FSTCompletionLookup extends Lookup { int inputLineCount = 0; while ((spare = iterator.next()) != null) { if (spare.length + 4 >= buffer.length) { - buffer = ArrayUtil.grow(buffer, spare.length + 4); + buffer = ArrayUtil.growNoCopy(buffer, spare.length + 4); } output.reset(buffer); diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/fst/FSTTester.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/fst/FSTTester.java index 07f877575b0..1564b2a7e88 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/fst/FSTTester.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/fst/FSTTester.java @@ -155,7 +155,7 @@ public class FSTTester { } static IntsRef toIntsRef(BytesRef br, IntsRefBuilder ir) { - ir.grow(br.length); + ir.growNoCopy(br.length); ir.clear(); for (int i = 0; i < br.length; i++) { ir.append(br.bytes[br.offset + i] & 0xFF);