…enum
Replace the 60-70 branch if-else chains in Joda.forPattern() and
DateFormatters.forPattern() with switch statements on the FormatNames
enum value already resolved by FormatNames.forName(input).
Previously, each method called FormatNames.forName(input) for the
camel-case deprecation check (a full linear scan) and then immediately
ran a second full linear scan via the if-else chain, calling
FormatNames.XXX.matches(input) on every branch (up to 120 string
comparisons in the worst case).
The switch dispatch compiles to a JVM tableswitch/lookupswitch
instruction (O(1)) and eliminates the redundant second scan entirely.
The observable behaviour is identical.
Benefits:
- O(1) enum dispatch vs O(n) string comparisons on the hot path
- Compiler exhaustiveness checking: missing FormatNames cases are
now detectable at build time via IDE/spotbugs warnings
- Improved readability: 1-to-1 mapping between FormatNames constant
and formatter is immediately visible
Signed-off-by: Rajat Jain <rajatjain.ix@gmail.com>
Fixes: #22567
Description
Replace the 60-70 branch if-else chains in Joda.forPattern() and DateFormatters.forPattern() with switch statements on the FormatNames enum value already resolved by FormatNames.forName(input).
Previously, each method called FormatNames.forName(input) for the camel-case deprecation check (a full linear scan) and then immediately ran a second full linear scan via the if-else chain, calling FormatNames.XXX.matches(input) on every branch (up to 120 string comparisons in the worst case).
The switch dispatch compiles to a JVM tableswitch/lookupswitch instruction (O(1)) and eliminates the redundant second scan entirely. The observable behaviour is identical.
Benefits:
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.