Skip to content

Add @Stable to LocaleProviderAdapter #26354

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

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

wenshao
Copy link
Contributor

@wenshao wenshao commented Jul 16, 2025

I read @minberg 's PR #25727 (JDK-8359119: Change Charset to use StableValue ) and found that LocaleProviderAdapter can also be optimized.

  1. We can provide the adapter field in LocaleProviderAdapter$Type and replace the original ConcurrentMap.putIfAbsent with LazyInit + Stable
public abstract class LocaleProviderAdapter {
    public enum Type {
        @Stable
        private volatile LocaleProviderAdapter adapter;
    }
}
  1. In LocaleProviderAdapter$Type::getAdapter, switch + new is used instead of the original reflection, which can improve startup performance and maintainability.
        private synchronized LocaleProviderAdapter getAdapter0() {
            if (adapter == null) {
                // lazily load adapters here
                adapter = switch (this) {
                    case JRE -> new JRELocaleProviderAdapter();
                    case CLDR -> new CLDRLocaleProviderAdapter();
                    case SPI -> new SPILocaleProviderAdapter();
                    case HOST -> new HostLocaleProviderAdapter();
                    case FALLBACK -> new FallbackLocaleProviderAdapter();
                    default -> throw new ServiceConfigurationError("Locale provider adapter \"" +
                            this + "\"cannot be instantiated.");
                };
            }
            return adapter;
        }
  1. Using multiple ifs in LocaleProviderAdapter instead of ConcurrentHashMap.putIfAbsent allows C2 Inline to optimize unnecessary code branches
    private static ConcurrentMap<Locale, LocaleProviderAdapter> getAdapterMap(Class<? extends LocaleServiceProvider> providerClass) {
        if (providerClass == BreakIteratorProvider.class)           return breakIteratorProviderAdapterMap;
        if (providerClass == CollatorProvider.class)                return collatorProviderAdapterMap;
        if (providerClass == DateFormatProvider.class)              return dateFormatProviderAdapterMap;
        if (providerClass == DateFormatSymbolsProvider.class)       return dateFormatSymbolsProviderAdapterMap;
        if (providerClass == DecimalFormatSymbolsProvider.class)    return decimalFormatSymbolsProviderAdapterMap;
        if (providerClass == NumberFormatProvider.class)            return numberFormatProviderAdapterMap;
        if (providerClass == CurrencyNameProvider.class)            return currencyNameProviderAdapterMap;
        if (providerClass == LocaleNameProvider.class)              return localeNameProviderAdapterMap;
        if (providerClass == TimeZoneNameProvider.class)            return timeZoneNameProviderAdapterMap;
        if (providerClass == CalendarDataProvider.class)            return calendarDataProviderAdapterMap;
        if (providerClass == CalendarNameProvider.class)            return calendarNameProviderAdapterMap;
        if (providerClass == CalendarProvider.class)                return calendarProviderAdapterMap;
        if (providerClass == JavaTimeDateTimePatternProvider.class) return javaTimeDateTimePatternProviderAdapterMap;
        throw new InternalError("should not come down here");
    }

In this way, calls like the following can fold away unnecessary branches

  adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
                desiredLocale);
  1. In the JRELocaleProviderAdapter::getLocaleServiceProvider method, use multiple ifs instead of switch(String) so that CodeSize < 325 can be inlined

  2. Add @stable to the immutable fields of FallbackLocaleProviderAdapter/JRELocaleProviderAdapter/CLDRLocaleProviderAdapter


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26354/head:pull/26354
$ git checkout pull/26354

Update a local copy of the PR:
$ git checkout pull/26354
$ git pull https://git.openjdk.org/jdk.git pull/26354/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26354

View PR using the GUI difftool:
$ git pr show -t 26354

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26354.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 16, 2025

👋 Welcome back swen! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jul 16, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jul 16, 2025

@wenshao The following labels will be automatically applied to this pull request:

  • core-libs
  • i18n

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org labels Jul 16, 2025
@wenshao wenshao changed the title Change Locale to use StableValue Add @Stable to LocaleProviderAdapter Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

1 participant