Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1057

Micro optimization: Replace StringBuilder with String concatenation so that the compiler can better optimize the code

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.5
    • None

    Description

      This is a placeholder issue for github PR https://github.com/apache/commons-lang/pull/36

      Some parts of our code base use StringBuilder for simple string concatenation. But if one compare the generated byte code of:

      String foo = "str1" + var + "str2"
      

      with

      String foo = StringBuilder().append("str1").append(var).append("str2)
      

      it become apperent that the first is a bit more efficient. This is, because the compiler will generate the following as byte code:

      String foo = StringBuilder("str1").append(var).append(str2)
      

      Furthermore by using simple String concatenation we leave the choice of how to concat the strings to compiler implementors, which may make the code more efficient when it is compiled on future compilers.

      Attachments

        Activity

          People

            chonton Charles Honton
            britter Benedikt Ritter
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: