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

perf: cache missing XBlock mappings in load_class #813

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion xblock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
XBlock Courseware Components
"""

__version__ = '5.1.0'
__version__ = '5.1.1'
56 changes: 35 additions & 21 deletions xblock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,42 @@ def select(identifier, all_entry_points):
"""
identifier = identifier.lower()
key = (cls.entry_point, identifier)
if key not in PLUGIN_CACHE:

if select is None:
select = default_select

all_entry_points = [
*importlib.metadata.entry_points(group=f'{cls.entry_point}.overrides', name=identifier),
*importlib.metadata.entry_points(group=cls.entry_point, name=identifier)
]

for extra_identifier, extra_entry_point in iter(cls.extra_entry_points):
if identifier == extra_identifier:
all_entry_points.append(extra_entry_point)

try:
selected_entry_point = select(identifier, all_entry_points)
except PluginMissingError:
if default is not None:
return default
raise

PLUGIN_CACHE[key] = cls._load_class_entry_point(selected_entry_point)
if key in PLUGIN_CACHE:
xblock_cls = PLUGIN_CACHE[key] or default
if xblock_cls:
return xblock_cls

# If the key was in PLUGIN_CACHE, but the value stored was None, and
# there was no default class to fall back to, we give up and raise
# PluginMissingError. This is for performance reasons. We've cached
# the fact that there is no XBlock class that maps to this
# identifier, and it is very expensive to check through the entry
# points each time. This assumes that XBlock class mappings do not
# change during the life of the process.
raise PluginMissingError(identifier)

if select is None:
select = default_select

all_entry_points = [
*importlib.metadata.entry_points(group=f'{cls.entry_point}.overrides', name=identifier),
*importlib.metadata.entry_points(group=cls.entry_point, name=identifier)
]

for extra_identifier, extra_entry_point in iter(cls.extra_entry_points):
if identifier == extra_identifier:
all_entry_points.append(extra_entry_point)

try:
selected_entry_point = select(identifier, all_entry_points)
except PluginMissingError:
PLUGIN_CACHE[key] = None
if default is not None:
return default
raise

PLUGIN_CACHE[key] = cls._load_class_entry_point(selected_entry_point)

return PLUGIN_CACHE[key]

Expand Down
Loading