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
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));

View File

@ -138,7 +138,7 @@ public final class UserDictionary implements Dictionary<UserMorphData> {
}
// 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));

View File

@ -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));

View File

@ -121,7 +121,7 @@ public final class UserDictionary implements Dictionary<UserMorphData> {
}
// 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));

View File

@ -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();

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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();

View File

@ -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);

View File

@ -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);
}

View File

@ -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();

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -155,7 +155,7 @@ public class FSTTester<T> {
}
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);