Commons Lang
  1. Commons Lang
  2. LANG-148

[lang] Performance modifications on StringUtils.replace

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2
    • Component/s: lang.*
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

      Description

      I dont know how picky you are about performance, but I think it would be nice
      for methods like this not to create new objects (StringBuffers) and copy over,
      if it doesnt need to (e.g. if what you are searching for is not in the "text"
      String). Also, if the text is empty, we can return it right away. And, my
      profiler gets two StringBuffers and an arrayCopy here sometimes since we only
      allocate enough for the original String. I think overall it is better to
      allocate a little more (+20%) for the StringBuffer to reduce this possibility
      and only have 1 StringBuffer to be garbage collected. Feel free to pick and
      choose from any of these ideas.

      public static String replace(String text, String repl, String with, int max) {

      could be written as:

      public static String replace(String text, String repl, String with, int max)
      {
      if (isEmpty(text) || isEmpty(repl) || with == null || max == 0)

      { return text; }
      int start = 0, end = text.indexOf(repl, start);
      if (end == -1) { return text; }

      //add a bit of padding so we reduce the StringBuffer extend capacity
      StringBuffer buf = new StringBuffer(text.length()
      + (text.length() < 20 ? 4 : text.length()/5));
      while (end != -1) {
      buf.append(text.substring(start, end)).append(with);
      start = end + repl.length();

      if (--max == 0)

      { break; }

      end = text.indexOf(repl, start);
      }
      buf.append(text.substring(start));
      return buf.toString();
      }

      I tested it on the test cases in the javadoc and it worked fine.

      Thanks!
      Chris

        Activity

        Chris created issue -
        Hide
        Stephen Colebourne added a comment -

        Code changed, based on code below, but with different algorithm for sizing.
        Please test this algorithm to see if it still helps with your performance issues.

        Show
        Stephen Colebourne added a comment - Code changed, based on code below, but with different algorithm for sizing. Please test this algorithm to see if it still helps with your performance issues.
        Henri Yandell made changes -
        Field Original Value New Value
        issue.field.bugzillaimportkey 36583 12342547
        Henri Yandell made changes -
        Key COM-2395 LANG-148
        Assignee Jakarta Commons Developers Mailing List [ commons-dev@jakarta.apache.org ]
        Affects Version/s unspecified [ 12311647 ]
        Component/s Lang [ 12311121 ]
        Fix Version/s 2.2 [ 12311686 ]
        Project Commons [ 12310458 ] Commons Lang [ 12310481 ]
        Henri Yandell made changes -
        Fix Version/s 2.2 [ 12311702 ]
        Henri Yandell made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Henri Yandell made changes -
        Component/s lang.* [ 12313203 ]
        Mark Thomas made changes -
        Workflow jira [ 12371452 ] Default workflow, editable Closed status [ 12602391 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Resolved Resolved Closed Closed
        514d 18h 1 Henri Yandell 06/Feb/07 23:47

          People

          • Assignee:
            Unassigned
            Reporter:
            Chris
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development