Skip to content

Commit

Permalink
LUCENE-7743: Avoid calling new String(String).
Browse files Browse the repository at this point in the history
  • Loading branch information
jpountz committed Mar 28, 2017
1 parent c189139 commit 390ef9a
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 17 deletions.
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Other
* LUCENE-7751: Avoid boxing primitives only to call compareTo.
(Daniel Jelinski via Adrien Grand)

* LUCENE-7743: Never call new String(String).
(Daniel Jelinski via Adrien Grand)

======================= Lucene 6.5.0 =======================

API Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public void rewind() {

@Override
public String nextElement() {
String res = new String(curkey);
String res = curkey;
cur = up();
run();
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
*/
public class Utility {

public static final char[] STRING_CHAR_ARRAY = new String("未##串")
public static final char[] STRING_CHAR_ARRAY = "未##串"
.toCharArray();

public static final char[] NUMBER_CHAR_ARRAY = new String("未##数")
public static final char[] NUMBER_CHAR_ARRAY = "未##数"
.toCharArray();

public static final char[] START_CHAR_ARRAY = new String("始##始")
public static final char[] START_CHAR_ARRAY = "始##始"
.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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ public void testThreadInterruptDeadlock() throws Exception {
t.finish = true;
t.join();
if (t.failed) {
fail(new String(t.bytesLog.toString("UTF-8")));
fail(t.bytesLog.toString("UTF-8"));
}
}

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

public class TestRamUsageEstimator extends LuceneTestCase {
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("string2", 5000L);
Expand All @@ -37,9 +37,9 @@ public void testSanity() {
shallowSizeOfInstance(Holder.class) == shallowSizeOfInstance(HolderSubclass2.class));

String[] strings = new String[] {
new String("test string"),
new String("hollow"),
new String("catchmaster")
"test string",
"hollow",
"catchmaster"
};
assertTrue(sizeOf(strings) > shallowSizeOf(strings));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AbstractDIHCacheTestCase {
@Before
public void setup() {
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) {
Assert.fail("Could not Set up Test");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,8 @@ protected Object alternateField(Document doc, int docId, String fieldName, FvhCo
if( alternateFieldLen <= 0 ){
altList.add(encoder.encodeText(altText));
} else{
//note: seemingly redundant new String(...) releases memory to the larger text. But is copying better?
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) );
len += altText.length();
if( len >= alternateFieldLen ) break;
Expand Down
4 changes: 2 additions & 2 deletions solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void testUpConfigLinkConfigClearZk() throws Exception {
@Test
public void testGet() throws Exception {
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);
String[] args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd",
"get", getNode};
Expand All @@ -284,7 +284,7 @@ public void testGetFile() throws Exception {
File tmpDir = createTempDir().toFile();

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

File file = new File(tmpDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testResponse() throws Exception {
final SolrQueryResponse response = new SolrQueryResponse();
assertEquals("response initial value", null, response.getResponse());
final Object newValue = (random().nextBoolean()
? (random().nextBoolean() ? new String("answer") : new Integer(42)) : null);
? (random().nextBoolean() ? "answer" : Integer.valueOf(42)) : null);
response.addResponse(newValue);
assertEquals("response new value", newValue, response.getResponse());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class TrieIntPrefixActsAsRangeQueryFieldType extends TrieIntField {

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

}

0 comments on commit 390ef9a

Please sign in to comment.