Skip to content
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

localedata.locale_identifiers is slow #620

Closed
lexh opened this issue Jan 3, 2019 · 1 comment · Fixed by #644
Closed

localedata.locale_identifiers is slow #620

lexh opened this issue Jan 3, 2019 · 1 comment · Fixed by #644

Comments

@lexh
Copy link

lexh commented Jan 3, 2019

localedata.locale_identifiers is slow

localedata.locale_identifiers reads the available locale identifiers from disk every time it is called. Since the locale data files are distributed with the lib, there shouldn't be a need to scan the disk every time this is called, as it could be cached.

I noticed a hot spot in this method when profiling a service that uses this library. Specifically I noticed that babel.Locale.parse performs very poorly when supplied bogus locales. I ran a small benchmark to demonstrate this:

Steps to Reproduce

import timeit

import babel


def wrapped():
    try:
        babel.Locale.parse("en_ZH")
    except babel.UnknownLocaleError:
        pass


print(timeit.timeit(wrapped, number=1000))

Actual Results

Above snippet takes several seconds to execute.

Output on my machine:

3.91633701324

Expected Results

Should be much faster.

Reproducibility

100%

Additional Information:

Could this information be built up in scripts/import_cldr.py and written to the global.dat file? Then the data could be quickly loaded as needed in localedata.py.

akx added a commit to akx/babel that referenced this issue May 27, 2019
@akx
Copy link
Member

akx commented May 27, 2019

Good catch!

I'll just go with memoizing the result after the first invocation (PR #644).

@akx akx closed this as completed in #644 May 27, 2019
akx added a commit that referenced this issue May 27, 2019
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 a pull request may close this issue.

2 participants