Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use growNoCopy in some places #12951

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private TokenInfoDictionaryWriter buildDictionary(List<Path> csvFiles) throws IO
// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public int compare(String[] left, String[] right) {
}
// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private TokenInfoDictionaryWriter buildDictionary(List<Path> csvFiles) throws IO
// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private UserDictionary(List<String> entries) throws IOException {
}

// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public BytesRef apply(int docID) {
} 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()));
Expand Down Expand Up @@ -571,7 +571,7 @@ public BytesRef lookupOrd(int ord) throws IOException {
} 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();
Expand Down Expand Up @@ -758,7 +758,7 @@ public BytesRef lookupOrd(long ord) throws IOException {
} 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public int nextPosition() throws IOException {
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();
Expand Down Expand Up @@ -727,7 +727,7 @@ private void loadTerms() throws IOException {
}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Fields get(int doc) throws IOException {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void addProx(int numProx, DataInput positions, DataInput offsets) throws
if (payload == null) {
payload = new BytesRefBuilder();
}
payload.grow(payloadLength);
payload.growNoCopy(payloadLength);

positions.readBytes(payload.bytes(), 0, payloadLength);
payload.setLength(payloadLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public int nextPosition() throws IOException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void setBytesRef(BytesRefBuilder builder, BytesRef result, long offset, int leng
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public BytesRef get(BytesRefBuilder spare, int index) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public void grow(int 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. */
public void append(byte b) {
grow(ref.length + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ public void grow(int 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. */
public void copyInts(int[] otherInts, int otherOffset, int otherLength) {
grow(otherLength);
growNoCopy(otherLength);
System.arraycopy(otherInts, otherOffset, ref.ints, 0, otherLength);
ref.length = otherLength;
}
Expand All @@ -94,7 +99,7 @@ public void copyInts(IntsRef ints) {
* 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public BytesRef next() throws IOException {
}

short length = in.readShort();
ref.grow(length);
ref.growNoCopy(length);
ref.setLength(length);
in.readBytes(ref.bytes(), 0, length);
return ref.get();
Expand Down
4 changes: 2 additions & 2 deletions lucene/core/src/java/org/apache/lucene/util/fst/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ private static String printableLabel(int label) {
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));
}
Expand Down Expand Up @@ -794,7 +794,7 @@ public static IntsRef toIntsRef(BytesRef input, IntsRefBuilder scratch) {

/** 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public void build(InputIterator iterator) throws IOException {
payload = null;
}

buffer = ArrayUtil.grow(buffer, requiredLength);
buffer = ArrayUtil.growNoCopy(buffer, requiredLength);

output.reset(buffer);

Expand Down Expand Up @@ -522,7 +522,7 @@ public void build(InputIterator iterator) throws IOException {
}
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void write(BytesRef term, TermsEnum termsEnum) throws IOException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void add(BytesRef utf8, int bucket) throws IOException {
"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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void build(InputIterator iterator) throws IOException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static IntsRef toIntsRefUTF32(String s, IntsRefBuilder ir) {
}

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);
Expand Down
Loading