From 42b5daacc42f89b58c11bdf772597ea4be35f640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 19 Feb 2023 14:46:47 +0100 Subject: [PATCH] performance: cache parsed constraints and versions --- src/poetry/core/constraints/generic/parser.py | 2 ++ src/poetry/core/constraints/version/parser.py | 2 ++ src/poetry/core/version/pep440/parser.py | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/poetry/core/constraints/generic/parser.py b/src/poetry/core/constraints/generic/parser.py index abfba8eb5..9b5185385 100644 --- a/src/poetry/core/constraints/generic/parser.py +++ b/src/poetry/core/constraints/generic/parser.py @@ -1,5 +1,6 @@ from __future__ import annotations +import functools import re from typing import TYPE_CHECKING @@ -17,6 +18,7 @@ BASIC_CONSTRAINT = re.compile(r"^(!?==?)?\s*([^\s]+?)\s*$") +@functools.lru_cache(maxsize=None) def parse_constraint(constraints: str) -> BaseConstraint: if constraints == "*": return AnyConstraint() diff --git a/src/poetry/core/constraints/version/parser.py b/src/poetry/core/constraints/version/parser.py index 118a78cc3..86d59f649 100644 --- a/src/poetry/core/constraints/version/parser.py +++ b/src/poetry/core/constraints/version/parser.py @@ -1,5 +1,6 @@ from __future__ import annotations +import functools import re from typing import TYPE_CHECKING @@ -12,6 +13,7 @@ from poetry.core.constraints.version.version_constraint import VersionConstraint +@functools.lru_cache(maxsize=None) def parse_constraint(constraints: str) -> VersionConstraint: if constraints == "*": from poetry.core.constraints.version.version_range import VersionRange diff --git a/src/poetry/core/version/pep440/parser.py b/src/poetry/core/version/pep440/parser.py index 11fae074d..54c92db21 100644 --- a/src/poetry/core/version/pep440/parser.py +++ b/src/poetry/core/version/pep440/parser.py @@ -1,5 +1,6 @@ from __future__ import annotations +import functools import re from typing import TYPE_CHECKING @@ -63,6 +64,7 @@ def _get_local(cls, match: Match[str] | None) -> LocalSegmentType | None: ) @classmethod + @functools.lru_cache(maxsize=None) def parse(cls, value: str, version_class: type[T]) -> T: match = cls._regex.search(value) if value else None if not match: