Skip to content

Commit

Permalink
Fix pkg_resources->importlib performance regression (cctbx#658)
Browse files Browse the repository at this point in the history
Regression observed in large experiment lists with scaling/profile models.

Only seems to affect Python 3.10, potentially stemming from:
    https://bugs.python.org/issue44246

Fixes cctbx#657
  • Loading branch information
jbeilstenedmands authored and toastisme committed Jul 11, 2024
1 parent 738b1dc commit ad5335e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions newsfragments/658.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix performance regression loading large experiment lists containing profile/scaling models.
10 changes: 9 additions & 1 deletion src/dxtbx/model/experiment_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class InvalidExperimentListError(RuntimeError):
"""


try:
scaling_model_entry_points = importlib.metadata.entry_points()[
"dxtbx.scaling_model_ext"
]
except KeyError:
scaling_model_entry_points = []


class FormatChecker:
"""A helper class to speed up identifying the correct image format by first
trying the last format that was used."""
Expand Down Expand Up @@ -576,7 +584,7 @@ def _lookup_model(self, name, experiment_dict):
@staticmethod
def _scaling_model_from_dict(obj):
"""Get the scaling model from a dictionary."""
for entry_point in importlib.metadata.entry_points()["dxtbx.scaling_model_ext"]:
for entry_point in scaling_model_entry_points:
if entry_point.name == obj["__id__"]:
return entry_point.load().from_dict(obj)

Expand Down
7 changes: 6 additions & 1 deletion src/dxtbx/model/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import importlib.metadata
import logging

try:
profile_entry_points = importlib.metadata.entry_points()["dxtbx.profile_model"]
except KeyError:
profile_entry_points = []


class ProfileModelFactory:
"""
Expand All @@ -16,7 +21,7 @@ def from_dict(obj):
"""
if obj is None:
return None
for entry_point in importlib.metadata.entry_points()["dxtbx.profile_model"]:
for entry_point in profile_entry_points:
if entry_point.name == obj["__id__"]:
return entry_point.load().from_dict(obj)
logging.getLogger("dxtbx.model.profile").warn(
Expand Down

0 comments on commit ad5335e

Please sign in to comment.