Skip to content

Commit 37986f9

Browse files
committed
Use str.lower and replace to further improve performance of normalize
1 parent 89d9346 commit 37986f9

1 file changed

Lines changed: 2 additions & 11 deletions

File tree

importlib_metadata/__init__.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -910,14 +910,6 @@ def search(self, prepared: Prepared):
910910
return itertools.chain(infos, eggs)
911911

912912

913-
# Translation table for Prepared.normalize: lowercase and
914-
# replace "-" (hyphen) and "." (dot) with "_" (underscore).
915-
_normalize_table = str.maketrans(
916-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-.",
917-
"abcdefghijklmnopqrstuvwxyz__",
918-
)
919-
920-
921913
class Prepared:
922914
"""
923915
A prepared search query for metadata on a possibly-named package.
@@ -953,9 +945,8 @@ def normalize(name):
953945
"""
954946
PEP 503 normalization plus dashes as underscores.
955947
"""
956-
# Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
957-
# About 3x faster, safe since packages only support alphanumeric characters
958-
value = name.translate(_normalize_table)
948+
# Much faster than re.sub, and even faster than str.translate
949+
value = name.lower().replace("-", "_").replace(".", "_")
959950
# Condense repeats (faster than regex)
960951
while "__" in value:
961952
value = value.replace("__", "_")

0 commit comments

Comments
 (0)