Skip to content

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

@davidricodias

Description

@davidricodias

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 -> [JAVA] [PERF] improve java enum fromValue generation #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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions