mirror of https://github.com/apache/lucene.git
Avoid allocating redundant Strings (#13085)
This commit is contained in:
parent
61f322905a
commit
8f759d5bf6
|
@ -113,7 +113,7 @@ public class ErrorReportingTestListener implements TestOutputListener, TestListe
|
|||
|
||||
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];
|
||||
|
|
|
@ -200,6 +200,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)
|
||||
|
|
|
@ -278,7 +278,7 @@ class BrazilianStemmer {
|
|||
return false;
|
||||
}
|
||||
|
||||
return value.substring(value.length() - suffix.length()).equals(suffix);
|
||||
return value.endsWith(suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -142,7 +142,7 @@ public class PatternParser extends DefaultHandler {
|
|||
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));
|
||||
|
|
|
@ -669,7 +669,7 @@ public class TestHTMLStripCharFilter extends BaseTokenStreamTestCase {
|
|||
builder.append((char) ch);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (gold.equals(builder.toString())) {
|
||||
if (gold.contentEquals(builder)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Exception(
|
||||
|
|
|
@ -30,19 +30,13 @@ import org.apache.lucene.tests.analysis.Token;
|
|||
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", "", ""});
|
||||
|
|
|
@ -82,8 +82,8 @@ public class TestPatternReplaceCharFilter extends BaseTokenStreamTestCase {
|
|||
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);
|
||||
|
|
|
@ -112,13 +112,13 @@ public class EnwikiContentSource extends ContentSource {
|
|||
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();
|
||||
|
|
|
@ -60,7 +60,7 @@ public class TrecFBISParser extends TrecDocParser {
|
|||
docData.setName(name);
|
||||
docData.setDate(date);
|
||||
docData.setTitle(title);
|
||||
docData.setBody(stripTags(docBuf, mark).toString());
|
||||
docData.setBody(stripTags(docBuf, mark));
|
||||
return docData;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,14 +53,14 @@ public class TrecFR94Parser extends TrecDocParser {
|
|||
// 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ public class TrecFTParser extends TrecDocParser {
|
|||
docData.setName(name);
|
||||
docData.setDate(date);
|
||||
docData.setTitle(title);
|
||||
docData.setBody(stripTags(docBuf, mark).toString());
|
||||
docData.setBody(stripTags(docBuf, mark));
|
||||
return docData;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class TrecLATimesParser extends TrecDocParser {
|
|||
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());
|
||||
}
|
||||
|
||||
|
@ -59,14 +59,14 @@ public class TrecLATimesParser extends TrecDocParser {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public class SearchWithSortTask extends ReadTask {
|
|||
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");
|
||||
}
|
||||
|
|
|
@ -169,7 +169,7 @@ public class SimpleTextStoredFieldsReader extends StoredFieldsReader {
|
|||
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);
|
||||
|
|
|
@ -380,7 +380,7 @@ public final class CodecUtil {
|
|||
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);
|
||||
|
|
|
@ -191,7 +191,7 @@ public final class IndexFileNames {
|
|||
if (idx == -1) {
|
||||
return null;
|
||||
} else {
|
||||
return filename.substring(idx + 1, filename.length());
|
||||
return filename.substring(idx + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -677,16 +677,11 @@ public class TestStandardAnalyzer extends BaseTokenStreamTestCase {
|
|||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.apache.lucene.index;
|
|||
|
||||
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;
|
||||
|
@ -289,12 +288,10 @@ public class TestPayloads extends LuceneTestCase {
|
|||
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);
|
||||
}
|
||||
|
@ -493,7 +490,7 @@ public class TestPayloads extends LuceneTestCase {
|
|||
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);
|
||||
|
|
|
@ -107,7 +107,7 @@ public class TestPrefixRandom extends LuceneTestCase {
|
|||
|
||||
@Override
|
||||
public String toString(String field) {
|
||||
return field.toString() + ":" + prefix.toString();
|
||||
return field + ":" + prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -143,7 +143,7 @@ public class TestRegexpRandom2 extends LuceneTestCase {
|
|||
|
||||
@Override
|
||||
public String toString(String field) {
|
||||
return field.toString() + automaton.toString();
|
||||
return field + automaton;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -213,10 +213,10 @@ public class TestLevenshteinAutomata extends LuceneTestCase {
|
|||
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));
|
||||
|
|
|
@ -119,7 +119,7 @@ public class TestRegExp extends LuceneTestCase {
|
|||
|
||||
// Add any head to the result, unchanged
|
||||
if (substitutionPoint > 0) {
|
||||
result.append(docValue.substring(0, substitutionPoint));
|
||||
result.append(docValue, 0, substitutionPoint);
|
||||
}
|
||||
|
||||
// Modify the middle...
|
||||
|
|
|
@ -1398,7 +1398,7 @@ public class UnifiedHighlighter {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ public class BooleanQueryTestFacade {
|
|||
public void doTest() throws Exception {
|
||||
|
||||
if (verbose) {
|
||||
System.out.println("");
|
||||
System.out.println();
|
||||
System.out.println("Query: " + queryText);
|
||||
}
|
||||
|
||||
|
|
|
@ -113,7 +113,7 @@ public class RecursivePrefixTreeStrategy extends PrefixTreeStrategy {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -927,7 +927,7 @@ public class AnalyzingInfixSuggester extends Lookup implements Closeable {
|
|||
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()));
|
||||
}
|
||||
|
|
|
@ -892,7 +892,7 @@ public class TestAnalyzingInfixSuggester extends LuceneTestCase {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -166,7 +166,6 @@ public final class English {
|
|||
result.append("one ");
|
||||
break;
|
||||
case 0:
|
||||
result.append("");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -307,7 +307,7 @@ public class LineFileDocs implements Closeable {
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue