Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java: bad performance in StringUtils #84

Closed
jkronegg opened this issue Dec 29, 2022 · 0 comments · Fixed by #85
Closed

java: bad performance in StringUtils #84

jkronegg opened this issue Dec 29, 2022 · 0 comments · Fixed by #85

Comments

@jkronegg
Copy link
Contributor

👓 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

mpkorstanje pushed a commit that referenced this issue Jan 3, 2023
Precompiled the Pattern used in the utility methods.

Improves performance. `StringUtils.trim()` is more than 3 times faster.

Fixes: #84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant