From 92dedb3658777f643b95abaf79f837a5c1799b1e Mon Sep 17 00:00:00 2001 From: Nevada Sanchez Date: Mon, 3 Feb 2020 14:26:58 -0500 Subject: [PATCH] Cache the results of match, validate, and compare I've found that calling `semantic_version.match(...)` quickly becomes the performance bottleneck in tight loops, forcing me to wrap it in another function that I can put behind an `lru_cache`. I propose working into the package permanently. --- semantic_version/base.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/semantic_version/base.py b/semantic_version/base.py index 871ccb0..cedbe46 100644 --- a/semantic_version/base.py +++ b/semantic_version/base.py @@ -563,14 +563,17 @@ def __hash__(self): return hash((self.kind, self.spec)) +@functools.lru_cache() def compare(v1, v2): return Version(v1).__cmp__(Version(v2)) +@functools.lru_cache() def match(spec, version): return Spec(spec).match(Version(version)) +@functools.lru_cache() def validate(version_string): """Validates a version string againt the SemVer specification.""" try: