8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
authorigerasim
Thu, 02 Jul 2015 00:26:35 +0300
changeset 31471 ae27c6f1d8bf
parent 31470 93708c7917fc
child 31472 850f01aa6a87
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument Reviewed-by: martin
jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Attribute.java
jdk/src/java.base/share/classes/java/io/StringWriter.java
jdk/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
jdk/src/java.base/share/classes/java/net/URI.java
jdk/src/java.base/share/classes/java/text/MergeCollation.java
jdk/src/java.base/share/classes/java/text/MessageFormat.java
jdk/src/java.base/share/classes/sun/invoke/util/BytecodeName.java
jdk/src/java.base/share/classes/sun/net/www/ParseUtil.java
jdk/src/java.base/share/classes/sun/text/normalizer/UnicodeSet.java
jdk/src/java.base/share/classes/sun/util/BuddhistCalendar.java
jdk/src/java.base/unix/classes/java/io/UnixFileSystem.java
jdk/src/java.base/windows/classes/java/io/WinNTFileSystem.java
--- a/jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Attribute.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Attribute.java	Thu Jul 02 00:26:35 2015 +0300
@@ -1235,7 +1235,7 @@
         int sofar = 0;  // how far have we processed the layout?
         for (;;) {
             // for each dash, collect everything up to the dash
-            result.append(layout.substring(sofar, dash));
+            result.append(layout, sofar, dash);
             sofar = dash+1;  // skip the dash
             // then collect intermediate values
             int value0 = parseIntBefore(layout, dash);
@@ -1249,7 +1249,7 @@
             dash = findCaseDash(layout, sofar);
             if (dash < 0)  break;
         }
-        result.append(layout.substring(sofar));  // collect the rest
+        result.append(layout, sofar, layout.length());  // collect the rest
         return result.toString();
     }
     static {
--- a/jdk/src/java.base/share/classes/java/io/StringWriter.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/io/StringWriter.java	Thu Jul 02 00:26:35 2015 +0300
@@ -109,7 +109,7 @@
      * @param  len  Number of characters to write
      */
     public void write(String str, int off, int len)  {
-        buf.append(str.substring(off, off + len));
+        buf.append(str, off, off + len);
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/lang/AbstractStringBuilder.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/lang/AbstractStringBuilder.java	Thu Jul 02 00:26:35 2015 +0300
@@ -515,8 +515,12 @@
                 + s.length());
         int len = end - start;
         ensureCapacityInternal(count + len);
-        for (int i = start, j = count; i < end; i++, j++)
-            value[j] = s.charAt(i);
+        if (s instanceof String) {
+            ((String)s).getChars(start, end, value, count);
+        } else {
+            for (int i = start, j = count; i < end; i++, j++)
+                value[j] = s.charAt(i);
+        }
         count += len;
         return this;
     }
--- a/jdk/src/java.base/share/classes/java/net/URI.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/net/URI.java	Thu Jul 02 00:26:35 2015 +0300
@@ -2018,7 +2018,7 @@
             StringBuilder sb = new StringBuilder(base.length() + cn);
             // 5.2 (6a)
             if (i >= 0)
-                sb.append(base.substring(0, i + 1));
+                sb.append(base, 0, i + 1);
             // 5.2 (6b)
             sb.append(child);
             path = sb.toString();
@@ -2686,7 +2686,7 @@
                 if (!match(c, lowMask, highMask)) {
                     if (sb == null) {
                         sb = new StringBuffer();
-                        sb.append(s.substring(0, i));
+                        sb.append(s, 0, i);
                     }
                     appendEscape(sb, (byte)c);
                 } else {
@@ -2698,7 +2698,7 @@
                            || Character.isISOControl(c))) {
                 if (sb == null) {
                     sb = new StringBuffer();
-                    sb.append(s.substring(0, i));
+                    sb.append(s, 0, i);
                 }
                 appendEncoded(sb, c);
             } else {
--- a/jdk/src/java.base/share/classes/java/text/MergeCollation.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/text/MergeCollation.java	Thu Jul 02 00:26:35 2015 +0300
@@ -329,8 +329,8 @@
                 PatternEntry e = patterns.get(i);
                 if (e.chars.regionMatches(0,entry.chars,0,
                                               e.chars.length())) {
-                    excessChars.append(entry.chars.substring(e.chars.length(),
-                                                            entry.chars.length()));
+                    excessChars.append(entry.chars, e.chars.length(),
+                            entry.chars.length());
                     break;
                 }
             }
--- a/jdk/src/java.base/share/classes/java/text/MessageFormat.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/text/MessageFormat.java	Thu Jul 02 00:26:35 2015 +0300
@@ -1239,7 +1239,7 @@
         int lastOffset = 0;
         int last = result.length();
         for (int i = 0; i <= maxOffset; ++i) {
-            result.append(pattern.substring(lastOffset, offsets[i]));
+            result.append(pattern, lastOffset, offsets[i]);
             lastOffset = offsets[i];
             int argumentNumber = argumentNumbers[i];
             if (arguments == null || argumentNumber >= arguments.length) {
@@ -1332,7 +1332,7 @@
                 }
             }
         }
-        result.append(pattern.substring(lastOffset, pattern.length()));
+        result.append(pattern, lastOffset, pattern.length());
         if (characterIterators != null && last != result.length()) {
             characterIterators.add(createAttributedCharacterIterator(
                                    result.substring(last)));
--- a/jdk/src/java.base/share/classes/sun/invoke/util/BytecodeName.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/sun/invoke/util/BytecodeName.java	Thu Jul 02 00:26:35 2015 +0300
@@ -511,7 +511,7 @@
                 if (s.charAt(0) != ESCAPE_C && i > 0)
                     sb.append(NULL_ESCAPE);
                 // append the string so far, which is unremarkable:
-                sb.append(s.substring(0, i));
+                sb.append(s, 0, i);
             }
 
             // rewrite \ to \-, / to \|, etc.
@@ -544,7 +544,7 @@
                     if (sb == null) {
                         sb = new StringBuilder(s.length());
                         // append the string so far, which is unremarkable:
-                        sb.append(s.substring(stringStart, i));
+                        sb.append(s, stringStart, i);
                     }
                     ++i;  // skip both characters
                     c = oc;
--- a/jdk/src/java.base/share/classes/sun/net/www/ParseUtil.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/sun/net/www/ParseUtil.java	Thu Jul 02 00:26:35 2015 +0300
@@ -451,7 +451,7 @@
                 if (!match(c, lowMask, highMask) && !isEscaped(s, i)) {
                     if (sb == null) {
                         sb = new StringBuffer();
-                        sb.append(s.substring(0, i));
+                        sb.append(s, 0, i);
                     }
                     appendEscape(sb, (byte)c);
                 } else {
@@ -463,7 +463,7 @@
                            || Character.isISOControl(c))) {
                 if (sb == null) {
                     sb = new StringBuffer();
-                    sb.append(s.substring(0, i));
+                    sb.append(s, 0, i);
                 }
                 appendEncoded(sb, c);
             } else {
--- a/jdk/src/java.base/share/classes/sun/text/normalizer/UnicodeSet.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/sun/text/normalizer/UnicodeSet.java	Thu Jul 02 00:26:35 2015 +0300
@@ -1850,7 +1850,7 @@
             syntaxError(chars, "Invalid property pattern");
         }
         chars.jumpahead(pos.getIndex());
-        rebuiltPat.append(patStr.substring(0, pos.getIndex()));
+        rebuiltPat.append(patStr, 0, pos.getIndex());
     }
 
     //----------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/sun/util/BuddhistCalendar.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/share/classes/sun/util/BuddhistCalendar.java	Thu Jul 02 00:26:35 2015 +0300
@@ -242,12 +242,13 @@
             return s;
         }
         p += yearField.length();
-        StringBuilder sb = new StringBuilder(s.substring(0, p));
+        StringBuilder sb = new StringBuilder(s.length() + 10);
+        sb.append(s, 0, p);
         // Skip the year number
         while (Character.isDigit(s.charAt(p++)))
             ;
         int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET;
-        sb.append(year).append(s.substring(p - 1));
+        sb.append(year).append(s, p - 1, s.length());
         return sb.toString();
     }
 
--- a/jdk/src/java.base/unix/classes/java/io/UnixFileSystem.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/unix/classes/java/io/UnixFileSystem.java	Thu Jul 02 00:26:35 2015 +0300
@@ -65,8 +65,8 @@
         int n = len;
         while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--;
         if (n == 0) return "/";
-        StringBuffer sb = new StringBuffer(pathname.length());
-        if (off > 0) sb.append(pathname.substring(0, off));
+        StringBuilder sb = new StringBuilder(pathname.length());
+        if (off > 0) sb.append(pathname, 0, off);
         char prevChar = 0;
         for (int i = off; i < n; i++) {
             char c = pathname.charAt(i);
--- a/jdk/src/java.base/windows/classes/java/io/WinNTFileSystem.java	Wed Jul 01 22:17:32 2015 +0300
+++ b/jdk/src/java.base/windows/classes/java/io/WinNTFileSystem.java	Thu Jul 02 00:26:35 2015 +0300
@@ -104,7 +104,7 @@
         if (off < 3) off = 0;   /* Avoid fencepost cases with UNC pathnames */
         int src;
         char slash = this.slash;
-        StringBuffer sb = new StringBuffer(len);
+        StringBuilder sb = new StringBuilder(len);
 
         if (off == 0) {
             /* Complete normalization, including prefix */
@@ -112,7 +112,7 @@
         } else {
             /* Partial normalization */
             src = off;
-            sb.append(path.substring(0, off));
+            sb.append(path, 0, off);
         }
 
         /* Remove redundant slashes from the remainder of the path, forcing all
@@ -156,8 +156,7 @@
             }
         }
 
-        String rv = sb.toString();
-        return rv;
+        return sb.toString();
     }
 
     /* A normal Win32 pathname contains no duplicate slashes, except possibly
@@ -172,7 +171,7 @@
                 else directory-relative (has form "z:foo")
            3  absolute local pathname (begins with "z:\\")
      */
-    private int normalizePrefix(String path, int len, StringBuffer sb) {
+    private int normalizePrefix(String path, int len, StringBuilder sb) {
         int src = 0;
         while ((src < len) && isSlash(path.charAt(src))) src++;
         char c;