Use growNoCopy in some places (#12951)

This commit is contained in:
Zhang Chao 2024-02-05 00:32:19 +08:00 committed by GitHub
parent 9caeb9395d
commit c02f5473b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 37 additions and 25 deletions

View File

@ -116,7 +116,7 @@ class TokenInfoDictionaryBuilder {
// new word to add to fst // new word to add to fst
ord++; ord++;
lastValue = token; lastValue = token;
scratch.grow(token.length()); scratch.growNoCopy(token.length());
scratch.setLength(token.length()); scratch.setLength(token.length());
for (int i = 0; i < token.length(); i++) { for (int i = 0; i < token.length(); i++) {
scratch.setIntAt(i, token.charAt(i)); scratch.setIntAt(i, token.charAt(i));

View File

@ -138,7 +138,7 @@ public final class UserDictionary implements Dictionary<UserMorphData> {
} }
// add mapping to FST // add mapping to FST
String token = values[0]; String token = values[0];
scratch.grow(token.length()); scratch.growNoCopy(token.length());
scratch.setLength(token.length()); scratch.setLength(token.length());
for (int i = 0; i < token.length(); i++) { for (int i = 0; i < token.length(); i++) {
scratch.setIntAt(i, (int) token.charAt(i)); scratch.setIntAt(i, (int) token.charAt(i));

View File

@ -112,7 +112,7 @@ class TokenInfoDictionaryBuilder {
// new word to add to fst // new word to add to fst
ord++; ord++;
lastValue = surfaceForm; lastValue = surfaceForm;
scratch.grow(surfaceForm.length()); scratch.growNoCopy(surfaceForm.length());
scratch.setLength(surfaceForm.length()); scratch.setLength(surfaceForm.length());
for (int i = 0; i < surfaceForm.length(); i++) { for (int i = 0; i < surfaceForm.length(); i++) {
scratch.setIntAt(i, surfaceForm.charAt(i)); scratch.setIntAt(i, surfaceForm.charAt(i));

View File

@ -121,7 +121,7 @@ public final class UserDictionary implements Dictionary<UserMorphData> {
} }
// add mapping to FST // add mapping to FST
scratch.grow(token.length()); scratch.growNoCopy(token.length());
scratch.setLength(token.length()); scratch.setLength(token.length());
for (int i = 0; i < token.length(); i++) { for (int i = 0; i < token.length(); i++) {
scratch.setIntAt(i, token.charAt(i)); scratch.setIntAt(i, token.charAt(i));

View File

@ -330,7 +330,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer {
} catch (ParseException pe) { } catch (ParseException pe) {
throw new CorruptIndexException("failed to parse int length", in, pe); throw new CorruptIndexException("failed to parse int length", in, pe);
} }
termByteArray.grow(len); termByteArray.growNoCopy(len);
termByteArray.setLength(len); termByteArray.setLength(len);
in.readBytes(termByteArray.bytes(), 0, len); in.readBytes(termByteArray.bytes(), 0, len);
term.copyBytes(SimpleTextUtil.fromBytesRefString(termByteArray.get().utf8ToString())); term.copyBytes(SimpleTextUtil.fromBytesRefString(termByteArray.get().utf8ToString()));
@ -571,7 +571,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer {
} catch (ParseException pe) { } catch (ParseException pe) {
throw new CorruptIndexException("failed to parse int length", in, pe); throw new CorruptIndexException("failed to parse int length", in, pe);
} }
term.grow(len); term.growNoCopy(len);
term.setLength(len); term.setLength(len);
in.readBytes(term.bytes(), 0, len); in.readBytes(term.bytes(), 0, len);
return term.get(); return term.get();
@ -758,7 +758,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer {
} catch (ParseException pe) { } catch (ParseException pe) {
throw new CorruptIndexException("failed to parse int length", in, pe); throw new CorruptIndexException("failed to parse int length", in, pe);
} }
term.grow(len); term.growNoCopy(len);
term.setLength(len); term.setLength(len);
in.readBytes(term.bytes(), 0, len); in.readBytes(term.bytes(), 0, len);
return term.get(); return term.get();

View File

@ -600,7 +600,7 @@ class SimpleTextFieldsReader extends FieldsProducer {
SimpleTextUtil.readLine(in, scratch); SimpleTextUtil.readLine(in, scratch);
if (StringHelper.startsWith(scratch.get(), PAYLOAD)) { if (StringHelper.startsWith(scratch.get(), PAYLOAD)) {
final int len = scratch.length() - PAYLOAD.length; final int len = scratch.length() - PAYLOAD.length;
scratch2.grow(len); scratch2.growNoCopy(len);
System.arraycopy(scratch.bytes(), PAYLOAD.length, scratch2.bytes(), 0, len); System.arraycopy(scratch.bytes(), PAYLOAD.length, scratch2.bytes(), 0, len);
scratch2.setLength(len); scratch2.setLength(len);
payload = scratch2.get(); payload = scratch2.get();
@ -727,7 +727,7 @@ class SimpleTextFieldsReader extends FieldsProducer {
} }
lastDocsStart = in.getFilePointer(); lastDocsStart = in.getFilePointer();
final int len = scratch.length() - TERM.length; final int len = scratch.length() - TERM.length;
lastTerm.grow(len); lastTerm.growNoCopy(len);
System.arraycopy(scratch.bytes(), TERM.length, lastTerm.bytes(), 0, len); System.arraycopy(scratch.bytes(), TERM.length, lastTerm.bytes(), 0, len);
lastTerm.setLength(len); lastTerm.setLength(len);
docFreq = 0; docFreq = 0;

View File

@ -150,7 +150,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
readLine(); readLine();
assert StringHelper.startsWith(scratch.get(), TERMTEXT); assert StringHelper.startsWith(scratch.get(), TERMTEXT);
int termLength = scratch.length() - TERMTEXT.length; int termLength = scratch.length() - TERMTEXT.length;
term.grow(termLength); term.growNoCopy(termLength);
term.setLength(termLength); term.setLength(termLength);
System.arraycopy(scratch.bytes(), TERMTEXT.length, term.bytes(), 0, termLength); System.arraycopy(scratch.bytes(), TERMTEXT.length, term.bytes(), 0, termLength);

View File

@ -143,7 +143,7 @@ public abstract class TermVectorsWriter implements Closeable, Accountable {
if (payload == null) { if (payload == null) {
payload = new BytesRefBuilder(); payload = new BytesRefBuilder();
} }
payload.grow(payloadLength); payload.growNoCopy(payloadLength);
positions.readBytes(payload.bytes(), 0, payloadLength); positions.readBytes(payload.bytes(), 0, payloadLength);
payload.setLength(payloadLength); payload.setLength(payloadLength);

View File

@ -505,7 +505,7 @@ class FreqProxFields extends Fields {
hasPayload = true; hasPayload = true;
// has a payload // has a payload
payload.setLength(posReader.readVInt()); payload.setLength(posReader.readVInt());
payload.grow(payload.length()); payload.growNoCopy(payload.length());
posReader.readBytes(payload.bytes(), 0, payload.length()); posReader.readBytes(payload.bytes(), 0, payload.length());
} else { } else {
hasPayload = false; hasPayload = false;

View File

@ -216,7 +216,7 @@ public final class ByteBlockPool implements Accountable {
result.offset = pos; result.offset = pos;
} else { } else {
// Uncommon case: The slice spans at least 2 blocks, so we must copy the bytes. // 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.bytes = builder.get().bytes;
result.offset = 0; result.offset = 0;
readBytes(offset, result.bytes, 0, length); readBytes(offset, result.bytes, 0, length);

View File

@ -94,7 +94,7 @@ public final class BytesRefArray implements SortableBytesRefArray {
Objects.checkIndex(index, lastElement); Objects.checkIndex(index, lastElement);
int offset = offsets[index]; int offset = offsets[index];
int length = index == lastElement - 1 ? currentOffset - offset : offsets[index + 1] - offset; int length = index == lastElement - 1 ? currentOffset - offset : offsets[index + 1] - offset;
spare.grow(length); spare.growNoCopy(length);
spare.setLength(length); spare.setLength(length);
pool.readBytes(offset, spare.bytes(), 0, spare.length()); pool.readBytes(offset, spare.bytes(), 0, spare.length());
return spare.get(); return spare.get();

View File

@ -60,6 +60,13 @@ public class BytesRefBuilder {
ref.bytes = ArrayUtil.grow(ref.bytes, capacity); 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. */ /** Append a single byte to this builder. */
public void append(byte b) { public void append(byte b) {
grow(ref.length + 1); grow(ref.length + 1);

View File

@ -77,9 +77,14 @@ public class IntsRefBuilder {
ref.ints = ArrayUtil.grow(ref.ints, newLength); 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. */ /** Copies the given array into this instance. */
public void copyInts(int[] otherInts, int otherOffset, int otherLength) { public void copyInts(int[] otherInts, int otherOffset, int otherLength) {
grow(otherLength); growNoCopy(otherLength);
System.arraycopy(otherInts, otherOffset, ref.ints, 0, otherLength); System.arraycopy(otherInts, otherOffset, ref.ints, 0, otherLength);
ref.length = otherLength; ref.length = otherLength;
} }
@ -94,7 +99,7 @@ public class IntsRefBuilder {
* UTF-8 to UTF-32 and then copied into this builder. * UTF-8 to UTF-32 and then copied into this builder.
*/ */
public void copyUTF8Bytes(BytesRef bytes) { public void copyUTF8Bytes(BytesRef bytes) {
grow(bytes.length); growNoCopy(bytes.length);
ref.length = UnicodeUtil.UTF8toUTF32(bytes, ref.ints); ref.length = UnicodeUtil.UTF8toUTF32(bytes, ref.ints);
} }

View File

@ -593,7 +593,7 @@ public class OfflineSorter {
} }
short length = in.readShort(); short length = in.readShort();
ref.grow(length); ref.growNoCopy(length);
ref.setLength(length); ref.setLength(length);
in.readBytes(ref.bytes(), 0, length); in.readBytes(ref.bytes(), 0, length);
return ref.get(); return ref.get();

View File

@ -738,7 +738,7 @@ public final class Util {
public static IntsRef toUTF16(CharSequence s, IntsRefBuilder scratch) { public static IntsRef toUTF16(CharSequence s, IntsRefBuilder scratch) {
final int charLimit = s.length(); final int charLimit = s.length();
scratch.setLength(charLimit); scratch.setLength(charLimit);
scratch.grow(charLimit); scratch.growNoCopy(charLimit);
for (int idx = 0; idx < charLimit; idx++) { for (int idx = 0; idx < charLimit; idx++) {
scratch.setIntAt(idx, s.charAt(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. */ /** Just converts IntsRef to BytesRef; you must ensure the int values fit into a byte. */
public static BytesRef toBytesRef(IntsRef input, BytesRefBuilder scratch) { public static BytesRef toBytesRef(IntsRef input, BytesRefBuilder scratch) {
scratch.grow(input.length); scratch.growNoCopy(input.length);
for (int i = 0; i < input.length; i++) { for (int i = 0; i < input.length; i++) {
int value = input.ints[i + input.offset]; int value = input.ints[i + input.offset];
// NOTE: we allow -128 to 255 // NOTE: we allow -128 to 255

View File

@ -452,7 +452,7 @@ public class AnalyzingSuggester extends Lookup {
payload = null; payload = null;
} }
buffer = ArrayUtil.grow(buffer, requiredLength); buffer = ArrayUtil.growNoCopy(buffer, requiredLength);
output.reset(buffer); output.reset(buffer);
@ -522,7 +522,7 @@ public class AnalyzingSuggester extends Lookup {
} }
input.reset(bytes.bytes, bytes.offset, bytes.length); input.reset(bytes.bytes, bytes.offset, bytes.length);
short analyzedLength = input.readShort(); short analyzedLength = input.readShort();
analyzed.grow(analyzedLength + 2); analyzed.growNoCopy(analyzedLength + 2);
input.readBytes(analyzed.bytes(), 0, analyzedLength); input.readBytes(analyzed.bytes(), 0, analyzedLength);
analyzed.setLength(analyzedLength); analyzed.setLength(analyzedLength);

View File

@ -223,7 +223,7 @@ final class CompletionFieldsConsumer extends FieldsConsumer {
ByteArrayDataInput input = ByteArrayDataInput input =
new ByteArrayDataInput(payload.bytes, payload.offset, payload.length); new ByteArrayDataInput(payload.bytes, payload.offset, payload.length);
int len = input.readVInt(); int len = input.readVInt();
scratch.grow(len); scratch.growNoCopy(len);
scratch.setLength(len); scratch.setLength(len);
input.readBytes(scratch.bytes(), 0, scratch.length()); input.readBytes(scratch.bytes(), 0, scratch.length());
long weight = input.readVInt() - 1; long weight = input.readVInt() - 1;

View File

@ -176,7 +176,7 @@ public class FSTCompletionBuilder {
"Bucket outside of the allowed range [0, " + buckets + "): " + bucket); "Bucket outside of the allowed range [0, " + buckets + "): " + bucket);
} }
scratch.grow(utf8.length + 10); scratch.growNoCopy(utf8.length + 10);
scratch.clear(); scratch.clear();
scratch.append((byte) bucket); scratch.append((byte) bucket);
scratch.append(utf8); scratch.append(utf8);

View File

@ -180,7 +180,7 @@ public class FSTCompletionLookup extends Lookup {
int inputLineCount = 0; int inputLineCount = 0;
while ((spare = iterator.next()) != null) { while ((spare = iterator.next()) != null) {
if (spare.length + 4 >= buffer.length) { if (spare.length + 4 >= buffer.length) {
buffer = ArrayUtil.grow(buffer, spare.length + 4); buffer = ArrayUtil.growNoCopy(buffer, spare.length + 4);
} }
output.reset(buffer); output.reset(buffer);

View File

@ -155,7 +155,7 @@ public class FSTTester<T> {
} }
static IntsRef toIntsRef(BytesRef br, IntsRefBuilder ir) { static IntsRef toIntsRef(BytesRef br, IntsRefBuilder ir) {
ir.grow(br.length); ir.growNoCopy(br.length);
ir.clear(); ir.clear();
for (int i = 0; i < br.length; i++) { for (int i = 0; i < br.length; i++) {
ir.append(br.bytes[br.offset + i] & 0xFF); ir.append(br.bytes[br.offset + i] & 0xFF);