Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6355654

(str) String.contentEquals(CharSequence) calculates twice for AbstractStringBuilder

XMLWordPrintable

    • b03
    • x86
    • windows_xp
    • Not verified

      A DESCRIPTION OF THE REQUEST :
      In String.contentEquals(CharSquence), if an AbstractStringBuilder has the same value as the String the method will compare the values of the two objects twice, instead of returning true after they were clearly the same the first time.

      Quotation from source of jdk.1.6.0:

      public boolean contentEquals(CharSequence cs) {
              if (count != cs.length())
                  return false;
              // Argument is a StringBuffer, StringBuilder
              if (cs instanceof AbstractStringBuilder) {
                  char v1[] = value;
                  char v2[] = ((AbstractStringBuilder)cs).getValue();
                  int i = offset;
                  int j = 0;
                  int n = count;
                  while (n-- != 0) {
                      if (v1[i++] != v2[j++])
                          return false;
                  }
                  // ---------------------- The content is clearly the same here, because all the individual chars were the same.
                  //----------------------- 'return true;' should be added here, to improve efficiency.
              }
              // Argument is a String
              if (cs.equals(this))
                  return true;
              // Argument is a generic CharSequence
              char v1[] = value;
              int i = offset;
              int j = 0;
              int n = count;
              while (n-- != 0) {
                  if (v1[i++] != cs.charAt(j++))
                      return false;
              }
              return true;
          }


      Adding this return would make the method almost twice as fast for AbstractStringBuilder's.


      JUSTIFICATION :
      This enhancement is needed to provide a reasonable amount of efficiency for AbstractStringBuilder's in this method.

            iris Iris Clark
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: