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

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

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b03
    • CPU:
      x86
    • OS:
      windows_xp
    • Verification:
      Not verified

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: