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

Use normalized names to distinguish unique distributions for performance #317

Merged
merged 3 commits into from May 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,10 @@
v4.3.0
=======

* #317: De-duplication of distributions no longer requires
loading the full metadata for ``PathDistribution`` objects,
entry point loading performance by ~10x.

v4.2.0
=======

Expand Down
14 changes: 13 additions & 1 deletion importlib_metadata/__init__.py
Expand Up @@ -498,6 +498,11 @@ def name(self):
"""Return the 'Name' metadata for the distribution package."""
return self.metadata['Name']

@property
def _normalized_name(self):
"""Return a normalized version of the name."""
return Prepared.normalize(self.name)

@property
def version(self):
"""Return the 'Version' metadata for the distribution package."""
Expand Down Expand Up @@ -805,6 +810,12 @@ def read_text(self, filename):
def locate_file(self, path):
return self._path.parent / path

@property
def _normalized_name(self):
stem = os.path.basename(str(self._path))
name, sep, rest = stem.partition('-')
return name


def distribution(distribution_name):
"""Get the ``Distribution`` instance for the named package.
Expand Down Expand Up @@ -859,7 +870,8 @@ def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:

:return: EntryPoints or SelectableGroups for all installed packages.
"""
unique = functools.partial(unique_everseen, key=operator.attrgetter('name'))
norm_name = operator.attrgetter('_normalized_name')
unique = functools.partial(unique_everseen, key=norm_name)
eps = itertools.chain.from_iterable(
dist.entry_points for dist in unique(distributions())
)
Expand Down