Skip to content

Commit

Permalink
Avoid allocating redundant Strings (apache#13085)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabi0 authored and stefanvodita committed Feb 23, 2024
1 parent e5058e7 commit cbea492
Show file tree
Hide file tree
Showing 29 changed files with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void afterSuite(final TestDescriptor suite, TestResult result) {

if (echoOutput && !verboseMode) {
synchronized (this) {
System.out.println("");
System.out.println();
System.out.println(suite.getClassName() + " > test suite's output saved to " + outputLog + ", copied below:");
try (BufferedReader reader = Files.newBufferedReader(outputLog, StandardCharsets.UTF_8)) {
char[] buf = new char[1024];
Expand Down
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Optimizations

* GITHUB#13115: Short circuit queued flush check when flush on update is disabled (Prabhat Sharma)

* GITHUB#13085: Remove unnecessary toString() / substring() calls to save some String allocations (Dmitry Cherniachenko)

Bug Fixes
---------------------
(No changes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private boolean suffix(String value, String suffix) {
return false;
}

return value.substring(value.length() - suffix.length()).equals(suffix);
return value.endsWith(suffix);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected String readToken(StringBuilder chars) {
break;
}
}
token.append(chars.toString().substring(0, i));
token.append(chars, 0, i);
// chars.delete(0,i);
for (int countr = i; countr < chars.length(); countr++) {
chars.setCharAt(countr - i, chars.charAt(countr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public static void assertHTMLStripsTo(Reader input, String gold, Set<String> esc
builder.append((char) ch);
}
} catch (Exception e) {
if (gold.equals(builder.toString())) {
if (gold.contentEquals(builder)) {
throw e;
}
throw new Exception(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,13 @@
public class TestTrimFilter extends BaseTokenStreamTestCase {

public void testTrim() throws Exception {
char[] a = " a ".toCharArray();
char[] b = "b ".toCharArray();
char[] ccc = "cCc".toCharArray();
char[] whitespace = " ".toCharArray();
char[] empty = "".toCharArray();

TokenStream ts =
new CannedTokenStream(
new Token(new String(a, 0, a.length), 1, 5),
new Token(new String(b, 0, b.length), 6, 10),
new Token(new String(ccc, 0, ccc.length), 11, 15),
new Token(new String(whitespace, 0, whitespace.length), 16, 20),
new Token(new String(empty, 0, empty.length), 21, 21));
new Token(" a ", 1, 5),
new Token("b ", 6, 10),
new Token("cCc", 11, 15),
new Token(" ", 16, 20),
new Token("", 21, 21));
ts = new TrimFilter(ts);

assertTokenStreamContents(ts, new String[] {"a", "b", "cCc", "", ""});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ private void checkOutput(
indexMatched.append((cs.correctOffset(i) < 0 ? "-" : input.charAt(cs.correctOffset(i))));
}

boolean outputGood = expectedOutput.equals(output.toString());
boolean indexMatchedGood = expectedIndexMatchedOutput.equals(indexMatched.toString());
boolean outputGood = expectedOutput.contentEquals(output);
boolean indexMatchedGood = expectedIndexMatchedOutput.contentEquals(indexMatched);

if (!outputGood || !indexMatchedGood || false) {
System.out.println("Pattern : " + pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ String[] next() throws NoMoreDataException {
String time(String original) {
StringBuilder buffer = new StringBuilder();

buffer.append(original.substring(8, 10));
buffer.append(original, 8, 10);
buffer.append('-');
buffer.append(months[Integer.parseInt(original.substring(5, 7)) - 1]);
buffer.append('-');
buffer.append(original.substring(0, 4));
buffer.append(original, 0, 4);
buffer.append(' ');
buffer.append(original.substring(11, 19));
buffer.append(original, 11, 19);
buffer.append(".000");

return buffer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DocData parse(
docData.setName(name);
docData.setDate(date);
docData.setTitle(title);
docData.setBody(stripTags(docBuf, mark).toString());
docData.setBody(stripTags(docBuf, mark));
return docData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public DocData parse(
// date...
String dateStr = extract(docBuf, DATE, DATE_END, h2, DATE_NOISE_PREFIXES);
if (dateStr != null) {
dateStr = stripTags(dateStr, 0).toString();
dateStr = stripTags(dateStr, 0);
date = trecSrc.parseDate(dateStr.trim());
}
}
docData.clear();
docData.setName(name);
docData.setDate(date);
docData.setBody(stripTags(docBuf, mark).toString());
docData.setBody(stripTags(docBuf, mark));
return docData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public DocData parse(
docData.setName(name);
docData.setDate(date);
docData.setTitle(title);
docData.setBody(stripTags(docBuf, mark).toString());
docData.setBody(stripTags(docBuf, mark));
return docData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DocData parse(
if (d2a > 0) {
dateStr = dateStr.substring(0, d2a + 3); // we need the "day" part
}
dateStr = stripTags(dateStr, 0).toString();
dateStr = stripTags(dateStr, 0);
date = trecSrc.parseDate(dateStr.trim());
}

Expand All @@ -59,14 +59,14 @@ public DocData parse(
title = extract(docBuf, HEADLINE, HEADLINE_END, -1, null);
}
if (title != null) {
title = stripTags(title, 0).toString().trim();
title = stripTags(title, 0).trim();
}

docData.clear();
docData.setName(name);
docData.setDate(date);
docData.setTitle(title);
docData.setBody(stripTags(docBuf, mark).toString());
docData.setBody(stripTags(docBuf, mark));
return docData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void setParams(String sortField) {
String typeString;
if (index != -1) {
fieldName = field.substring(0, index);
typeString = field.substring(1 + index, field.length());
typeString = field.substring(1 + index);
} else {
throw new RuntimeException("You must specify the sort type ie page:int,subject:string");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void readField(BytesRef type, FieldInfo fieldInfo, StoredFieldVisitor vi
if (type == TYPE_STRING) {
byte[] bytes = new byte[scratch.length() - VALUE.length];
System.arraycopy(scratch.bytes(), VALUE.length, bytes, 0, bytes.length);
visitor.stringField(fieldInfo, new String(bytes, 0, bytes.length, StandardCharsets.UTF_8));
visitor.stringField(fieldInfo, new String(bytes, StandardCharsets.UTF_8));
} else if (type == TYPE_BINARY) {
byte[] copy = new byte[scratch.length() - VALUE.length];
System.arraycopy(scratch.bytes(), VALUE.length, copy, 0, copy.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public static String checkIndexHeaderSuffix(DataInput in, String expectedSuffix)
int suffixLength = in.readByte() & 0xFF;
byte[] suffixBytes = new byte[suffixLength];
in.readBytes(suffixBytes, 0, suffixBytes.length);
String suffix = new String(suffixBytes, 0, suffixBytes.length, StandardCharsets.UTF_8);
String suffix = new String(suffixBytes, StandardCharsets.UTF_8);
if (!suffix.equals(expectedSuffix)) {
throw new CorruptIndexException(
"file mismatch, expected suffix=" + expectedSuffix + ", got=" + suffix, in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static String getExtension(String filename) {
if (idx == -1) {
return null;
} else {
return filename.substring(idx + 1, filename.length());
return filename.substring(idx + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,16 +677,11 @@ public void testNormalize() {
public void testMaxTokenLengthDefault() throws Exception {
StandardAnalyzer a = new StandardAnalyzer();

StringBuilder bToken = new StringBuilder();
// exact max length:
for (int i = 0; i < StandardAnalyzer.DEFAULT_MAX_TOKEN_LENGTH; i++) {
bToken.append('b');
}

String bString = bToken.toString();
String bString = "b".repeat(StandardAnalyzer.DEFAULT_MAX_TOKEN_LENGTH);
// first bString is exact max default length; next one is 1 too long
String input = "x " + bString + " " + bString + "b";
assertAnalyzesTo(a, input.toString(), new String[] {"x", bString, bString, "b"});
assertAnalyzesTo(a, input, new String[] {"x", bString, bString, "b"});
a.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -289,12 +288,10 @@ private void performTest(Directory dir) throws Exception {
reader.close();
}

static final Charset utf8 = StandardCharsets.UTF_8;

private void generateRandomData(byte[] data) {
// this test needs the random data to be valid unicode
String s = TestUtil.randomFixedByteLengthUnicodeString(random(), data.length);
byte[] b = s.getBytes(utf8);
byte[] b = s.getBytes(StandardCharsets.UTF_8);
assert b.length == data.length;
System.arraycopy(b, 0, data, 0, b.length);
}
Expand Down Expand Up @@ -493,7 +490,7 @@ private class PoolingPayloadTokenStream extends TokenStream {
this.pool = pool;
payload = pool.get();
generateRandomData(payload);
term = new String(payload, 0, payload.length, utf8);
term = new String(payload, StandardCharsets.UTF_8);
first = true;
payloadAtt = addAttribute(PayloadAttribute.class);
termAtt = addAttribute(CharTermAttribute.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected AcceptStatus accept(BytesRef term) throws IOException {

@Override
public String toString(String field) {
return field.toString() + ":" + prefix.toString();
return field + ":" + prefix;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected AcceptStatus accept(BytesRef term) throws IOException {

@Override
public String toString(String field) {
return field.toString() + automaton.toString();
return field + automaton;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ private Automaton transpositionsOf(String s) {
List<Automaton> list = new ArrayList<>();
for (int i = 0; i < s.length() - 1; i++) {
StringBuilder sb = new StringBuilder();
sb.append(s.substring(0, i));
sb.append(s, 0, i);
sb.append(s.charAt(i + 1));
sb.append(s.charAt(i));
sb.append(s.substring(i + 2, s.length()));
sb.append(s, i + 2, s.length());
String st = sb.toString();
if (!st.equals(s)) {
list.add(Automata.makeString(st));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected String checkRandomExpression(String docValue) {

// Add any head to the result, unchanged
if (substitutionPoint > 0) {
result.append(docValue.substring(0, substitutionPoint));
result.append(docValue, 0, substitutionPoint);
}

// Modify the middle...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ public void stringField(FieldInfo fieldInfo, String value) throws IOException {
curValueBuilder.append(curValue);
}
curValueBuilder.append(valueSeparator);
curValueBuilder.append(value.substring(0, Math.min(lengthBudget - 1, value.length())));
curValueBuilder.append(value, 0, Math.min(lengthBudget - 1, value.length()));
values[currentField] = curValueBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void checkNrHits() {
public void doTest() throws Exception {

if (verbose) {
System.out.println("");
System.out.println();
System.out.println("Query: " + queryText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public String toString() {
if (pointsOnly) str.append(",pointsOnly");
if (pruneLeafyBranches) str.append(",pruneLeafyBranches");
if (prefixGridScanLevel != grid.getMaxLevels() - 4)
str.append(",prefixGridScanLevel:").append("").append(prefixGridScanLevel);
str.append(",prefixGridScanLevel:").append(prefixGridScanLevel);
if (!multiOverlappingIndexedShapes) str.append(",!multiOverlappingIndexedShapes");
return str.append(')').toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ protected void addPrefixMatch(
return;
}
sb.append("<b>");
sb.append(surface.substring(0, prefixToken.length()));
sb.append(surface, 0, prefixToken.length());
sb.append("</b>");
sb.append(surface.substring(prefixToken.length()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ private static String hilite(boolean lastPrefix, String[] inputTerms, String[] q
b.append("<b>");
b.append(queryTerm);
b.append("</b>");
b.append(inputTerm.substring(queryTerm.length(), inputTerm.length()));
b.append(inputTerm.substring(queryTerm.length()));
matched = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public static void longToEnglish(long i, StringBuilder result) {
result.append("one ");
break;
case 0:
result.append("");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public Document nextDoc() throws IOException {
throw new RuntimeException("line: [" + line + "] is in an invalid format !");
}

docState.body.setStringValue(line.substring(1 + spot2, line.length()));
docState.body.setStringValue(line.substring(1 + spot2));
final String title = line.substring(0, spot);
docState.title.setStringValue(title);
docState.titleTokenized.setStringValue(title);
Expand Down

0 comments on commit cbea492

Please sign in to comment.