OpenAPITools / OpenAPITools/openapi-generator

[JAVA] [PERF] Use maps for lookups in enum fromValue methods

Open
#22,996 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

The current modelEnum.mustache templates implementation for fromValue for the JVM has an O(n) worst case.

Describe the solution you'd like

I want the fromValue to use a map to retrieve the enum value in constant time.

Describe alternatives you've considered

Possible alternatives are

  1. Ignore this request. The implementation and possible impacts are too troublesome for the project.
  2. Use hashmaps/treemaps for the lookup. I have opened a PR with benchmarks and the implementation targeting Java -> https://github.com/OpenAPITools/openapi-generator/pull/22995

Additional context

I have run jmh benchmnarks, which yield the following results (MacOS ARM 26;JMH version: 1.37;JDK 11.0.22, OpenJDK 64-Bit Server VM, 11.0.22+7-LTS)

Benchmark                                                        (inputSize)   Mode  Cnt         Score         Error  Units
EnumLookupJmhBenchmark.hashMapLookupCaseInsensitive                    50000  thrpt    5  25144177.892 ±  820137.701  ops/s
EnumLookupJmhBenchmark.hashMapLookupWithOptional                       50000  thrpt    5  99265713.896 ± 2460266.752  ops/s
EnumLookupJmhBenchmark.hashMapLookupWithoutOptional                    50000  thrpt    5  99777930.270 ± 2376641.407  ops/s
EnumLookupJmhBenchmark.linearLookup                                    50000  thrpt    5  24858482.938 ±  367926.448  ops/s
EnumLookupJmhBenchmark.linearLookupCaseInsensitive                     50000  thrpt    5  12758767.698 ±  667129.809  ops/s
EnumLookupJmhBenchmark.treeMapLookupCaseInsensitiveWithOptional        50000  thrpt    5  14069454.341 ±  331263.365  ops/s

The benchmarks can be run by anyone, the command is provided.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked pull request 22995, then inspect the JVM implementation in the modelEnum.mustache template and the EnumLookupJmhBenchmark results described here. Done means the generated fromValue lookup uses the agreed map-based approach and the relevant benchmarks or tests validate the change without regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.