From c141ef400975be45469a0f7e4fcb2d9c5d9e6a1b Mon Sep 17 00:00:00 2001 From: Kui LIU Date: Wed, 11 Oct 2017 15:01:48 +0200 Subject: [PATCH 1/2] Fix the problem of using '+=' operator to concatenate strings a in a loop. The method is building a String using concatenation in a loop. In each iteration, the String is converted to a StringBuilder, appended to, and converted back to a String. This can lead to a cost quadratic in the number of iterations, as the growing string is recopied in each iteration. Better performance can be obtained by using a StringBuilder explicitly. http://findbugs.sourceforge.net/bugDescriptions.html#SBSC_USE_STRINGBUFFER_CONCATENATION --- src/main/org/apache/tools/ant/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index b104fd33a..a7d477671 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -1292,9 +1292,9 @@ public class Main implements AntMain { // now, start printing the targets and their descriptions final String lSep = System.getProperty("line.separator"); // got a bit annoyed that I couldn't find a pad function - String spaces = " "; + StringBuilder spaces = new StringBuilder(" "); while (spaces.length() <= maxlen) { - spaces += spaces; + spaces.append(spaces); } final StringBuilder msg = new StringBuilder(); msg.append(heading).append(lSep).append(lSep); From 013e9159e927cf96493a5f8577cfd2807cb045c8 Mon Sep 17 00:00:00 2001 From: Kui LIU Date: Wed, 11 Oct 2017 14:18:19 +0200 Subject: [PATCH 2/2] Fix the inefficient use of keySet iterator with entrySet iterator. The current source code accesses the key and value of a Hashtable entry, using a key that is retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the Hashtable, to avoid the Map.get(key) lookup. --- src/main/org/apache/tools/ant/filters/ReplaceTokens.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/org/apache/tools/ant/filters/ReplaceTokens.java b/src/main/org/apache/tools/ant/filters/ReplaceTokens.java index 74da64a16..bcbc312cd 100644 --- a/src/main/org/apache/tools/ant/filters/ReplaceTokens.java +++ b/src/main/org/apache/tools/ant/filters/ReplaceTokens.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.Reader; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map; import java.util.Properties; import java.util.SortedMap; import java.util.TreeMap; @@ -116,8 +117,8 @@ public final class ReplaceTokens if (!resolvedTokensBuilt) { // build the resolved tokens tree map. - for (String key : hash.keySet()) { - resolvedTokens.put(beginToken + key + endToken, hash.get(key)); + for (Map.Entry entry : hash.entrySet()) { + resolvedTokens.put(beginToken + entry.getKey() + endToken, entry.getValue()); } resolvedTokensBuilt = true; }