Skip to content

Commit 24e27b2

Browse files
author
CogComp Dev
authored
Merge pull request #689 from schen149/master
Fixing inefficient string concat in core-utilities
2 parents bce700c + 7f6eef0 commit 24e27b2

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

core-utilities/src/main/java/edu/illinois/cs/cogcomp/annotation/BasicTextAnnotationBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ public static TextAnnotation createTextAnnotationFromListofListofTokens(List<Lis
7979
public static TextAnnotation createTextAnnotationFromTokens(String corpusId, String textId,
8080
List<String[]> tokenizedSentences) {
8181
Tokenization tokenization = tokenizeTextSpan(tokenizedSentences);
82-
String text = "";
82+
StringBuilder text = new StringBuilder();
8383
for (String[] sentenceTokens : tokenizedSentences)
84-
text += StringUtils.join(sentenceTokens, ' ') + System.lineSeparator();
84+
text.append(StringUtils.join(sentenceTokens, ' '))
85+
.append(System.lineSeparator());
8586

86-
return new TextAnnotation(corpusId, textId, text, tokenization.getCharacterOffsets(),
87+
return new TextAnnotation(corpusId, textId, text.toString(), tokenization.getCharacterOffsets(),
8788
tokenization.getTokens(), tokenization.getSentenceEndTokenIndexes());
8889
}
8990

core-utilities/src/main/java/edu/illinois/cs/cogcomp/core/datastructures/trees/Tree.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,27 +297,29 @@ public boolean hasEdgeLabels() {
297297
*/
298298

299299
private String toString(boolean firstChild, int spaces) {
300-
String treeString = "";
300+
StringBuilder treeString = new StringBuilder();
301301
if (!firstChild) {
302302
for (int i = 0; i < spaces; i++) {
303-
treeString += " ";
303+
treeString.append(" ");
304304
}
305305
}
306306

307307
if (!this.isLeaf())
308-
treeString += "(";
308+
treeString.append("(");
309309

310310
int position = this.getPositionAmongParentsChildren();
311311

312312
if (this.parent != null && this.parent.childrenEdgeLabels != null
313313
&& this.parent.childrenEdgeLabels.get(position) != null) {
314-
treeString += (":LABEL:" + this.parent.childrenEdgeLabels.get(position) + " ");
314+
treeString.append(":LABEL:")
315+
.append(this.parent.childrenEdgeLabels.get(position))
316+
.append(" ");
315317
}
316318

317-
treeString += label.toString();
319+
treeString.append(label.toString());
318320

319321
if (children.size() > 0)
320-
treeString += " ";
322+
treeString.append(" ");
321323

322324
int len = treeString.length();
323325

@@ -326,19 +328,19 @@ private String toString(boolean firstChild, int spaces) {
326328
for (Iterator<Tree<T>> iterator = children.iterator(); iterator.hasNext();) {
327329
Tree<T> child = iterator.next();
328330

329-
treeString += child.toString(first, len);
331+
treeString.append(child.toString(first, len));
330332

331333
first = false;
332334

333335
if (iterator.hasNext())
334-
treeString += "\n";
336+
treeString.append("\n");
335337
index++;
336338
}
337339

338340
if (!this.isLeaf())
339-
treeString += ")";
341+
treeString.append(")");
340342

341-
return treeString;
343+
return treeString.toString();
342344

343345
}
344346

core-utilities/src/main/java/edu/illinois/cs/cogcomp/core/utilities/Table.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public String toTextTable() {
8282
}
8383
}
8484

85-
String formatterString = "";
85+
StringBuilder formatterString = new StringBuilder();
8686
for (int i = 0; i < formatterList.size(); i ++) {
87-
formatterString += formatterList.get(i);
87+
formatterString.append(formatterList.get(i));
8888
}
8989

9090
int lenColSep = 0;
@@ -99,7 +99,7 @@ public String toTextTable() {
9999
buffer.append(colSep);
100100
buffer.append("|\n");
101101
}
102-
String appendStr = String.format(formatterString, (allTableRows.get(i)).toArray());
102+
String appendStr = String.format(formatterString.toString(), (allTableRows.get(i)).toArray());
103103
buffer.append(appendStr);
104104
if (i == 0) {
105105
lenColSep = appendStr.length();

core-utilities/src/test/java/edu/illinois/cs/cogcomp/nlp/utilities/TextCleanerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ public void testCleanLongText() {
159159
fail(e.getMessage());
160160
}
161161

162-
String badText = "";
162+
StringBuilder badText = new StringBuilder();
163163

164164
for (String line : badTextLines)
165-
badText += line + "\n";
165+
badText.append(line).append("\n");
166166

167-
String cleanText = TextCleaner.replaceControlSequence(badText);
167+
String cleanText = TextCleaner.replaceControlSequence(badText.toString());
168168

169169
boolean isGood = !(cleanText.contains("^@"));
170170

0 commit comments

Comments
 (0)