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

Add proper caching to LocaleDataMap #16982

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions source/characterProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
"""
self._localeDataFactory: Callable[[str], _LocaleDataT] = localeDataFactory
self._dataMap: Dict[str, _LocaleDataT] = {}
self._noDataLocalesCache: set[str] = set()

def fetchLocaleData(self, locale: str, fallback: bool = True) -> _LocaleDataT:
"""
Expand All @@ -60,10 +61,14 @@ def fetchLocaleData(self, locale: str, fallback: bool = True) -> _LocaleDataT:
data = self._dataMap.get(loc)
if data:
return data
try:
data = self._localeDataFactory(loc)
except LookupError:
elif loc in self._noDataLocalesCache:
data = None
else:
try:
data = self._localeDataFactory(loc)
except LookupError:
self._noDataLocalesCache.add(loc)
data = None
if not data:
continue
self._dataMap[loc] = data
Expand All @@ -79,12 +84,14 @@ def invalidateLocaleData(self, locale: str) -> None:
del self._dataMap[locale]
except KeyError:
pass
self._noDataLocalesCache.discard(locale)

def invalidateAllData(self):
"""Invalidate all data within this locale map.
This will cause a new data object to be created for every locale that is next requested.
"""
self._dataMap.clear()
self._noDataLocalesCache.clear()


class CharacterDescriptions(object):
Expand Down