Skip to content

Commit 76e2272

Browse files
hugovkhenryiii
andcommitted
Use translate to improve performance of canonicalize_name
Co-Authored-By: Henry Schreiner <henryschreineriii@gmail.com>
1 parent ce6bae9 commit 76e2272

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Lib/importlib/metadata/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,13 @@ def search(self, prepared: Prepared):
890890
return itertools.chain(infos, eggs)
891891

892892

893+
# Translation table for Prepared.normalize: lowercase and replace - . with _
894+
_normalize_table = str.maketrans(
895+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-.",
896+
"abcdefghijklmnopqrstuvwxyz__",
897+
)
898+
899+
893900
class Prepared:
894901
"""
895902
A prepared search query for metadata on a possibly-named package.
@@ -925,7 +932,13 @@ def normalize(name):
925932
"""
926933
PEP 503 normalization plus dashes as underscores.
927934
"""
928-
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
935+
# Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
936+
# About 3x faster, safe since packages only support alphanumeric characters
937+
value = name.translate(_normalize_table)
938+
# Condense repeats (faster than regex)
939+
while "__" in value:
940+
value = value.replace("__", "_")
941+
return value
929942

930943
@staticmethod
931944
def legacy_normalize(name):

0 commit comments

Comments
 (0)