Skip to content

java: bad performance in StringUtils #84

@jkronegg

Description

@jkronegg

👓 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions