LANG-1358: Improve StringUtils#replace throughput (closes #300)

Motivation:

`StringUtils#replace` uses `substring` to append a String region into a
StringBuilder. This causes useless copies, as `StringBuilder#append`
can take start and end indexes.

Modification:

Use proper `StringBuilder#append` [overload](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#append-java.lang.CharSequence-int-int-).

Result:

Based on benchmark from [JOOQ’s post](https://blog.jooq.org/2017/10/11/benchmarking-jdk-string-replace-vs-apache-commons-stringutils-replace):

Benchmark Mode Cnt Score Error Units
StringReplaceBenchmark.testFastStringReplaceLongStringOneMatch thrpt 21
7546534,219 ± 145523,962 ops/s
StringReplaceBenchmark.testStringUtilsReplaceLongStringOneMatch thrpt
21 7353512,552 ± 124498,228 ops/s

StringReplaceBenchmark.testFastStringReplaceLongStringSeveralMatches
thrpt 21 5077255,810 ± 62358,937 ops/s
StringReplaceBenchmark.testStringUtilsReplaceLongStringSeveralMatches
thrpt 21 4108357,612 ± 92909,038 ops/s

StringReplaceBenchmark.testFastStringReplaceShortStringOneMatch thrpt
21 15911221,949 ± 541064,693 ops/s
StringReplaceBenchmark.testStringUtilsReplaceShortStringOneMatch thrpt
21 10677897,475 ± 491091,973 ops/s

StringReplaceBenchmark.testFastStringReplaceShortStringSeveralMatches
thrpt 21 9271742,251 ± 220150,121 ops/s
StringReplaceBenchmark.testStringUtilsReplaceShortStringSeveralMatches
thrpt 21 6158829,188 ± 99637,607 ops/s
1 file changed
tree: 42b249367cfe99f47e094ade4700892b6357e988
  1. src/
  2. .gitattributes
  3. .gitignore
  4. .travis.yml
  5. checkstyle-suppressions.xml
  6. checkstyle.xml
  7. CONTRIBUTING.md
  8. findbugs-exclude-filter.xml
  9. LICENSE.txt
  10. NOTICE.txt
  11. pom.xml
  12. README.md
  13. RELEASE-NOTES.txt
README.md

Apache Commons Lang

Build Status Coverage Status Maven Central Javadocs

Apache Commons Lang, a package of Java utility classes for the classes that are in java.lang's hierarchy, or are considered to be so standard as to justify existence in java.lang.

Documentation

More information can be found on the homepage. The JavaDoc can be browsed. Questions related to the usage of Apache Commons Lang should be posted to the user mailing list.

Where can I get the latest release?

You can download source and binaries from our download page.

Alternatively you can pull it from the central Maven repositories:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.6</version>
</dependency>

Contributing

We accept PRs via github. The developer mailing list is the main channel of communication for contributors. There are some guidelines which will make applying PRs easier for us:

  • No tabs! Please use spaces for indentation.
  • Respect the code style.
  • Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted create a separate PR for this change.
  • Provide JUnit tests for your changes and make sure your changes don't break any existing tests by running mvn clean test.

If you plan to contribute on a regular basis, please consider filing a contributor license agreement. You can learn more about contributing via GitHub in our contribution guidelines.

License

Code is under the Apache Licence v2.

Donations

You like Apache Commons Lang? Then donate back to the ASF to support the development.

Additional Resources