cucumber/gherkin

java: bad performance in StringUtils

Closed this issue ยท 0 comments

๐Ÿ‘“ What did you see?

There are performance issues in https://github.com/cucumber/gherkin/blob/main/java/src/main/java/io/cucumber/gherkin/StringUtils.java because the String.replaceAll method used in the StringUtils class makes a new Pattern.compile at every call.

The impact is that on a project with ~150 teststeps and ~400 test scenarios, the feature parser uses 0.68% of the total CPU time in the StringUtils.trim() method (about 50 ms). That's not a lot, but the still worth the investment effort.

โœ… What did you expect to see?

I expect the performance StringUtils methods to run faster. It's easy to do so by precompiling the Pattern :

Method Description ops/s
StringUtilsBenchmark.trim0 original version 1'162'751 ยฑ 74'322
StringUtilsBenchmark.trim1 precomputed Pattern.compile 3'861'664 ยฑ 87'344

๐Ÿ“ฆ Which tool/library version are you using?

Cucumber 7.10.1

๐Ÿ”ฌ How could we reproduce it?

Steps to reproduce the behavior:

  1. Create a Maven project with the following dependencies:

    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit-platform-engine</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>org.openjdk.jmh</groupId>
       <artifactId>jmh-generator-annprocess</artifactId>
       <version>1.36</version>
       <scope>test</scope>
    </dependency>
    
  2. Run the JMH benchmark from
    gherkin.zip