LUCENE-7743: Avoid calling new String(String).

This commit is contained in:
Adrien Grand 2017-03-28 15:25:16 +02:00 committed by Shalin Shekhar Mangar
parent 12d8de86f1
commit a0a6c22c34
10 changed files with 19 additions and 17 deletions

View File

@ -90,6 +90,9 @@ Other
* LUCENE-7751: Avoid boxing primitives only to call compareTo. * LUCENE-7751: Avoid boxing primitives only to call compareTo.
(Daniel Jelinski via Adrien Grand) (Daniel Jelinski via Adrien Grand)
* LUCENE-7743: Never call new String(String).
(Daniel Jelinski via Adrien Grand)
======================= Lucene 6.5.0 ======================= ======================= Lucene 6.5.0 =======================
API Changes API Changes

View File

@ -516,7 +516,7 @@ public class TernaryTree implements Cloneable {
@Override @Override
public String nextElement() { public String nextElement() {
String res = new String(curkey); String res = curkey;
cur = up(); cur = up();
run(); run();
return res; return res;

View File

@ -24,16 +24,16 @@ import org.apache.lucene.analysis.cn.smart.hhmm.SegTokenFilter; // for javadoc
*/ */
public class Utility { public class Utility {
public static final char[] STRING_CHAR_ARRAY = new String("未##串") public static final char[] STRING_CHAR_ARRAY = "未##串"
.toCharArray(); .toCharArray();
public static final char[] NUMBER_CHAR_ARRAY = new String("未##数") public static final char[] NUMBER_CHAR_ARRAY = "未##数"
.toCharArray(); .toCharArray();
public static final char[] START_CHAR_ARRAY = new String("始##始") public static final char[] START_CHAR_ARRAY = "始##始"
.toCharArray(); .toCharArray();
public static final char[] END_CHAR_ARRAY = new String("末##末").toCharArray(); public static final char[] END_CHAR_ARRAY = "末##末".toCharArray();
/** /**
* Delimiters will be filtered to this character by {@link SegTokenFilter} * Delimiters will be filtered to this character by {@link SegTokenFilter}

View File

@ -1154,7 +1154,7 @@ public class TestIndexWriter extends LuceneTestCase {
t.finish = true; t.finish = true;
t.join(); t.join();
if (t.failed) { if (t.failed) {
fail(new String(t.bytesLog.toString("UTF-8"))); fail(t.bytesLog.toString("UTF-8"));
} }
} }

View File

@ -24,7 +24,7 @@ import java.util.Random;
public class TestRamUsageEstimator extends LuceneTestCase { public class TestRamUsageEstimator extends LuceneTestCase {
public void testSanity() { public void testSanity() {
assertTrue(sizeOf(new String("test string")) > shallowSizeOfInstance(String.class)); assertTrue(sizeOf("test string") > shallowSizeOfInstance(String.class));
Holder holder = new Holder(); Holder holder = new Holder();
holder.holder = new Holder("string2", 5000L); holder.holder = new Holder("string2", 5000L);
@ -37,9 +37,9 @@ public class TestRamUsageEstimator extends LuceneTestCase {
shallowSizeOfInstance(Holder.class) == shallowSizeOfInstance(HolderSubclass2.class)); shallowSizeOfInstance(Holder.class) == shallowSizeOfInstance(HolderSubclass2.class));
String[] strings = new String[] { String[] strings = new String[] {
new String("test string"), "test string",
new String("hollow"), "hollow",
new String("catchmaster") "catchmaster"
}; };
assertTrue(sizeOf(strings) > shallowSizeOf(strings)); assertTrue(sizeOf(strings) > shallowSizeOf(strings));
} }

View File

@ -47,7 +47,7 @@ public class AbstractDIHCacheTestCase {
@Before @Before
public void setup() { public void setup() {
try { try {
APPLE = new SerialClob(new String("Apples grow on trees and they are good to eat.").toCharArray()); APPLE = new SerialClob("Apples grow on trees and they are good to eat.".toCharArray());
} catch (SQLException sqe) { } catch (SQLException sqe) {
Assert.fail("Could not Set up Test"); Assert.fail("Could not Set up Test");
} }

View File

@ -733,9 +733,8 @@ public class DefaultSolrHighlighter extends SolrHighlighter implements PluginInf
if( alternateFieldLen <= 0 ){ if( alternateFieldLen <= 0 ){
altList.add(encoder.encodeText(altText)); altList.add(encoder.encodeText(altText));
} else{ } else{
//note: seemingly redundant new String(...) releases memory to the larger text. But is copying better?
altList.add( len + altText.length() > alternateFieldLen ? altList.add( len + altText.length() > alternateFieldLen ?
encoder.encodeText(new String(altText.substring( 0, alternateFieldLen - len ))) : encoder.encodeText(altText.substring(0, alternateFieldLen - len)) :
encoder.encodeText(altText) ); encoder.encodeText(altText) );
len += altText.length(); len += altText.length();
if( len >= alternateFieldLen ) break; if( len >= alternateFieldLen ) break;

View File

@ -272,7 +272,7 @@ public class ZkCLITest extends SolrTestCaseJ4 {
@Test @Test
public void testGet() throws Exception { public void testGet() throws Exception {
String getNode = "/getNode"; String getNode = "/getNode";
byte [] data = new String("getNode-data").getBytes(StandardCharsets.UTF_8); byte [] data = "getNode-data".getBytes(StandardCharsets.UTF_8);
this.zkClient.create(getNode, data, CreateMode.PERSISTENT, true); this.zkClient.create(getNode, data, CreateMode.PERSISTENT, true);
String[] args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd", String[] args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd",
"get", getNode}; "get", getNode};
@ -284,7 +284,7 @@ public class ZkCLITest extends SolrTestCaseJ4 {
File tmpDir = createTempDir().toFile(); File tmpDir = createTempDir().toFile();
String getNode = "/getFileNode"; String getNode = "/getFileNode";
byte [] data = new String("getFileNode-data").getBytes(StandardCharsets.UTF_8); byte [] data = "getFileNode-data".getBytes(StandardCharsets.UTF_8);
this.zkClient.create(getNode, data, CreateMode.PERSISTENT, true); this.zkClient.create(getNode, data, CreateMode.PERSISTENT, true);
File file = new File(tmpDir, File file = new File(tmpDir,

View File

@ -79,7 +79,7 @@ public class TestSolrQueryResponse extends LuceneTestCase {
final SolrQueryResponse response = new SolrQueryResponse(); final SolrQueryResponse response = new SolrQueryResponse();
assertEquals("response initial value", null, response.getResponse()); assertEquals("response initial value", null, response.getResponse());
final Object newValue = (random().nextBoolean() final Object newValue = (random().nextBoolean()
? (random().nextBoolean() ? new String("answer") : new Integer(42)) : null); ? (random().nextBoolean() ? "answer" : Integer.valueOf(42)) : null);
response.addResponse(newValue); response.addResponse(newValue);
assertEquals("response new value", newValue, response.getResponse()); assertEquals("response new value", newValue, response.getResponse());
} }

View File

@ -26,7 +26,7 @@ import org.apache.solr.search.QParser;
public class TrieIntPrefixActsAsRangeQueryFieldType extends TrieIntField { public class TrieIntPrefixActsAsRangeQueryFieldType extends TrieIntField {
public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) { public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) {
return getRangeQuery(parser, sf, termStr, new String(Integer.MAX_VALUE + ""), true, false); return getRangeQuery(parser, sf, termStr, Integer.MAX_VALUE + "", true, false);
} }
} }