Skip to content

improve speed of generator#23272

Open
Picazsoo wants to merge 9 commits intoOpenAPITools:masterfrom
Picazsoo:feature/fix-speed-bottlenecks
Open

improve speed of generator#23272
Picazsoo wants to merge 9 commits intoOpenAPITools:masterfrom
Picazsoo:feature/fix-speed-bottlenecks

Conversation

@Picazsoo
Copy link
Contributor

@Picazsoo Picazsoo commented Mar 16, 2026

For me these changes sped up sample regen via ./bin/generate-samples.sh from 3m9s to 1m59s

I have another branch where I prevent unnecessary recompilation of regex patterns and am down to 1m54s.
This is on Ryzen 9950x3D with 64GB ram. I guess the improvement will be more noticeable on slower machines.

Update: Down to 1m45s.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Speed up code generation by caching template contents and resolved paths, adding per‑executor engine/template caches, and tightening duplicate/file checks. This reduces CPU and disk I/O with no output changes.

  • Performance

    • Cache template contents in TemplateManager; cache resolved template paths (including misses) in GeneratorTemplateContentLocator; precompile file‑separator pattern for classpath paths; add early file‑length check before byte comparison.
    • Add per‑executor caches in HandlebarsEngineAdapter (engines and compiled templates) and MustacheEngineAdapter (compiled templates) using ConcurrentHashMap; no cache clearing or cross‑executor sharing.
    • Use a lowercased shadow set for O(1) case‑insensitive duplicate path detection in DefaultGenerator.
    • In DefaultCodegen: precompile trailing‑digits pattern; cache Unicode remove‑char regexes; speed up case‑insensitive unique name generation.
  • Refactors

    • Consistent logging placeholders (including throwable), final fields, simpler boolean/null checks, and small null‑safety tweaks. No behavior changes.

Written for commit 9941a95. Summary will update on new commits.

@Picazsoo Picazsoo marked this pull request as ready for review March 16, 2026 21:59
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/MustacheEngineAdapter.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/MustacheEngineAdapter.java:73">
P2: Template cache key omits executor, so cached mustache templates can be reused with stale template sources/partials from a previous executor.</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/HandlebarsEngineAdapter.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/HandlebarsEngineAdapter.java:118">
P2: `IOException` from Handlebars compilation is converted into `RuntimeException`, changing the method’s checked-exception behavior and breaking expected caller error handling.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/MustacheEngineAdapter.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/MustacheEngineAdapter.java:82">
P2: Executor-bound template cache invalidation is non-atomic, allowing cross-executor template contamination under concurrent calls.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@Picazsoo Picazsoo marked this pull request as draft March 16, 2026 22:56
@Picazsoo Picazsoo marked this pull request as ready for review March 17, 2026 07:25
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

@wing328
Copy link
Member

wing328 commented Mar 17, 2026

thanks for the PR

cc @OpenAPITools/generator-core-team

@Picazsoo
Copy link
Contributor Author

Yeah - this one should definitely wait for the next release (not the imminent one) to be on the safe side.

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 this pull request may close these issues.

2 participants